November 01, 2010 Re: higher-order functions | ||||
---|---|---|---|---|
| ||||
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));
}
|
November 01, 2010 Re: higher-order functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jesse Phillips | Jesse Phillips:
> Since when?
You are right, what I have said doesn't apply to map/filter. Sorry for my silly mistake.
Bye,
bearophile
|
Copyright © 1999-2021 by the D Language Foundation