diff --git a/docs/api/packages/function.md b/docs/api/packages/function.md index 843d006..284f6fc 100644 --- a/docs/api/packages/function.md +++ b/docs/api/packages/function.md @@ -37,6 +37,8 @@ import ( - [Or](#Or) - [Negate](#Negate) - [Nor](#Nor) +- [Xnor](#Xnor) +- [Nand](#Nand)
@@ -563,3 +565,77 @@ func main() { // false } ``` + +### Nand + +

返回一个复合谓词函数,表示给定谓词函数列表的逻辑非与 (NAND)。仅当列表中所有函数对给定参数返回false时,才返回true,否则返回false。

+ +函数签名: + +```go +func Nand[T any](predicates ...func(T) bool) func(T) bool +``` + +示例: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/function" +) + +func main() { + isNumericAndLength5 := function.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 +} +``` + +### Xnor + +

返回一个复合谓词函数,表示给定一组谓词函数的逻辑异或 (XNOR)。只有当所有 谓词函数对给参数都返回true或false时,该谓词函数才返回true。

+ +函数签名: + +```go +func Xnor[T any](predicates ...func(T) bool) func(T) bool +``` + +示例: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/function" +) + +func main() { + isEven := func(i int) bool { return i%2 == 0 } + isPositive := func(i int) bool { return i > 0 } + + match := function.Xnor(isEven, isPositive) + + fmt.Println(match(2)) + fmt.Println(match(-3)) + fmt.Println(match(3)) + + // Output: + // true + // true + // false +} +``` \ No newline at end of file diff --git a/docs/en/api/packages/function.md b/docs/en/api/packages/function.md index ef2a30c..c810b92 100644 --- a/docs/en/api/packages/function.md +++ b/docs/en/api/packages/function.md @@ -37,6 +37,8 @@ import ( - [Or](#Or) - [Negate](#Negate) - [Nor](#Nor) +- [Xnor](#Xnor) +- [Nand](#Nand)
@@ -562,4 +564,78 @@ func main() { // true // false } +``` + +### 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.

+ +Signature: + +```go +func Nand[T any](predicates ...func(T) bool) func(T) bool +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/function" +) + +func main() { + isNumericAndLength5 := function.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 +} +``` + +### Xnor + +

Returns a composed predicate that represents the logical XNOR of a list of predicates. It evaluates to true only if all predicates evaluate to true or false for the given value.

+ +Signature: + +```go +func Xnor[T any](predicates ...func(T) bool) func(T) bool +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/function" +) + +func main() { + isEven := func(i int) bool { return i%2 == 0 } + isPositive := func(i int) bool { return i > 0 } + + match := function.Xnor(isEven, isPositive) + + fmt.Println(match(2)) + fmt.Println(match(-3)) + fmt.Println(match(3)) + + // Output: + // true + // true + // false +} ``` \ No newline at end of file diff --git a/function/predicate.go b/function/predicate.go index dbdd795..6d76d0a 100644 --- a/function/predicate.go +++ b/function/predicate.go @@ -16,7 +16,7 @@ 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. +// 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. func Nand[T any](predicates ...func(T) bool) func(T) bool { if len(predicates) < 2 {