[Python-ideas] pipe function for itertools?
Donald 'Paddy' McCarthy
paddy3118 at gmail.com
Tue May 26 12:32:58 CEST 2009
Hi, I have a blog entry, http://paddy3118.blogspot.com/2009/05/pipe-fitting-with-python-generators.html,
in which I define a helper function to get around the problem of
reversion in the nesting of generators and the proliferation of nested
brackets.
See the blog entry for a fuller treatment, but in essence if you have
a series of generators, and want the data to conceptually flow like
this:
gen1 -> gen2 -> gen3 -> gen4 ...
You have to write:
...(gen4(gen3(gen2(gen1()))))...
With pipe, you would write:
pipe(gen1, gen2, gen3, gen4, ...)
Which you could use like this:
for data in pipe(...): do_something_with_data()
If I use dots for indentation, then maybe the definition will come
through to the group:
def pipe(*cmds):
....gen = cmds[0]
....for cmd in cmds[1:]:
....... gen = cmd(gen)
....for x in gen:
....... yield x
A couple of readers thought that it might be a good tool for itertools
to have. There are other solutions out there that use classes to
overload '|', or, but it seems more natural to me to use pipe(), even
though I am a heavy Unix user.
Please discuss.
More information about the Python-ideas
mailing list