doc: add go playground demo

This commit is contained in:
dudaodong
2024-03-06 15:28:55 +08:00
parent 1b31014f81
commit a6eaaef563
11 changed files with 127 additions and 26 deletions

View File

@@ -800,20 +800,28 @@ import "github.com/duke-git/lancet/v2/function"
[[play](https://go.dev/play/p/hbON-Xeyn5N)] [[play](https://go.dev/play/p/hbON-Xeyn5N)]
- **<big>Pipeline</big>** : takes a list of functions and returns a function whose param will be passed into the functions one by one. - **<big>Pipeline</big>** : takes a list of functions and returns a function whose param will be passed into the functions one by one.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/function.md#Pipeline)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/function.md#Pipeline)]
[[play](https://go.dev/play/p/mPdUVvj6HD6)]
- **<big>AcceptIf</big>** : returns another function of the same signature as the apply function but also includes a bool value to indicate success or failure. - **<big>AcceptIf</big>** : returns another function of the same signature as the apply function but also includes a bool value to indicate success or failure.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/function.md#AcceptIf)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/function.md#AcceptIf)]
[[play](https://go.dev/play/p/XlXHHtzCf7d)]
- **<big>And</big>** : returns a composed predicate that represents the logical AND of a list of predicates. - **<big>And</big>** : returns a composed predicate that represents the logical AND of a list of predicates.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/function.md#And)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/function.md#And)]
[[play](https://go.dev/play/p/dTBHJMQ0zD2)]
- **<big>Or</big>** : returns a composed predicate that represents the logical OR of a list of predicates. - **<big>Or</big>** : returns a composed predicate that represents the logical OR of a list of predicates.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/function.md#Or)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/function.md#Or)]
[[play](https://go.dev/play/p/LitCIsDFNDA)]
- **<big>Negate</big>** : returns a predicate that represents the logical negation of this predicate. - **<big>Negate</big>** : returns a predicate that represents the logical negation of this predicate.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/function.md#Negate)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/function.md#Negate)]
[[play](https://go.dev/play/p/jbI8BtgFnVE)]
- **<big>Nor</big>** : returns a composed predicate that represents the logical NOR of a list of predicates. - **<big>Nor</big>** : returns a composed predicate that represents the logical NOR of a list of predicates.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/function.md#Nor)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/function.md#Nor)]
[[play](https://go.dev/play/p/2KdCoBEOq84)]
- **<big>Nand</big>** : returns a composed predicate that represents the logical Nand of a list of predicates. - **<big>Nand</big>** : returns a composed predicate that represents the logical Nand of a list of predicates.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/function.md#Nand)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/function.md#Nand)]
[[play](https://go.dev/play/p/Rb-FdNGpgSO)]
- **<big>Xnor</big>** : returns a composed predicate that represents the logical XNOR of a list of predicates. - **<big>Xnor</big>** : returns a composed predicate that represents the logical XNOR of a list of predicates.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/function.md#Xnor)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/function.md#Xnor)]
[[play](https://go.dev/play/p/FJxko8SFbqc)]
- **<big>Watcher</big>** : Watcher is used for record code execution time. can start/stop/reset the watch timer. get the elapsed time of function execution. - **<big>Watcher</big>** : Watcher is used for record code execution time. can start/stop/reset the watch timer. get the elapsed time of function execution.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/function.md#Watcher)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/function.md#Watcher)]
[[play](https://go.dev/play/p/l2yrOpCLd1I)] [[play](https://go.dev/play/p/l2yrOpCLd1I)]
@@ -1423,6 +1431,8 @@ import "github.com/duke-git/lancet/v2/slice"
[[play](https://go.dev/play/p/UzpGQptWppw)] [[play](https://go.dev/play/p/UzpGQptWppw)]
- **<big>SetToDefaultIf</big>** : set elements to their default value if they match the given predicate. - **<big>SetToDefaultIf</big>** : set elements to their default value if they match the given predicate.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/slice.md#SetToDefaultIf)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/slice.md#SetToDefaultIf)]
[[play](https://go.dev/play/p/9AXGlPRC0-A)]
<h3 id="stream"> 19. Stream package implements a sequence of elements supporting sequential and operations. this package is an experiment to explore if stream in go can work as the way java does. its function is very limited. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3> <h3 id="stream"> 19. Stream package implements a sequence of elements supporting sequential and operations. this package is an experiment to explore if stream in go can work as the way java does. its function is very limited. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3>

View File

@@ -804,22 +804,31 @@ import "github.com/duke-git/lancet/v2/function"
[[play](https://go.dev/play/p/mPdUVvj6HD6)] [[play](https://go.dev/play/p/mPdUVvj6HD6)]
- **<big>AcceptIf</big>** : AcceptIf函数会返回另一个函数该函数的签名与apply函数相同但同时还会包含一个布尔值来表示成功或失败。 - **<big>AcceptIf</big>** : AcceptIf函数会返回另一个函数该函数的签名与apply函数相同但同时还会包含一个布尔值来表示成功或失败。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/function.md#AcceptIf)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/function.md#AcceptIf)]
[[play](https://go.dev/play/p/XlXHHtzCf7d)]
- **<big>And</big>** : 返回一个复合谓词判断函数该判断函数表示一组谓词的逻辑and操作。 - **<big>And</big>** : 返回一个复合谓词判断函数该判断函数表示一组谓词的逻辑and操作。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/function.md#And)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/function.md#And)]
[[play](https://go.dev/play/p/dTBHJMQ0zD2)]
- **<big>Or</big>** : 返回一个复合谓词判断函数该判断函数表示一组谓词的逻辑or操作。 - **<big>Or</big>** : 返回一个复合谓词判断函数该判断函数表示一组谓词的逻辑or操作。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/function.md#Or)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/function.md#Or)]
[[play](https://go.dev/play/p/LitCIsDFNDA)]
- **<big>Negate</big>** : 返回一个谓词函数,该谓词函数表示当前谓词的逻辑否定。 - **<big>Negate</big>** : 返回一个谓词函数,该谓词函数表示当前谓词的逻辑否定。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/function.md#Negate)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/function.md#Negate)]
[[play](https://go.dev/play/p/jbI8BtgFnVE)]
- **<big>Nor</big>** : 返回一个复合谓词判断函数该判断函数表示一组谓词的逻辑非或nor的操作。 - **<big>Nor</big>** : 返回一个复合谓词判断函数该判断函数表示一组谓词的逻辑非或nor的操作。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/function.md#Nor)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/function.md#Nor)]
[[play](https://go.dev/play/p/2KdCoBEOq84)]
- **<big>Nand</big>** : 返回一个复合谓词判断函数该判断函数表示一组谓词的逻辑非与nand的操作。 - **<big>Nand</big>** : 返回一个复合谓词判断函数该判断函数表示一组谓词的逻辑非与nand的操作。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/function.md#Nand)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/function.md#Nand)]
[[play](https://go.dev/play/p/Rb-FdNGpgSO)]
- **<big>Xnor</big>** : 返回一个复合谓词判断函数该判断函数表示一组谓词的逻辑异或xnor的操作。 - **<big>Xnor</big>** : 返回一个复合谓词判断函数该判断函数表示一组谓词的逻辑异或xnor的操作。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/function.md#Xnor)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/function.md#Xnor)]
[[play](https://go.dev/play/p/FJxko8SFbqc)]
- **<big>Watcher</big>** : Watcher 用于记录代码执行时间。可以启动/停止/重置手表定时器。获取函数执行的时间。 - **<big>Watcher</big>** : Watcher 用于记录代码执行时间。可以启动/停止/重置手表定时器。获取函数执行的时间。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/function.md#Watcher)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/function.md#Watcher)]
[[play](https://go.dev/play/p/l2yrOpCLd1I)] [[play](https://go.dev/play/p/l2yrOpCLd1I)]
<h3 id="maputil"> 12. maputil 包括一些操作 map 的函数。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3> <h3 id="maputil"> 12. maputil 包括一些操作 map 的函数。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>
```go ```go
@@ -1421,6 +1430,7 @@ import "github.com/duke-git/lancet/v2/slice"
[[play](https://go.dev/play/p/UzpGQptWppw)] [[play](https://go.dev/play/p/UzpGQptWppw)]
- **<big>SetToDefaultIf</big>** : 根据给定给定的predicate判定函数来修改切片中的元素。 - **<big>SetToDefaultIf</big>** : 根据给定给定的predicate判定函数来修改切片中的元素。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/slice.md#SetToDefaultIf)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/slice.md#SetToDefaultIf)]
[[play](https://go.dev/play/p/9AXGlPRC0-A)]

View File

@@ -31,6 +31,7 @@ import (
- [Iterate](#Iterate) - [Iterate](#Iterate)
- [Keys](#Keys) - [Keys](#Keys)
- [Values](#Values) - [Values](#Values)
- [FilterByValue](#FilterByValue)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
@@ -276,7 +277,7 @@ func main() {
### <span id="Values">Values</span> ### <span id="Values">Values</span>
<p>返回hashmap所有值的切片 (随机顺序).</p> <p>返回hashmap所有值的切片 (随机顺序)</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -306,3 +307,40 @@ func main() {
``` ```
### <span id="FilterByValue">FilterByValue</span>
<p>返回一个过滤后的HashMap。 如果任何值与 perdicate 函数不匹配,则返回 nil否则返回包含选定值的 HashMap。</p>
<b>函数签名:</b>
```go
func (hm *HashMap) FilterByValue(perdicate func(value any) bool) *HashMap
```
<b>示例:</b>
```go
package main
import (
"fmt"
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
)
func main() {
hm := hashmap.NewHashMap()
hm.Put("a", 1)
hm.Put("b", 2)
hm.Put("c", 3)
hm.Put("d", 4)
hm.Put("e", 5)
hm.Put("f", 6)
filteredHM := hm.FilterByValue(func(value any) bool {
return value.(int) == 1 || value.(int) == 3
})
fmt.Println(filteredHM.Size()) //2
}
```

View File

@@ -424,7 +424,7 @@ func longRunningTask() {
func And[T any](predicates ...func(T) bool) func(T) bool func And[T any](predicates ...func(T) bool) func(T) bool
``` ```
<b>示例:</b> <b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/dTBHJMQ0zD2)</span></b>
```go ```go
package main package main
@@ -461,7 +461,7 @@ func main() {
func Or[T any](predicates ...func(T) bool) func(T) bool func Or[T any](predicates ...func(T) bool) func(T) bool
``` ```
<b>示例:</b> <b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/LitCIsDFNDA)</span></b>
```go ```go
package main package main
@@ -496,7 +496,7 @@ func main() {
func Negate[T any](predicate func(T) bool) func(T) bool func Negate[T any](predicate func(T) bool) func(T) bool
``` ```
<b>示例:</b> <b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/jbI8BtgFnVE)</span></b>
```go ```go
package main package main
@@ -536,7 +536,7 @@ func main() {
func Nor[T any](predicates ...func(T) bool) func(T) bool func Nor[T any](predicates ...func(T) bool) func(T) bool
``` ```
<b>示例:</b> <b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/2KdCoBEOq84)</span></b>
```go ```go
package main package main
@@ -578,7 +578,7 @@ func main() {
func Nand[T any](predicates ...func(T) bool) func(T) bool func Nand[T any](predicates ...func(T) bool) func(T) bool
``` ```
<b>示例:</b> <b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/Rb-FdNGpgSO)</span></b>
```go ```go
package main package main
@@ -615,7 +615,7 @@ func main() {
func Xnor[T any](predicates ...func(T) bool) func(T) bool func Xnor[T any](predicates ...func(T) bool) func(T) bool
``` ```
<b>示例:</b> <b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/FJxko8SFbqc)</span></b>
```go ```go
package main package main
@@ -652,7 +652,7 @@ func main() {
func AcceptIf[T any](predicate func(T) bool, apply func(T) T) func(T) (T, bool) func AcceptIf[T any](predicate func(T) bool, apply func(T) T) func(T) (T, bool)
``` ```
<b>示例:</b> <b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/XlXHHtzCf7d)</span></b>
```go ```go
package main package main
@@ -663,9 +663,8 @@ import (
) )
func main() { func main() {
adder := function.AcceptIf(
adder := AcceptIf( function.And(
And(
func(x int) bool { func(x int) bool {
return x > 10 return x > 10
}, func(x int) bool { }, func(x int) bool {

View File

@@ -2581,7 +2581,7 @@ func main() {
func SetToDefaultIf[T any](slice []T, predicate func(T) bool) ([]T, int) func SetToDefaultIf[T any](slice []T, predicate func(T) bool) ([]T, int)
``` ```
<b>示例:</b> <b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/9AXGlPRC0-A)</span></b>
```go ```go
import ( import (

View File

@@ -32,6 +32,7 @@ import (
- [Iterate](#Iterate) - [Iterate](#Iterate)
- [Keys](#Keys) - [Keys](#Keys)
- [Values](#Values) - [Values](#Values)
- [FilterByValue](#FilterByValue)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
@@ -311,4 +312,42 @@ func main() {
} }
``` ```
### <span id="FilterByValue">FilterByValue</span>
<p>Returns a filtered HashMap.</p>
<b>Signature:</b>
```go
func (hm *HashMap) FilterByValue(perdicate func(value any) bool) *HashMap
```
<b>Example:</b>
```go
package main
import (
"fmt"
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
)
func main() {
hm := hashmap.NewHashMap()
hm.Put("a", 1)
hm.Put("b", 2)
hm.Put("c", 3)
hm.Put("d", 4)
hm.Put("e", 5)
hm.Put("f", 6)
filteredHM := hm.FilterByValue(func(value any) bool {
return value.(int) == 1 || value.(int) == 3
})
fmt.Println(filteredHM.Size()) //2
}
```

View File

@@ -423,7 +423,7 @@ func longRunningTask() {
func And[T any](predicates ...func(T) bool) func(T) bool func And[T any](predicates ...func(T) bool) func(T) bool
``` ```
<b>Example:</b> <b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/dTBHJMQ0zD2)</span></b>
```go ```go
package main package main
@@ -460,7 +460,7 @@ func main() {
func Or[T any](predicates ...func(T) bool) func(T) bool func Or[T any](predicates ...func(T) bool) func(T) bool
``` ```
<b>Example:</b> <b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/LitCIsDFNDA)</span></b>
```go ```go
package main package main
@@ -495,7 +495,7 @@ func main() {
func Negate[T any](predicate func(T) bool) func(T) bool func Negate[T any](predicate func(T) bool) func(T) bool
``` ```
<b>Example:</b> <b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/jbI8BtgFnVE)</span></b>
```go ```go
package main package main
@@ -535,7 +535,7 @@ func main() {
func Nor[T any](predicates ...func(T) bool) func(T) bool func Nor[T any](predicates ...func(T) bool) func(T) bool
``` ```
<b>Example:</b> <b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/2KdCoBEOq84)</span></b>
```go ```go
package main package main
@@ -577,7 +577,7 @@ func main() {
func Nand[T any](predicates ...func(T) bool) func(T) bool func Nand[T any](predicates ...func(T) bool) func(T) bool
``` ```
<b>Example:</b> <b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/Rb-FdNGpgSO)</span></b>
```go ```go
package main package main
@@ -614,7 +614,7 @@ func main() {
func Xnor[T any](predicates ...func(T) bool) func(T) bool func Xnor[T any](predicates ...func(T) bool) func(T) bool
``` ```
<b>Example:</b> <b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/FJxko8SFbqc)</span></b>
```go ```go
package main package main
@@ -643,9 +643,7 @@ func main() {
### <span id="AcceptIf">AcceptIf</span> ### <span id="AcceptIf">AcceptIf</span>
<p>AcceptIf returns another function of the same signature as the apply function but also includes a bool value to indicate success or failure. <p>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.</p>
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.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -653,7 +651,7 @@ An apply function that also takes an argument of type T and returns a modified v
func AcceptIf[T any](predicate func(T) bool, apply func(T) T) func(T) (T, bool) func AcceptIf[T any](predicate func(T) bool, apply func(T) T) func(T) (T, bool)
``` ```
<b>Example:</b> <b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/XlXHHtzCf7d)</span></b>
```go ```go
package main package main
@@ -665,8 +663,8 @@ import (
func main() { func main() {
adder := AcceptIf( adder := function.AcceptIf(
And( function.And(
func(x int) bool { func(x int) bool {
return x > 10 return x > 10
}, func(x int) bool { }, func(x int) bool {

View File

@@ -2577,7 +2577,7 @@ func main() {
func SetToDefaultIf[T any](slice []T, predicate func(T) bool) ([]T, int) func SetToDefaultIf[T any](slice []T, predicate func(T) bool) ([]T, int)
``` ```
<b>Example:</b> <b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/9AXGlPRC0-A)</span></b>
```go ```go
import ( import (

View File

@@ -139,6 +139,7 @@ func Pipeline[T any](funcs ...func(T) T) func(T) T {
// AcceptIf returns another function of the same signature as the apply function but also includes a bool value to indicate success or failure. // 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. // 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. // An apply function that also takes an argument of type T and returns a modified value of the same type.
// Play: https://go.dev/play/p/XlXHHtzCf7d
func AcceptIf[T any](predicate func(T) bool, apply func(T) T) func(T) (T, bool) { func AcceptIf[T any](predicate func(T) bool, apply func(T) T) func(T) (T, bool) {
if predicate == nil { if predicate == nil {
panic("programming error: predicate must be not nil") panic("programming error: predicate must be not nil")

View File

@@ -2,6 +2,7 @@ package function
// And returns a composed predicate that represents the logical AND of a list of predicates. // 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. // It evaluates to true only if all predicates evaluate to true for the given value.
// Play: https://go.dev/play/p/dTBHJMQ0zD2
func And[T any](predicates ...func(T) bool) func(T) bool { func And[T any](predicates ...func(T) bool) func(T) bool {
if len(predicates) < 2 { if len(predicates) < 2 {
panic("programming error: predicates count must be at least 2") panic("programming error: predicates count must be at least 2")
@@ -18,6 +19,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 true only if all predicates evaluate to false for the given value.
// 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 {
panic("programming error: predicates count must be at least 2") panic("programming error: predicates count must be at least 2")
@@ -33,6 +35,7 @@ func Nand[T any](predicates ...func(T) bool) func(T) bool {
} }
// Negate returns a predicate that represents the logical negation of this predicate. // Negate returns a predicate that represents the logical negation of this predicate.
// Play: https://go.dev/play/p/jbI8BtgFnVE
func Negate[T any](predicate func(T) bool) func(T) bool { func Negate[T any](predicate func(T) bool) func(T) bool {
return func(value T) bool { return func(value T) bool {
return !predicate(value) return !predicate(value)
@@ -41,6 +44,7 @@ func Negate[T any](predicate func(T) bool) func(T) bool {
// Or returns a composed predicate that represents the logical OR of a list of predicates. // 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. // It evaluates to true if at least one of the predicates evaluates to true for the given value.
// Play: https://go.dev/play/p/LitCIsDFNDA
func Or[T any](predicates ...func(T) bool) func(T) bool { func Or[T any](predicates ...func(T) bool) func(T) bool {
if len(predicates) < 2 { if len(predicates) < 2 {
panic("programming error: predicates count must be at least 2") panic("programming error: predicates count must be at least 2")
@@ -57,6 +61,7 @@ func Or[T any](predicates ...func(T) bool) func(T) bool {
// Nor returns a composed predicate that represents the logical NOR of a list of predicates. // 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. // It evaluates to true only if all predicates evaluate to false for the given value.
// Play: https://go.dev/play/p/2KdCoBEOq84
func Nor[T any](predicates ...func(T) bool) func(T) bool { func Nor[T any](predicates ...func(T) bool) func(T) bool {
if len(predicates) < 2 { if len(predicates) < 2 {
panic("programming error: predicates count must be at least 2") panic("programming error: predicates count must be at least 2")
@@ -73,6 +78,7 @@ func Nor[T any](predicates ...func(T) bool) func(T) bool {
// Xnor returns a composed predicate that represents the logical XNOR of a list of predicates. // 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. // It evaluates to true only if all predicates evaluate to true or false for the given value.
// Play: https://go.dev/play/p/FJxko8SFbqc
func Xnor[T any](predicates ...func(T) bool) func(T) bool { func Xnor[T any](predicates ...func(T) bool) func(T) bool {
if len(predicates) < 2 { if len(predicates) < 2 {
panic("programming error: predicates count must be at least 2") panic("programming error: predicates count must be at least 2")

View File

@@ -1173,7 +1173,7 @@ func AppendIfAbsent[T comparable](slice []T, item T) []T {
// SetToDefaultIf sets elements to their default value if they match the given predicate. // SetToDefaultIf sets elements to their default value if they match the given predicate.
// It retains the positions of the elements in the slice. // It retains the positions of the elements in the slice.
// It returns slice of T and the count of modified slice items // It returns slice of T and the count of modified slice items
// Play: todo // Play: https://go.dev/play/p/9AXGlPRC0-A
func SetToDefaultIf[T any](slice []T, predicate func(T) bool) ([]T, int) { func SetToDefaultIf[T any](slice []T, predicate func(T) bool) ([]T, int) {
var count int var count int
for i := 0; i < len(slice); i++ { for i := 0; i < len(slice); i++ {