Skip to content

Commit

Permalink
iters One
Browse files Browse the repository at this point in the history
  • Loading branch information
esafonov committed Sep 30, 2024
1 parent 3247360 commit 6264844
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
15 changes: 15 additions & 0 deletions iters/iters.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,3 +434,18 @@ func Group[K comparable, V any](seq iter.Seq2[K, V]) map[K][]V {
func GroupFunc[K comparable, V any](seq iter.Seq[V], key func(V) K) map[K][]V {
return Group(WithKeys(seq, key))
}

// One return a sequence with one element.

func One[V any](v V) iter.Seq[V] {
return func(yield func(V) bool) {
_ = yield(v)
}
}

// One return a sequence with one element.
func One2[K, V any](k K, v V) iter.Seq2[K, V] {
return func(yield func(K, V) bool) {
_ = yield(k, v)
}
}
14 changes: 14 additions & 0 deletions iters/iters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,3 +509,17 @@ func ExampleGroupFunc() {
// Even: [2 4]
// Odd: [1 3 5]
}

func ExampleOne() {
printSeq(One(123456789))

// Output:
// 123456789
}

func ExampleOne2() {
printSeq2(One2(123456789, 987654321))

// Output:
// 123456789 987654321
}

0 comments on commit 6264844

Please sign in to comment.