Skip to content

Commit 4588015

Browse files
committed
feat(All): check that all stream items satisfy condition
1 parent f60542d commit 4588015

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

all.go

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package fungi
2+
3+
// All returns true if and only if all stream items satisfy given condition.
4+
func All[T any](pass func(T) bool) StreamReducer[T, bool] {
5+
return func(item Stream[T]) (ok bool, err error) {
6+
fail, err := Any(func(item T) bool { return !pass(item) })(item)
7+
ok = !fail
8+
return
9+
}
10+
}

all_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package fungi
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestAllTrue(t *testing.T) {
10+
source := SliceStream([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
11+
justOddNumbers := Filter(odd)
12+
allNumbersAreOdd := All(odd)
13+
result, err := allNumbersAreOdd(justOddNumbers(source))
14+
assert.NoError(t, err)
15+
assert.True(t, result)
16+
}
17+
18+
func TestAllFalse(t *testing.T) {
19+
source := SliceStream([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
20+
allNumbersAreEven := All(even)
21+
result, err := allNumbersAreEven(source)
22+
assert.NoError(t, err)
23+
assert.False(t, result)
24+
}

0 commit comments

Comments
 (0)