Skip to content

Instantly share code, notes, and snippets.

@suhailshergill
Last active August 29, 2015 14:07
Show Gist options
  • Save suhailshergill/9e33f2bcecdd14ff070d to your computer and use it in GitHub Desktop.
Save suhailshergill/9e33f2bcecdd14ff070d to your computer and use it in GitHub Desktop.
iter_chain.py
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