Skip to content

Commit

Permalink
chore add Empty and Empty2 iterators
Browse files Browse the repository at this point in the history
Also increase test coverage.

Signed-off-by: Dmitriy Matrenichev <dmitry.matrenichev@siderolabs.com>
  • Loading branch information
DmitriyMV committed Oct 24, 2024
1 parent c53b90b commit d898ee7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
6 changes: 6 additions & 0 deletions xiter/xiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,9 @@ func Fold2[K, V, R any](seq iter.Seq2[K, V], initial R, f func(R, K, V) R) R {

return result
}

// Empty returns an empty iterator.
func Empty[V any](func(V) bool) {}

// Empty2 returns an empty iterator.
func Empty2[V, V2 any](func(V, V2) bool) {}
24 changes: 24 additions & 0 deletions xiter/xiter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package xiter_test

import (
"fmt"
"iter"
"maps"
"slices"
"strconv"
Expand Down Expand Up @@ -82,6 +83,11 @@ func Example_with_numbers() {
slices.Backward(reverseNumbers),
))

fmt.Println("numbers and numbers with pos should be equal:", xiter.Equal2(
slices.All(numbers),
slices.All(numbers),
))

fmt.Println("numbers and reverseNumbers are not equal:", !xiter.Equal(
xiter.Values(slices.All(numbers)),
xiter.Values(slices.All(reverseNumbers)),
Expand Down Expand Up @@ -114,6 +120,7 @@ func Example_with_numbers() {
// Prime number positions:2,3,5,7,
// numbers and rev(reverseNumbers) are equal: true
// numbers and rev(reverseNumbers) with pos are not equal: true
// numbers and numbers with pos should be equal: true
// numbers and reverseNumbers are not equal: true
// numbers and rev(reverseNumbers) are equal: true
// numbers and rev(reverseNumbers) with pos dropped are equal: true
Expand Down Expand Up @@ -196,3 +203,20 @@ func isPrime(n int) bool {

return true
}

func ExampleEmpty() {
var it iter.Seq[int] = xiter.Empty

for v := range it {
fmt.Printf("This %d should not be printed\n", v)
}

var it2 iter.Seq2[int, string] = xiter.Empty2

for v, s := range it2 {
fmt.Printf("This %d %s should not be printed\n", v, s)
}

// Output:
//
}

0 comments on commit d898ee7

Please sign in to comment.