public
Last active

Equivalent implementation in Python of a Haskell code:

  • Download Gist
gistfile1.pyw
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#!/usr/bin/python
#
# See http://stackoverflow.com/questions/1016997/generate-from-generators
#
# Equivalent implementation in Python of this Haskell code:
#
# grandKids generation kidsFunc val =
# iterate (concatMap kidsFunc) [val] !! generation
 
from itertools import chain, islice, imap
from functools import partial
 
# Generic functions
 
def flatten(it):
"Flatten one level of nesting"
return chain.from_iterable(it)
 
def concatMap(func, it):
"""Map a function over a list and concatenate the results."""
return flatten(imap(func, it))
 
def iterate(func, x):
"""Yield repeated applications of f to x: x, f(x), f(f(x)), ..."""
while 1:
yield x
x = func(x)
 
def takeN(it, n):
"""Take the n-th element in iterator."""
return islice(it, n, None).next()
 
###
 
def kids(x): # children indices in a 1-based binary heap
yield x*2
yield x*2 + 1
 
def grandKids(generation, kidsFunc, val):
return takeN(iterate(partial(concatMap, kidsFunc), [val]), generation)
 
print list(grandKids(3, kids, 2))
# [16, 17, 18, 19, 20, 21, 22, 23]

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.