Skip to content

Instantly share code, notes, and snippets.

def fold_flatten(it):
"Flatten any number of nested collections into one list"
def _flattener(acc, a):
try:
return acc + list(fold_flatten(iter(a))) # Recursion
except TypeError: # a is not iterable
return acc + [a]
return fold(_flattener, it, [])
fold_flatten([1, [2, [3, 4], 5]]) # => [1, 2, 3, 4, 5]