root/lib/iters.py @ 1781
1 |
# Iterators
|
---|---|
2 |
|
3 |
def flatten(outer_iter): |
4 |
for iter_ in outer_iter: |
5 |
for el in iter_: yield el |
6 |
|
7 |
def flatten_n(outer_iter, depth=2): |
8 |
if depth < 2: return outer_iter |
9 |
elif depth == 2: return flatten(outer_iter) |
10 |
else: return flatten(flatten_n(outer_iter, depth-1)) |