Lazy list/range comprehensions

Peter Alexander peter.alexander.au at gmail.com
Sun Oct 3 12:53:47 PDT 2010


== Quote from Jimmy Cao (jcao219 at gmail.com)'s article
> The Python version:
> sum(item * item for item in sequence)
> can be translated to English as:
> "Sum of (item * item) foreach item in sequence"
> While the D version:
> reduce!("a+b")(map!("a*a")(sequence), 0);
> looks to me like:
> Reduce! a plus b map! a times a (sequence) (and a random 0 here).

If you write it as:

auto sum(Range)(Range r) { return reduce!("a+b")(r, 0); }
alias unaryFun("a*a") square;
alias map!(square) squares;

return sum(squares(sequence));

then it looks better than Python :-)

Or can we only judge a language by what's in its standard library?




More information about the Digitalmars-d mailing list