Skip to content

Commit f3c5a2b

Browse files
committed
chore: add Empty and Empty2 iterators
Also increase test coverage. Signed-off-by: Dmitriy Matrenichev <[email protected]>
1 parent c53b90b commit f3c5a2b

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

Diff for: xiter/xiter.go

+6
Original file line numberDiff line numberDiff line change
@@ -220,3 +220,9 @@ func Fold2[K, V, R any](seq iter.Seq2[K, V], initial R, f func(R, K, V) R) R {
220220

221221
return result
222222
}
223+
224+
// Empty returns an empty iterator.
225+
func Empty[V any](func(V) bool) {}
226+
227+
// Empty2 returns an empty iterator.
228+
func Empty2[V, V2 any](func(V, V2) bool) {}

Diff for: xiter/xiter_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package xiter_test
66

77
import (
88
"fmt"
9+
"iter"
910
"maps"
1011
"slices"
1112
"strconv"
@@ -82,6 +83,11 @@ func Example_with_numbers() {
8283
slices.Backward(reverseNumbers),
8384
))
8485

86+
fmt.Println("numbers and numbers with pos should be equal:", xiter.Equal2(
87+
slices.All(numbers),
88+
slices.All(numbers),
89+
))
90+
8591
fmt.Println("numbers and reverseNumbers are not equal:", !xiter.Equal(
8692
xiter.Values(slices.All(numbers)),
8793
xiter.Values(slices.All(reverseNumbers)),
@@ -114,6 +120,7 @@ func Example_with_numbers() {
114120
// Prime number positions:2,3,5,7,
115121
// numbers and rev(reverseNumbers) are equal: true
116122
// numbers and rev(reverseNumbers) with pos are not equal: true
123+
// numbers and numbers with pos should be equal: true
117124
// numbers and reverseNumbers are not equal: true
118125
// numbers and rev(reverseNumbers) are equal: true
119126
// numbers and rev(reverseNumbers) with pos dropped are equal: true
@@ -196,3 +203,20 @@ func isPrime(n int) bool {
196203

197204
return true
198205
}
206+
207+
func ExampleEmpty() {
208+
var it iter.Seq[int] = xiter.Empty
209+
210+
for v := range it {
211+
fmt.Printf("This %d should not be printed\n", v)
212+
}
213+
214+
var it2 iter.Seq2[int, string] = xiter.Empty2
215+
216+
for v, s := range it2 {
217+
fmt.Printf("This %d %s should not be printed\n", v, s)
218+
}
219+
220+
// Output:
221+
//
222+
}

0 commit comments

Comments
 (0)