Using reduce() with tuples created by zip()

Philippe Sigaud philippe.sigaud at gmail.com
Fri Nov 1 11:44:09 PDT 2013


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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20131101/5d6fdf7f/attachment.html>


More information about the Digitalmars-d-learn mailing list