Thread overview
reduce!"a+b"(R) syntax question
Aug 21, 2012
Andrew Spott
Aug 21, 2012
Andrew Spott
Aug 21, 2012
bearophile
Aug 21, 2012
Timon Gehr
Aug 21, 2012
Jonathan M Davis
August 21, 2012
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.

-Andrew
August 21, 2012
On Tuesday, 21 August 2012 at 17:05:46 UTC, 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.
>
> -Andrew

Oh, one more question:

does it act like a delegate?

if I do:

int a = 1;
reduce!"d - c - a"(R);

will that use the "a"? or is that out of scope?  If I want to use
external variables, do I have to do something like:

int a = 1;
reduce!((int c, int d) => d - c - a)(R);

instead?
August 21, 2012
On 08/21/2012 07:05 PM, 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"?

They are defined here:
http://dlang.org/phobos/std_functional.html#binaryFun

>
> This syntax isn't really documented at all in the language
> reference, which makes it a little bit of guess work.
>

This is not part of the language, it is a library artefact:
http://dlang.org/phobos/std_algorithm.html

'Many functions in this module are parameterized with a function or a predicate. The predicate may be passed either as a function name, a delegate name, a functor name, or a compile-time string. The string may consist of any legal D expression that uses the symbol a (for unary functions) or the symbols a and b (for binary functions). These names will NOT interfere with other homonym symbols in user code because they are evaluated in a different context. The default for all binary comparison predicates is "a == b" for unordered operations and "a < b" for ordered operations.'
August 21, 2012
Andrew Spott:

> If I want to use external variables, do I have to do something like:
>
> int a = 1;
> reduce!((int c, int d) => d - c - a)(R);
>
> instead?

Right.

Bye,
bearophile
August 21, 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