mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-16 18:52:27 +08:00
Merge branch 'main' into v2
This commit is contained in:
@@ -16,6 +16,22 @@ func And[T any](predicates ...func(T) bool) func(T) bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// And returns a composed predicate that represents the logical NAND of a list of predicates.
|
||||||
|
// It evaluates to true only if all predicates evaluate to false for the given value.
|
||||||
|
func Nand[T any](predicates ...func(T) bool) func(T) bool {
|
||||||
|
if len(predicates) < 2 {
|
||||||
|
panic("programming error: predicates count must be at least 2")
|
||||||
|
}
|
||||||
|
return func(value T) bool {
|
||||||
|
for _, predicate := range predicates {
|
||||||
|
if predicate(value) {
|
||||||
|
return false // Short-circuit on the first true predicate
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true // True if all predicates are false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Negate returns a predicate that represents the logical negation of this predicate.
|
// Negate returns a predicate that represents the logical negation of this predicate.
|
||||||
func Negate[T any](predicate func(T) bool) func(T) bool {
|
func Negate[T any](predicate func(T) bool) func(T) bool {
|
||||||
return func(value T) bool {
|
return func(value T) bool {
|
||||||
|
|||||||
@@ -53,6 +53,22 @@ func ExampleAnd() {
|
|||||||
// false
|
// false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExampleNand() {
|
||||||
|
isNumericAndLength5 := Nand(
|
||||||
|
func(s string) bool { return strings.ContainsAny(s, "0123456789") },
|
||||||
|
func(s string) bool { return len(s) == 5 },
|
||||||
|
)
|
||||||
|
|
||||||
|
fmt.Println(isNumericAndLength5("12345"))
|
||||||
|
fmt.Println(isNumericAndLength5("1234"))
|
||||||
|
fmt.Println(isNumericAndLength5("abcdef"))
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
// true
|
||||||
|
}
|
||||||
|
|
||||||
func ExampleNor() {
|
func ExampleNor() {
|
||||||
match := Nor(
|
match := Nor(
|
||||||
func(s string) bool { return strings.ContainsAny(s, "0123456789") },
|
func(s string) bool { return strings.ContainsAny(s, "0123456789") },
|
||||||
|
|||||||
@@ -54,6 +54,21 @@ func TestPredicatesAndPure(t *testing.T) {
|
|||||||
assert.ShouldBeFalse(isNumericAndLength5("abcde"))
|
assert.ShouldBeFalse(isNumericAndLength5("abcde"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPredicatesNandPure(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
assert := internal.NewAssert(t, "TestPredicatesNandPure")
|
||||||
|
|
||||||
|
isNumericAndLength5 := Nand(
|
||||||
|
func(s string) bool { return strings.ContainsAny(s, "0123456789") },
|
||||||
|
func(s string) bool { return len(s) == 5 },
|
||||||
|
)
|
||||||
|
|
||||||
|
assert.ShouldBeFalse(isNumericAndLength5("12345"))
|
||||||
|
assert.ShouldBeFalse(isNumericAndLength5("1234"))
|
||||||
|
assert.ShouldBeTrue(isNumericAndLength5("abcdef"))
|
||||||
|
}
|
||||||
|
|
||||||
func TestPredicatesNorPure(t *testing.T) {
|
func TestPredicatesNorPure(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
@@ -108,9 +123,12 @@ func TestPredicatesMix(t *testing.T) {
|
|||||||
|
|
||||||
assert.ShouldBeFalse(c("hello!"))
|
assert.ShouldBeFalse(c("hello!"))
|
||||||
|
|
||||||
c = Nor(a, b)
|
k := Nor(a, b)
|
||||||
assert.ShouldBeFalse(c("hello!"))
|
assert.ShouldBeFalse(k("hello!"))
|
||||||
|
|
||||||
c = Xnor(a, b)
|
o := Xnor(a, b)
|
||||||
assert.ShouldBeTrue(c("hello!"))
|
assert.ShouldBeTrue(o("hello!"))
|
||||||
|
|
||||||
|
p := Nand(c, k)
|
||||||
|
assert.ShouldBeTrue(p("hello!"))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user