www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Re: higher-order functions

reply Jesse Phillips <jessekphillips+D gmail.com> writes:
bearophile Wrote:

 Simen kjaeraas:
 They take both, in fact:
 auto cubes = map!((a){ return a*a*a; })(arr);
But that needs to be compile-time constant, so if you have several functions, you need to put them inside a typetuple, or duplicate the code. And some idioms are just not possible.
Since when? import std.stdio: writeln; import std.algorithm : map, iota; import std.conv : to; void main() { int squareDel(int a) { return a*a; } int cubeDel(int a) { return a*a*a; } int funny(int a) { return (a+1+a*2)/a; } auto delarr = [&squareDel, &cubeDel, &funny]; auto arr = iota(1, 10); foreach(del; delarr) writeln(map!(del)(arr)); }
Nov 01 2010
parent bearophile <bearophileHUGS lycos.com> writes:
Jesse Phillips:

 Since when?
You are right, what I have said doesn't apply to map/filter. Sorry for my silly mistake. Bye, bearophile
Nov 01 2010