[BangPypers] List Comprehensions Vs. map/filter (Was: if not with comparision statement in python)

Anand Chitipothu anandology at gmail.com
Mon Aug 1 17:00:15 CEST 2011


> I knew there was a way to better implement flatmap - its a combination
> of itertools.chain.from_iterable and map. Here's a much cleaner code
>
> from itertools import chain
>
> print filter(lambda (x,y,z) : x*x + y*y == z*z,
>    chain.from_iterable(map(
>        lambda x: chain.from_iterable(map(
>            lambda y: chain.from_iterable(map(
>                lambda z: [[x,y,z]],
>                range(y,100))),
>            range(x,100))),
>        range(1,50))))
>

Impressive. But having to return [[x, y, z]] instead of [x, y,z] is a
compromise.

I was trying to translate Python list-comprehensions into Javascript
and here is what I've come up with.

$pyjs.listcomp(
    function(x, y, z) { return [x, y, z]},
    [
        range(1, 100),
        function(x) { return range(x, 100)},
        function(x, y) { return range(y, 100)},
    ],
    function(x, y, z) { return x*x + y*y == z*z;}
)

I haven't worked out the $pyjs.listcomp function implementation yet.

Anand


More information about the BangPypers mailing list