koto-lang/koto

Add `iterator.flatten`

irh opened this issue · 0 comments

irh commented

It would be useful to have an iterator adaptor similar to Rust's Iterator::flatten.

The idea is to have an iterator adaptor that inspects incoming values, and if the value is iterable then iterate over its contents, otherwise output the value.

e.g.

x = (1, 2, 3), 4, [5, 6, 7, [8, 9]]
x.flatten().to_tuple()
# (1, 2, 3, 4, 5, 6, 7, [8, 9])

The flattening only affects one level of iteration, so in the example above the [8, 9] list is left alone.