I think it's a great idea that probably won't have much uptake on the list; FWIW in Scala you'd write

// Sum of the squares of all odd numbers up to a hundred
(0 until 100).filter(_ % 2 == 1)
             .map(math.pow(_, 2))
             .reduce(_ + _)

But method chaining isn't really a thing in the python world, and people don't seem to like it as much as I do.


On Thu, Apr 10, 2014 at 2:12 PM, Jacek Pliszka <jacek.pliszka@gmail.com> wrote:
Hi!

Sorry if someone has already talked about it (my simple search did not show any results).

What do you think about adding map, flatmap, filter and reduce methods to generator type ?

I must admit I've seen and I like Java 8 notation and I think it might be more readable than Python way in a few occasions.

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) )

Though longer - Java way seems to be a bit  more readable as the notation follows the data flow sequence.

Do you think it is worth a PEP?

BR,

Jacek

_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/