Skip to content

Commit

Permalink
add Any function
Browse files Browse the repository at this point in the history
  • Loading branch information
0x5a17ed committed May 24, 2024
1 parent c398ec1 commit 29af270
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
6 changes: 6 additions & 0 deletions itlib/each.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,9 @@ func Drop[T any](n uint, it itkit.Iterator[T]) itkit.Iterator[T] {
}
return it
}

// Any tests if any item in the iterator matches a predicate.
func Any[T any](it itkit.Iterator[T], fn EachFn[T]) (ok bool) {
Each(it, func(x T) bool { ok = fn(x); return ok })
return
}
20 changes: 20 additions & 0 deletions itlib/each_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,23 @@ func TestDrop(t *testing.T) {
assert.Equal(it.Value(), 4)
})
}

func TestAny(t *testing.T) {
type testCase[T any] struct {
name string
args []bool
wantOk bool
}
tt := []testCase[bool]{
{"false", []bool{false, false, false}, false},
{"true", []bool{false, false, true}, true},
}
for _, tt := range tt {
t.Run(tt.name, func(t *testing.T) {
got := itlib.Any(sliceit.In(tt.args), func(item bool) bool {
return item
})
assertpkg.Equal(t, tt.wantOk, got)
})
}
}

0 comments on commit 29af270

Please sign in to comment.