Skip to content

Commit

Permalink
feat: add predicate
Browse files Browse the repository at this point in the history
  • Loading branch information
Primetalk committed Oct 16, 2022
1 parent e3058bb commit 65d0774
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
18 changes: 18 additions & 0 deletions fun/predicate.go
@@ -0,0 +1,18 @@
package fun

// Predicate is a function with a boolean result type.
type Predicate[A any] func(A) bool

// IsEqualTo compares two arguments for equality.
func IsEqualTo[A comparable](a A) Predicate[A] {
return func(other A) bool {
return a == other
}
}

// Not negates the given predicate.
func Not[A any](p Predicate[A]) Predicate[A] {
return func(a A) bool {
return !p(a)
}
}
13 changes: 13 additions & 0 deletions fun/predicate_test.go
@@ -0,0 +1,13 @@
package fun_test

import (
"testing"

"github.com/primetalk/goio/fun"
"github.com/stretchr/testify/assert"
)

func TestEqualTo(t *testing.T) {
assert.True(t, fun.IsEqualTo(5)(5))
assert.False(t, fun.Not(fun.IsEqualTo(5))(5))
}

0 comments on commit 65d0774

Please sign in to comment.