Last active
August 29, 2015 14:07
-
-
Save suhailshergill/9e33f2bcecdd14ff070d to your computer and use it in GitHub Desktop.
iter_chain.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from itertools import imap | |
def foo(x): return iter(x) # function which returns an iter | |
bar = ["bar", "baz"] | |
list(foo("bar")) | |
#[Out]# ['b', 'a', 'r'] | |
baz = iter(bar) # iterator of inputs | |
qu = (item for sublist in imap(foo, baz) for item in sublist) | |
list(qu) # demonstration of chaining things together | |
#[Out]# ['b', 'a', 'r', 'b', 'a', 'z'] | |
# let's define a generic interface for such compositions | |
# bind_gen:: (a -> [b]) -> [a] -> [b], where [] = iter/generator | |
# note the similarity to qu above. bind_gen should be a utility function | |
def bind_gen(f, xs): (y for ys in imap(f, xs) for y in ys) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment