Skip to content

Commit

Permalink
doc: add go playground demo
Browse files Browse the repository at this point in the history
  • Loading branch information
duke-git committed Mar 6, 2024
1 parent 1b31014 commit a6eaaef
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 26 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -800,20 +800,28 @@ import "github.com/duke-git/lancet/v2/function"
[[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.
[[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.
[[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.
[[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.
[[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.
[[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.
[[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.
[[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.
[[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.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/function.md#Watcher)]
[[play](https://go.dev/play/p/l2yrOpCLd1I)]
Expand Down Expand Up @@ -1423,6 +1431,8 @@ import "github.com/duke-git/lancet/v2/slice"
[[play](https://go.dev/play/p/UzpGQptWppw)]
- **<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)]
[[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>
Expand Down
10 changes: 10 additions & 0 deletions README_zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -804,22 +804,31 @@ import "github.com/duke-git/lancet/v2/function"
[[play](https://go.dev/play/p/mPdUVvj6HD6)]
- **<big>AcceptIf</big>** : AcceptIf函数会返回另一个函数,该函数的签名与apply函数相同,但同时还会包含一个布尔值来表示成功或失败。
[[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操作。
[[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操作。
[[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>** : 返回一个谓词函数,该谓词函数表示当前谓词的逻辑否定。
[[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的操作。
[[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的操作。
[[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的操作。
[[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 用于记录代码执行时间。可以启动/停止/重置手表定时器。获取函数执行的时间。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/function.md#Watcher)]
[[play](https://go.dev/play/p/l2yrOpCLd1I)]



<h3 id="maputil"> 12. maputil 包括一些操作 map 的函数。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>

```go
Expand Down Expand Up @@ -1421,6 +1430,7 @@ import "github.com/duke-git/lancet/v2/slice"
[[play](https://go.dev/play/p/UzpGQptWppw)]
- **<big>SetToDefaultIf</big>** : 根据给定给定的predicate判定函数来修改切片中的元素。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/slice.md#SetToDefaultIf)]
[[play](https://go.dev/play/p/9AXGlPRC0-A)]



Expand Down
40 changes: 39 additions & 1 deletion docs/api/packages/datastructure/hashmap.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
- [Iterate](#Iterate)
- [Keys](#Keys)
- [Values](#Values)
- [FilterByValue](#FilterByValue)

<div STYLE="page-break-after: always;"></div>

Expand Down Expand Up @@ -276,7 +277,7 @@ func main() {

### <span id="Values">Values</span>

<p>返回hashmap所有值的切片 (随机顺序).</p>
<p>返回hashmap所有值的切片 (随机顺序)</p>

<b>函数签名:</b>

Expand Down Expand Up @@ -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
}
```
19 changes: 9 additions & 10 deletions docs/api/packages/function.md
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ func longRunningTask() {
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
package main
Expand Down Expand Up @@ -461,7 +461,7 @@ func main() {
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
package main
Expand Down Expand Up @@ -496,7 +496,7 @@ func main() {
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
package main
Expand Down Expand Up @@ -536,7 +536,7 @@ func main() {
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
package main
Expand Down Expand Up @@ -578,7 +578,7 @@ func main() {
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
package main
Expand Down Expand Up @@ -615,7 +615,7 @@ func main() {
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
package main
Expand Down Expand Up @@ -652,7 +652,7 @@ func main() {
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
package main
Expand All @@ -663,9 +663,8 @@ import (
)

func main() {

adder := AcceptIf(
And(
adder := function.AcceptIf(
function.And(
func(x int) bool {
return x > 10
}, func(x int) bool {
Expand Down
2 changes: 1 addition & 1 deletion docs/api/packages/slice.md
Original file line number Diff line number Diff line change
Expand Up @@ -2581,7 +2581,7 @@ func main() {
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
import (
Expand Down
39 changes: 39 additions & 0 deletions docs/en/api/packages/datastructure/hashmap.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
- [Iterate](#Iterate)
- [Keys](#Keys)
- [Values](#Values)
- [FilterByValue](#FilterByValue)

<div STYLE="page-break-after: always;"></div>

Expand Down Expand Up @@ -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
}
```


22 changes: 10 additions & 12 deletions docs/en/api/packages/function.md
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ func longRunningTask() {
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
package main
Expand Down Expand Up @@ -460,7 +460,7 @@ func main() {
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
package main
Expand Down Expand Up @@ -495,7 +495,7 @@ func main() {
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
package main
Expand Down Expand Up @@ -535,7 +535,7 @@ func main() {
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
package main
Expand Down Expand Up @@ -577,7 +577,7 @@ func main() {
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
package main
Expand Down Expand Up @@ -614,7 +614,7 @@ func main() {
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
package main
Expand Down Expand Up @@ -643,17 +643,15 @@ func main() {

### <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.
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>
<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>

<b>Signature:</b>

```go
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
package main
Expand All @@ -665,8 +663,8 @@ import (

func main() {

adder := AcceptIf(
And(
adder := function.AcceptIf(
function.And(
func(x int) bool {
return x > 10
}, func(x int) bool {
Expand Down
2 changes: 1 addition & 1 deletion docs/en/api/packages/slice.md
Original file line number Diff line number Diff line change
Expand Up @@ -2577,7 +2577,7 @@ func main() {
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
import (
Expand Down
1 change: 1 addition & 0 deletions function/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
// 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.
// Play: https://go.dev/play/p/XlXHHtzCf7d
func AcceptIf[T any](predicate func(T) bool, apply func(T) T) func(T) (T, bool) {
if predicate == nil {
panic("programming error: predicate must be not nil")
Expand Down
Loading

0 comments on commit a6eaaef

Please sign in to comment.