Using reduce() with tuples created by zip()

Craig Dillabaugh cdillaba at cg.scs.carleton.ca
Fri Nov 1 12:39:41 PDT 2013


On Friday, 1 November 2013 at 18:44:23 UTC, Philippe Sigaud wrote:
> reduce!( (result, elem) => result + (elem[0]-elem[1])^^2 
> )(zippedRange, 0)
>
>>
>> This is really where my problem arose. I understood everything 
>> up
>> to here, but I sort of had this idea, "hey zip returns a tuple 
>> so
>> that somehow the compiler
>> was going to figure out for me that function(e) has two values"
>> and is thus
>> a binary function so this should work (the 0.0 on the end was 
>> my
>> start value):
>>
>> reduce!(function(e) { return (e[1]-e[0])*(e[1]-e[0]); })(0.0)
>>
>> However, of course the compiler see's the tuple as just one 
>> value
>> - which is where I made my mistake.
>>
>
> The problem is not tuple, it's that reduce needs a binary 
> function to work.

Yes, that is more or less what I was trying to say. I was
figuring that the compiler could figure out that 'e' was really a
pair of values, thus making function(e) binary.  Of course, that
is asking too much from the compiler.


More information about the Digitalmars-d-learn mailing list