mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-04 12:52:28 +08:00
fix(package):[function] Corrected behaviour of Nand predicate (#319)
This commit is contained in:
@@ -620,7 +620,7 @@ func main() {
|
|||||||
|
|
||||||
### <span id="Nand">Nand</span>
|
### <span id="Nand">Nand</span>
|
||||||
|
|
||||||
<p>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.</p>
|
<p>Returns a composed predicate that represents the logical NAND of a list of predicates. It evaluates to false only if all predicates evaluate to true for the given value.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
@@ -650,7 +650,7 @@ func main() {
|
|||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// false
|
// false
|
||||||
// false
|
// true
|
||||||
// true
|
// true
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ func And[T any](predicates ...func(T) bool) func(T) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Nand returns a composed predicate that represents the logical NAND of a list of predicates.
|
// Nand 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.
|
// It evaluates to false only if all predicates evaluate to true for the given value.
|
||||||
// Play: https://go.dev/play/p/Rb-FdNGpgSO
|
// Play: https://go.dev/play/p/Rb-FdNGpgSO
|
||||||
func Nand[T any](predicates ...func(T) bool) func(T) bool {
|
func Nand[T any](predicates ...func(T) bool) func(T) bool {
|
||||||
if len(predicates) < 2 {
|
if len(predicates) < 2 {
|
||||||
@@ -26,11 +26,11 @@ func Nand[T any](predicates ...func(T) bool) func(T) bool {
|
|||||||
}
|
}
|
||||||
return func(value T) bool {
|
return func(value T) bool {
|
||||||
for _, predicate := range predicates {
|
for _, predicate := range predicates {
|
||||||
if predicate(value) {
|
if !predicate(value) {
|
||||||
return false // Short-circuit on the first true predicate
|
return true // Short-circuit on the first false predicate
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true // True if all predicates are false
|
return false // False if all predicates are true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ func ExampleNand() {
|
|||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// false
|
// false
|
||||||
// false
|
// true
|
||||||
// true
|
// true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ func TestPredicatesNandPure(t *testing.T) {
|
|||||||
)
|
)
|
||||||
|
|
||||||
assert.ShouldBeFalse(isNumericAndLength5("12345"))
|
assert.ShouldBeFalse(isNumericAndLength5("12345"))
|
||||||
assert.ShouldBeFalse(isNumericAndLength5("1234"))
|
assert.ShouldBeTrue(isNumericAndLength5("1234"))
|
||||||
assert.ShouldBeTrue(isNumericAndLength5("abcdef"))
|
assert.ShouldBeTrue(isNumericAndLength5("abcdef"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user