1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 12:52:28 +08:00
This commit is contained in:
dudaodong
2024-02-21 11:11:52 +08:00
parent a1a4fdc598
commit 8f5d297572
2 changed files with 319 additions and 0 deletions

View File

@@ -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)
<div STYLE="page-break-after: always;"></div>
@@ -32,6 +33,10 @@ import (
- [Schedule](#Schedule)
- [Pipeline](#Pipeline)
- [Watcher](#Watcher)
- [And](#And)
- [Or](#Or)
- [Negate](#Negate)
- [Nor](#Nor)
<div STYLE="page-break-after: always;"></div>
@@ -404,3 +409,157 @@ func longRunningTask() {
}
```
### <span id="And">And</span>
<p>返回一个复合谓词判断函数该判断函数表示一组谓词的逻辑and操作。只有当所有谓词判断函数对于给定的值都返回true时返回true, 否则返回false。</p>
<b>函数签名:</b>
```go
func And[T any](predicates ...func(T) bool) func(T) bool
```
<b>示例:</b>
```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
}
```
### <span id="Or">Or</span>
<p>返回一个复合谓词判断函数该判断函数表示一组谓词的逻辑or操作。只有当所有谓词判断函数对于给定的值都返回false时返回false, 否则返回true。</p>
<b>函数签名:</b>
```go
func Or[T any](predicates ...func(T) bool) func(T) bool
```
<b>示例:</b>
```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
}
```
### <span id="Negate">Negate</span>
<p>返回一个谓词函数,该谓词函数表示当前谓词的逻辑否定。</p>
<b>函数签名:</b>
```go
func Negate[T any](predicate func(T) bool) func(T) bool
```
<b>示例:</b>
```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
}
```
### <span id="Nor">Nor</span>
<p>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.</p>
<b>函数签名:</b>
```go
func Nor[T any](predicates ...func(T) bool) func(T) bool
```
<b>示例:</b>
```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
}
```

View File

@@ -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)
<div STYLE="page-break-after: always;"></div>
@@ -32,6 +33,10 @@ import (
- [Schedule](#Schedule)
- [Pipeline](#Pipeline)
- [Watcher](#Watcher)
- [And](#And)
- [Or](#Or)
- [Negate](#Negate)
- [Nor](#Nor)
<div STYLE="page-break-after: always;"></div>
@@ -403,3 +408,158 @@ func longRunningTask() {
}
```
### <span id="And">And</span>
<p>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.</p>
<b>Signature:</b>
```go
func And[T any](predicates ...func(T) bool) func(T) bool
```
<b>Example:</b>
```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
}
```
### <span id="Or">Or</span>
<p>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.</p>
<b>Signature:</b>
```go
func Or[T any](predicates ...func(T) bool) func(T) bool
```
<b>Example:</b>
```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
}
```
### <span id="Negate">Negate</span>
<p>Returns a predicate that represents the logical negation of this predicate.</p>
<b>Signature:</b>
```go
func Negate[T any](predicate func(T) bool) func(T) bool
```
<b>Example:</b>
```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
}
```
### <span id="Nor">Nor</span>
<p>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.</p>
<b>Signature:</b>
```go
func Nor[T any](predicates ...func(T) bool) func(T) bool
```
<b>Example:</b>
```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
}
```