www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - reduce!"a+b"(R) syntax question

reply "Andrew Spott" <andrew.spott gmail.com> writes:
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
Aug 21 2012
next sibling parent reply "Andrew Spott" <andrew.spott gmail.com> writes:
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?
Aug 21 2012
parent "bearophile" <bearophileHUGS lycos.com> writes:
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
Aug 21 2012
prev sibling next sibling parent Timon Gehr <timon.gehr gmx.ch> writes:
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.'
Aug 21 2012
prev sibling parent "Jonathan M Davis" <jmdavisProg gmx.com> writes:
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
Aug 21 2012