Lazy list/range comprehensions

"Jérôme M. Berger" jeberger at free.fr
Mon Oct 4 11:23:26 PDT 2010


Peter Alexander wrote:
> == 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?
> 
	Given that you can do the same but shorter in Python, then I'd say
yes. Especially since even when choosing an obscure way to define
"squares" it's still more readable...

squares = lambda s: type (s) (x*x for x in seq)

return sum (squares (sequence))

		Jerome
-- 
mailto:jeberger at free.fr
http://jeberger.free.fr
Jabber: jeberger at jabber.fr

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: OpenPGP digital signature
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20101004/2f3163df/attachment.pgp>


More information about the Digitalmars-d mailing list