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

Anh Hai Trinh anh.hai.trinh at gmail.com
Fri Feb 19 11:01:53 CET 2010


On Feb 19, 1:44 pm, Steve Howell <showel... at yahoo.com> wrote:
>
> > def coroutine(co):
> >    def _inner(*args, **kwargs):
> >        gen = co(*args, **kwargs)
> >        gen.next()
> >        return gen
> >    return _inner
>
> > def squares_and_cubes(lst, target):
> >    for n in lst:
> >        target.send((n * n, n * n * n))
>
> > @coroutine
> > def reject_bad_values(target):
> >    while True:
> >        square, cube = (yield)
> >        if not (square == 25 or cube == 64):
> >            target.send((square, cube))
>
> > @coroutine
> > def cubes_only(target):
> >    while True:
> >        square, cube = (yield)
> >        target.send(cube)
>
> > @coroutine
> > def print_results():
> >    while True:
> >        print (yield)
>
> > squares_and_cubes(range(10),
> >        reject_bad_values(
> >            cubes_only(
> >                print_results()
> >                )
> >            )
> >        )
>
> Wow!  It took me a while to get my head around it, but that's pretty
> cool.


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]


--
aht



More information about the Python-list mailing list