go-functional icon indicating copy to clipboard operation
go-functional copied to clipboard

[Feature 🔨]: Add a `tee` iterator

Open BooleanCat opened this issue 1 year ago • 0 comments

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

Other languages' iterator tooling provides a tee iterator and it seems reasonable to have one here too.

Describe the solution you'd like

A function that accepts an iterator and returns a pair of iterators, both yielding all members of the original iterator.

Provide code snippets to show how this new feature might be used.

numbers := iter.Take(iter.Count(), 3)
n1, n2 := iter.Tee(numbers)

for i := range n1 {
  fmt.Println(i)
}

for i := range n2 {
  fmt.Println(i)
}

// Output
// 0
// 1
// 2
// 0
// 1
// 2

Does this incur a breaking change?

No

Do you intend to build this feature yourself?

Yes.

Additional context

This is more complicated than it first seems. Here's some thoughts I have:

  • The iterators may be cosumed at different rates meaning the members of between the leading and lagging iterators will need to be remembered
  • Members that have been consumed by both need not be held in memory and can be forgotten.
  • Handle the case where wither iterator is stopped early.
  • I'll accept a naive implementation if we can nail down the API for it and are sure a better implementation won't change how it's used (which can be improved on later)

BooleanCat avatar Jun 30 '24 07:06 BooleanCat