mirror of
https://github.com/duke-git/lancet.git
synced 2025-10-05 23:46:54 +08:00
Function: AcceptIf (#198)
AcceptIf returns another function of the same signature as the apply function but also includes a bool value to indicate success or failure. A predicate function that takes an argument of type T and returns a bool. An apply function that also takes an argument of type T and returns a modified value of the same type.
This commit is contained in:
@@ -145,3 +145,32 @@ func ExamplePipeline() {
|
||||
// Output:
|
||||
// 36
|
||||
}
|
||||
|
||||
func ExampleAcceptIf() {
|
||||
|
||||
adder := AcceptIf(
|
||||
And(
|
||||
func(x int) bool {
|
||||
return x > 10
|
||||
}, func(x int) bool {
|
||||
return x%2 == 0
|
||||
}),
|
||||
func(x int) int {
|
||||
return x + 1
|
||||
},
|
||||
)
|
||||
|
||||
result, ok := adder(20)
|
||||
fmt.Println(result)
|
||||
fmt.Println(ok)
|
||||
|
||||
result, ok = adder(21)
|
||||
fmt.Println(result)
|
||||
fmt.Println(ok)
|
||||
|
||||
// Output:
|
||||
// 21
|
||||
// true
|
||||
// 0
|
||||
// false
|
||||
}
|
||||
|
Reference in New Issue
Block a user