digitalmars.D.learn - Re: higher-order functions
- Jesse Phillips (14/21) Nov 01 2010 Since when?
- bearophile (4/5) Nov 01 2010 You are right, what I have said doesn't apply to map/filter. Sorry for m...
bearophile Wrote:Simen kjaeraas: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)); }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.
Nov 01 2010
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