higher-order functions

Jesse Phillips jessekphillips+D at gmail.com
Mon Nov 1 15:36:54 PDT 2010


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));

}



More information about the Digitalmars-d-learn mailing list