reduce!"a+b"(R) syntax question

Jonathan M Davis jmdavisProg at gmx.com
Tue Aug 21 10:46:53 PDT 2012


On Tuesday, August 21, 2012 19:05:45 Andrew Spott wrote:
> When I'm doing an anonymous function for something like reduce,
> how are the arguments determined? Is it alphabetical? Can I use
> any names (reduce!"d-c"(R)?), or are the names defined in the
> function "reduce"?
> 
> This syntax isn't really documented at all in the language
> reference, which makes it a little bit of guess work.

The string lambdas use std.functional.unaryFun or std.functional.binaryFun 
(depending on whether the predicate needs to be unary or binary). In reduce's 
case, it's binary. In either case, the first parameter is always "a", and the 
second (if it's binary) is always "b". There are never more than two 
parameters.

http://dlang.org/phobos/std_functional.html

You can also use the new lambda syntax if you prefer. e.g.

reduce!((a, b) => a + b)(range);

The main downside to the string lambdas is that they don't have access to any 
functions which std.functional doesn't have access to, so basic stuff works 
great with them, but anything that needs delegates or whatnot won't. However, 
where they work, I think that the string lambdas still tend to be better for 
short stuff, since they're less verbose.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list