[Python-ideas] map, filter, reduce methods for generators

Joshua Landau joshua at landau.ws
Fri Apr 11 00:12:52 CEST 2014


On 10 April 2014 22:12, Jacek Pliszka <jacek.pliszka at gmail.com> wrote:
> I would like to be able to write:
>
> range(100).\
>   filter( f1 ).\
>   map( f2 ).\
>   filter( f3 ).\
>   map( f4 ).\
>   reduce(operator.add)
>
> in addition to current Pythonic way of
>
> reduce( operator.add, f4(x) for x in
>   ( f2(y) for y in range(100) if f1(y))
>  if f3(x) )

The current Pythonic way is

    longname_1 = (f2(shortname) for shortname in range(100) if f1(shortname))
    longname_2 = sum(f4(shortname) for shortname in longname_1 if f3(shortname))

If "shortname" and "longname_i" aren't to taste, find a context and
assign good ones.

Further, find me a plausibly-real-world example where my 2-line
version is *less* readable than the Java one. Then I'll consider it a
fair fight.


More information about the Python-ideas mailing list