Interesting talk on Python vs. Ruby and how he would like Python to have just a bit more syntactic flexibility.

Roald de Vries rdv at roalddevries.nl
Fri Feb 19 10:50:41 EST 2010


> This pipeline idea has actually been implemented further, see <http://
> blog.onideas.ws/stream.py>.
>
> from stream import map, filter, cut
> range(10) >> map(lambda x: [x**2, x**3]) >> filter(lambda t: t[0]!
> =25 and t[1]!=64) >> cut[1] >> list
> [0, 1, 8, 27, 216, 343, 512, 729]

Wow, cool!

Just to show that you can easily add the iterator.map(f).blabla-syntax  
to Python:

     from __future__ import print_function

     class rubified(list):
         map    = lambda self, f: rubified(map(f, self))
         filter = lambda self, f: rubified(filter(f, self))
         reject = lambda self, f: rubified(filter(lambda x: not f(x),  
self))
         # each = lambda self, f: rubified(reduce(lambda x, y:  
print(y), self, None))
         def each(self, f):
             for x in self: f(x)

         def __new__(cls, value):
             return list.__new__(cls, value)

     def print_numbers():
         rubified([1, 2, 3, 4, 5, 6]).map(lambda n:
             [n * n, n * n * n]).reject(lambda (square, cube):
             square == 25 or cube == 64).map(lambda (square, cube):
             cube).each(lambda n:
             print(n))






More information about the Python-list mailing list