problem with the variadic template

baleog maccarka at yahoo.com
Sat Jul 5 15:19:09 PDT 2008


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



More information about the Digitalmars-d mailing list