diff --git a/docs/api/packages/function.md b/docs/api/packages/function.md index cc330d5..5c0c64b 100644 --- a/docs/api/packages/function.md +++ b/docs/api/packages/function.md @@ -7,6 +7,7 @@ function 函数包控制函数执行流程,包含部分函数式编程。 ## 源码: - [https://github.com/duke-git/lancet/blob/main/function/function.go](https://github.com/duke-git/lancet/blob/main/function/function.go) +- [https://github.com/duke-git/lancet/blob/main/function/predicate.go](https://github.com/duke-git/lancet/blob/main/function/predicate.go) - [https://github.com/duke-git/lancet/blob/main/function/watcher.go](https://github.com/duke-git/lancet/blob/main/function/watcher.go)
@@ -32,6 +33,10 @@ import ( - [Schedule](#Schedule) - [Pipeline](#Pipeline) - [Watcher](#Watcher) +- [And](#And) +- [Or](#Or) +- [Negate](#Negate) +- [Nor](#Nor)
@@ -404,3 +409,157 @@ func longRunningTask() { } ``` + +### And + +

返回一个复合谓词判断函数,该判断函数表示一组谓词的逻辑and操作。只有当所有谓词判断函数对于给定的值都返回true时,返回true, 否则返回false。

+ +函数签名: + +```go +func And[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.And( + 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("abcde")) + + // Output: + // true + // false + // false +} +``` + +### Or + +

返回一个复合谓词判断函数,该判断函数表示一组谓词的逻辑or操作。只有当所有谓词判断函数对于给定的值都返回false时,返回false, 否则返回true。

+ +函数签名: + +```go +func Or[T any](predicates ...func(T) bool) func(T) bool +``` + +示例: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/function" +) + +func main() { + containsDigitOrSpecialChar := function.Or( + func(s string) bool { return strings.ContainsAny(s, "0123456789") }, + func(s string) bool { return strings.ContainsAny(s, "!@#$%") }, + ) + + fmt.Println(containsDigitOrSpecialChar("hello!")) + fmt.Println(containsDigitOrSpecialChar("hello")) + + // Output: + // true + // false +} +``` + +### Negate + +

返回一个谓词函数,该谓词函数表示当前谓词的逻辑否定。

+ +函数签名: + +```go +func Negate[T any](predicate func(T) bool) func(T) bool +``` + +示例: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/function" +) + +func main() { + // Define some simple predicates for demonstration + isUpperCase := func(s string) bool { + return strings.ToUpper(s) == s + } + isLowerCase := func(s string) bool { + return strings.ToLower(s) == s + } + isMixedCase := function.Negate(function.Or(isUpperCase, isLowerCase)) + + fmt.Println(isMixedCase("ABC")) + fmt.Println(isMixedCase("AbC")) + + // Output: + // false + // true +} +``` + + +### Nor + +

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

+ +函数签名: + +```go +func Nor[T any](predicates ...func(T) bool) func(T) bool +``` + +示例: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/function" +) + +func main() { + match := function.Nor( + func(s string) bool { return strings.ContainsAny(s, "0123456789") }, + func(s string) bool { return len(s) == 5 }, + ) + + fmt.Println(match("dbcdckkeee")) + + + match = function.Nor( + func(s string) bool { return strings.ContainsAny(s, "0123456789") }, + func(s string) bool { return len(s) == 5 }, + ) + + fmt.Println(match("0123456789")) + + // Output: + // true + // false +} +``` diff --git a/docs/en/api/packages/function.md b/docs/en/api/packages/function.md index c1dd70b..ef2a30c 100644 --- a/docs/en/api/packages/function.md +++ b/docs/en/api/packages/function.md @@ -7,6 +7,7 @@ Package function can control the flow of function execution and support part of ## Source: - [https://github.com/duke-git/lancet/blob/main/function/function.go](https://github.com/duke-git/lancet/blob/main/function/function.go) +- [https://github.com/duke-git/lancet/blob/main/function/predicate.go](https://github.com/duke-git/lancet/blob/main/function/predicate.go) - [https://github.com/duke-git/lancet/blob/main/function/watcher.go](https://github.com/duke-git/lancet/blob/main/function/watcher.go)
@@ -32,6 +33,10 @@ import ( - [Schedule](#Schedule) - [Pipeline](#Pipeline) - [Watcher](#Watcher) +- [And](#And) +- [Or](#Or) +- [Negate](#Negate) +- [Nor](#Nor)
@@ -403,3 +408,158 @@ func longRunningTask() { } ``` + + +### And + +

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

+ +Signature: + +```go +func And[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.And( + 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("abcde")) + + // Output: + // true + // false + // false +} +``` + +### Or + +

Returns a composed predicate that represents the logical OR of a list of predicates. It evaluates to true if at least one of the predicates evaluates to true for the given value.

+ +Signature: + +```go +func Or[T any](predicates ...func(T) bool) func(T) bool +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/function" +) + +func main() { + containsDigitOrSpecialChar := function.Or( + func(s string) bool { return strings.ContainsAny(s, "0123456789") }, + func(s string) bool { return strings.ContainsAny(s, "!@#$%") }, + ) + + fmt.Println(containsDigitOrSpecialChar("hello!")) + fmt.Println(containsDigitOrSpecialChar("hello")) + + // Output: + // true + // false +} +``` + +### Negate + +

Returns a predicate that represents the logical negation of this predicate.

+ +Signature: + +```go +func Negate[T any](predicate func(T) bool) func(T) bool +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/function" +) + +func main() { + // Define some simple predicates for demonstration + isUpperCase := func(s string) bool { + return strings.ToUpper(s) == s + } + isLowerCase := func(s string) bool { + return strings.ToLower(s) == s + } + isMixedCase := function.Negate(function.Or(isUpperCase, isLowerCase)) + + fmt.Println(isMixedCase("ABC")) + fmt.Println(isMixedCase("AbC")) + + // Output: + // false + // true +} +``` + + +### Nor + +

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

+ +Signature: + +```go +func Nor[T any](predicates ...func(T) bool) func(T) bool +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/function" +) + +func main() { + match := function.Nor( + func(s string) bool { return strings.ContainsAny(s, "0123456789") }, + func(s string) bool { return len(s) == 5 }, + ) + + fmt.Println(match("dbcdckkeee")) + + + match = function.Nor( + func(s string) bool { return strings.ContainsAny(s, "0123456789") }, + func(s string) bool { return len(s) == 5 }, + ) + + fmt.Println(match("0123456789")) + + // Output: + // true + // false +} +``` \ No newline at end of file