problem with the variadic template
Koroskin Denis
2korden at gmail.com
Sat Jul 5 15:31:14 PDT 2008
On Sun, 06 Jul 2008 02:19:09 +0400, baleog <maccarka at yahoo.com> wrote:
> Hello
> I want to write "map" function with the Common Lisp "mapcar" behaviour.
> But i don't understand how to write one template for the N-argument
> delegate.
> This is how it works now:
>
> T[] map(T, T1)(T delegate(T1) fun, T1[] xs) {
> T[] result; result.length = xs.length;
> foreach (i,x; xs)
> {
> result[i] = fun(x);
> }
> return result;
> }
> T[] map(T,T1,T2)(T delegate(T1, T2) fun, T1[] xs, T2[] ys) {
> T[] result; result.length = reduce!(min)(xs.length,[ys.length]);
> for (int i = 0; i<result.length; ++i)
> {
> result[i] = fun(xs[i], ys[i]);
> }
> return result;
> }
>
> How can i improve the code?
>
> Thank you
There already exists a map function in D2, see std.algorithm
(http://www.digitalmars.com/d/2.0/phobos/std_algorithm.html), take a loop
at it.
More information about the Digitalmars-d
mailing list