Skip to content

Commit

Permalink
Add method to convert Iterator to Seq
Browse files Browse the repository at this point in the history
  • Loading branch information
BooleanCat committed Jul 1, 2024
1 parent 1926f25 commit 8591cce
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
10 changes: 10 additions & 0 deletions it/itx/iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ type (
Iterator2[V, W any] iter.Seq2[V, W]
)

// Seq converts an [Iterator] to an [iter.Seq].
func (iterator Iterator[V]) Seq() iter.Seq[V] {
return iter.Seq[V](iterator)
}

// Seq converts an [Iterator2] to an [iter.Seq2].
func (iterator Iterator2[V, W]) Seq() iter.Seq2[V, W] {
return iter.Seq2[V, W](iterator)
}

// Collect is a convenience method for chaining [slices.Collect] on
// [Iterator]s.
func (iterator Iterator[V]) Collect() []V {
Expand Down
12 changes: 12 additions & 0 deletions it/itx/iter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,22 @@ package itx_test

import (
"fmt"
"maps"
"slices"

"github.com/BooleanCat/go-functional/v2/it/itx"
)

func ExampleIterator_Seq() {
fmt.Println(slices.Collect(itx.Count[int]().Take(3).Seq()))
// Output: [0 1 2]
}

func ExampleIterator2_Seq() {
fmt.Println(maps.Collect(itx.MapsAll(map[int]int{1: 2}).Seq()))
// Output: map[1:2]
}

func ExampleIterator_Collect() {
fmt.Println(itx.SlicesValues([]int{1, 2, 3}).Collect())
// Output: [1 2 3]
Expand Down

0 comments on commit 8591cce

Please sign in to comment.