Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: slice.Exists, slice.Forall #91

Merged
merged 2 commits into from Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -438,6 +438,8 @@ Some utilities that are convenient when working with slices.
- `slice.FoldLeft[A any, B any](as []A, zero B, f func(B, A)B) (res B)`
- `slice.Filter[A any](as []A, p func(a A) bool) (res []A)`
- `slice.FilterNot[A any](as []A, p func(a A) bool) (res []A)` - same as `Filter`, but inverses the predicate `p`.
- `slice.Exists[A any](p Predicate[A]) Predicate[[]A]` - Exists returns a predicate on slices. The predicate is true if there is an element that satisfy the given element-wise predicate. It's false for an empty slice.
- `slice.Forall[A any](p Predicate[A]) Predicate[[]A]` - Forall returns a predicate on slices. The predicate is true if all elements satisfy the given element-wise predicate. It's true for an empty slice.
- `slice.Collect[A any, B any](as []A, f func(a A) option.Option[B]) (bs []B)` - Collect runs through the slice, executes the given function and only keeps good returned values.
- `slice.Count[A any](as []A, predicate Predicate[A]) (cnt int)` - Count counts the number of elements that satisfy the given predicate.
- `slice.Flatten[A any](ass [][]A)(aas[]A)`
Expand Down
32 changes: 32 additions & 0 deletions slice/slice.go
Expand Up @@ -181,3 +181,35 @@ func Collect[A any, B any](as []A, f func(a A) option.Option[B]) (bs []B) {
}
return
}

// Exists returns a predicate on slices.
// The predicate is true if there is an element that satisfy the given element-wise predicate.
// It's false for an empty slice.
func Exists[A any](p Predicate[A]) Predicate[[]A] {
return func(as []A) (res bool) {
res = false
for _, a := range as {
if p(a) {
res = true
break
}
}
return
}
}

// Forall returns a predicate on slices.
// The predicate is true if all elements satisfy the given element-wise predicate.
// It's true for an empty slice.
func Forall[A any](p Predicate[A]) Predicate[[]A] {
return func(as []A) (res bool) {
res = true
for _, a := range as {
if !p(a) {
res = false
break
}
}
return
}
}
12 changes: 12 additions & 0 deletions slice/slice_test.go
Expand Up @@ -12,14 +12,26 @@ func IsEven(i int) bool {
return i%2 == 0
}

func IsPositive(i int) bool {
return i > 0
}
func IsNegative(i int) bool {
return i < 0
}

var nats10Values = []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

func TestFilter(t *testing.T) {
sumEven := slice.Sum(slice.Filter(nats10Values, IsEven))
assert.Equal(t, 30, sumEven)

sumOdd := slice.Sum(slice.FilterNot(nats10Values, IsEven))

assert.Equal(t, 25, sumOdd)
assert.True(t, slice.Exists(IsEven)(nats10Values))
assert.False(t, slice.Forall(IsEven)(nats10Values))
assert.True(t, slice.Forall(IsPositive)(nats10Values))
assert.False(t, slice.Forall(IsNegative)(nats10Values))
}

func TestFlatten(t *testing.T) {
Expand Down