BooleanCat / go-functional

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

[Feature 🔨]: Add a `tee` iterator #117

Closed BooleanCat closed 1 month ago

BooleanCat commented 2 months ago

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: