higher-order functions

Simen kjaeraas simen.kjaras at gmail.com
Sun Oct 31 15:57:54 PDT 2010

spir <denis.spir at gmail.com> wrote:

> On Sun, 31 Oct 2010 17:31:54 -0400
> "Nick Sabalausky" <a at a.a> wrote:
>> "spir" <denis.spir at gmail.com> wrote in message
>> news:mailman.45.1288523296.21107.digitalmars-d-learn at puremagic.com...
>> >
>> >Also, I could not find functional methods like map, filter,
>> > reduce in std.functional. Where else? Also not in std.array.
>> std.algorithm. But the docs for it do need to be improved.
> I find it very strange that map/filter/reduce take *strings* as func  
> expressions, instead of function literals:
> 	int[] arr = [ 1, 2, 3, 4 ];
> 	auto squares = map!("a * a")(arr);

They take both, in fact:

auto cubes = map!((a){ return a*a*a; })(arr);

> Why then have func literal? Func parameter is precisely the place where  
> programmers like to use them, isn't it? Isn't this a sign that D func  
> literals are not considered as practical enough?

For very short functions, strings are better, because of the length of
the 'return' keyword. Had D instead always returned the result of the
last line of a function (unless specified to be of void return type),
map/filter/reduce would likely not take strings.


More information about the Digitalmars-d-learn mailing list