BooleanCat / go-functional

go-functional is a library of iterators to augment the standard library
MIT License
407 stars 23 forks source link

[Feature 🔨]: `Tee` behaviour for iterators #88

Closed BooleanCat closed 3 months ago

BooleanCat commented 9 months ago

Is your feature request related to a problem? Please describe.

It's possible users would like to use an iterator more than once. Be using tee as inspiration we can have that behaviour in go-functional.

Describe the solution you'd like

evens := iter.Count().Filter(isEven).Take(5)

iters := iter.Tee(evens)

iters.One.Take(2).Collect()  // [0 2]
iters.Two.Take(2).Collect()  // [0 2]

Does this incur a breaking change?

No.

Do you intend to build this feature yourself?

If no one else picks it up.

Additional context

Since the iterators that are generated by tee may be consumed at different rates, the tee struct will need to remember a portion of the results from underlying iterator (the lag between the faster and slower iterators). A naive solution would just remember the entire underlying iterator, a better solution would use something like a double ended queue.

Thread safety is out of scope unless you think it should be in scope and can covince me.

BooleanCat commented 3 months ago

This needs to be re-written for the v2 API.