Skip to content

Commit

Permalink
add All function
Browse files Browse the repository at this point in the history
  • Loading branch information
0x5a17ed committed May 24, 2024
1 parent 29af270 commit 770c53f
Show file tree
Hide file tree
Showing 2 changed files with 27 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 @@ -128,3 +128,9 @@ 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
}

// All tests if all items in the iterator matches a predicate.
func All[T any](it itkit.Iterator[T], fn EachFn[T]) (ok bool) {
Each(it, func(x T) bool { ok = fn(x); return !ok })
return
}
21 changes: 21 additions & 0 deletions itlib/each_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,24 @@ func TestAny(t *testing.T) {
})
}
}

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

0 comments on commit 770c53f

Please sign in to comment.