1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-03-01 00:35:28 +08:00

doc: add example and update docment for channel

This commit is contained in:
dudaodong
2022-12-31 13:35:44 +08:00
parent cc54dd7ec9
commit a6ba1028c5
5 changed files with 389 additions and 290 deletions
+22 -11
View File
@@ -73,7 +73,6 @@ func main() {
## Documentation
### 1. Algorithm package implements some basic algorithm. eg. sort, search.
```go
@@ -81,6 +80,7 @@ import "github.com/duke-git/lancet/v2/algorithm"
```
#### Function list:
- **<big>BubbleSort</big>** : sorts slice with bubble sort algorithm, will change the original slice.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#BubbleSort)]
[[play](https://go.dev/play/p/GNdv7Jg2Taj)]
@@ -125,16 +125,26 @@ import "github.com/duke-git/lancet/v2/concurrency"
#### Function list:
- [NewChannel](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#NewChannel)
- [Bridge](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Bridge)
- [FanIn](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#FanIn)
- [Generate](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Generate)
- [Or](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Or)
- [OrDone](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#OrDone)
- [Repeat](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Repeat)
- [RepeatFn](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#RepeatFn)
- [Take](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Take)
- [Tee](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Tee)
- **<big>NewChannel</big>** : create a Channel pointer instance.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#NewChannel)]
- **<big>Bridge</big>** : link multiply channels into one channel.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/Bridge.md#NewChannel)]
- **<big>FanIn</big>** : merge multiple channels into one channel.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#FanIn)]
- **<big>Generate</big>** : creates a channel, then put values into the channel.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Generate)]
- **<big>Or</big>** : read one or more channels into one channel, will close when any readin channel is closed.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Or)]
- **<big>OrDone</big>** : read a channel into another channel, will close until cancel context.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#OrDone)]
- **<big>Repeat</big>** : create channel, put values into the channel repeatly until cancel the context.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Repeat)]
- **<big>RepeatFn</big>** : create a channel, excutes fn repeatly, and put the result into the channel, until close context.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#RepeatFn)]
- **<big>Take</big>** : create a channel whose values are taken from another channel with limit number.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Take)]
- **<big>Tee</big>** : split one chanel into two channels, until cancel the context.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Tee)]
### 3. Condition package contains some functions for conditional judgment. eg. And, Or, TernaryOperator...
@@ -566,6 +576,7 @@ import "github.com/duke-git/lancet/v2/system"
```
#### Function list:
- **<big>IsWindows</big>** : check if current os is windows.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/system.md#IsWindows)]
[[play](https://go.dev/play/p/XzJULbzmf9m)]
+23 -10
View File
@@ -79,6 +79,7 @@ import "github.com/duke-git/lancet/v2/algorithm"
```
#### Function list:
- **<big>BubbleSort</big>** : 使用冒泡排序算法对切片进行排序。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#BubbleSort)]
[[play](https://go.dev/play/p/GNdv7Jg2Taj)]
@@ -123,16 +124,26 @@ import "github.com/duke-git/lancet/v2/concurrency"
#### Function list:
- [NewChannel](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#NewChannel)
- [Bridge](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Bridge)
- [FanIn](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#FanIn)
- [Generate](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Generate)
- [Or](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Or)
- [OrDone](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#OrDone)
- [Repeat](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Repeat)
- [RepeatFn](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#RepeatFn)
- [Take](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Take)
- [Tee](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Tee)
- **<big>NewChannel</big>** : 返回一个 Channel 指针实例。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#NewChannel)]
- **<big>Bridge</big>** : 将多个 channel 链接到一个 channel,直到取消上下文。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/Bridge.md#NewChannel)]
- **<big>FanIn</big>** : 将多个 channel 合并为一个 channel,直到取消上下文。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#FanIn)]
- **<big>Generate</big>** : 根据传入的值,生成channel。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Generate)]
- **<big>Or</big>** : 将一个或多个channel读取到一个channel中,当任何读取channel关闭时将结束读取。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Or)]
- **<big>OrDone</big>** : 将一个channel读入另一个channel,直到取消上下文。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#OrDone)]
- **<big>Repeat</big>** : 返回一个channel,将参数`values`重复放入channel,直到取消上下文。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Repeat)]
- **<big>RepeatFn</big>** : 返回一个channel,重复执行函数fn,并将结果放入返回的channel,直到取消上下文。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#RepeatFn)]
- **<big>Take</big>** : 返回一个channel,其值从另一个channel获取,直到取消上下文。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Take)]
- **<big>Tee</big>** : 将一个channel分成两个channel,直到取消上下文。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Tee)]
### 3. condition 包含一些用于条件判断的函数。
@@ -495,6 +506,7 @@ import "github.com/duke-git/lancet/v2/strutil"
```
#### 函数列表:
- **<big>After</big>** : 返回源字符串中指定字符串首次出现时的位置之后的子字符串。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#After)]
[[play](https://go.dev/play/p/RbCOQqCDA7m)]
@@ -562,6 +574,7 @@ import "github.com/duke-git/lancet/v2/system"
```
#### 函数列表:
- **<big>IsWindows</big>** : 检查当前操作系统是否是 windows。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN#IsWindows)]
[[play](https://go.dev/play/p/XzJULbzmf9m)]
+6 -6
View File
@@ -19,7 +19,7 @@ func NewChannel[T any]() *Channel[T] {
return &Channel[T]{}
}
// Generate a data of type any channel, put param values into the channel.
// Generate creates channel, then put values into the channel.
// Play:
func (c *Channel[T]) Generate(ctx context.Context, values ...T) <-chan T {
dataStream := make(chan T)
@@ -39,7 +39,7 @@ func (c *Channel[T]) Generate(ctx context.Context, values ...T) <-chan T {
return dataStream
}
// Repeat return a data of type any channel, put param `values` into the channel repeatly until cancel the context.
// Repeat create channel, put values into the channel repeatly until cancel the context.
// Play:
func (c *Channel[T]) Repeat(ctx context.Context, values ...T) <-chan T {
dataStream := make(chan T)
@@ -59,8 +59,8 @@ func (c *Channel[T]) Repeat(ctx context.Context, values ...T) <-chan T {
return dataStream
}
// RepeatFn return a channel, excutes fn repeatly, and put the result into retruned channel
// until close the `done` channel.
// RepeatFn create a channel, excutes fn repeatly, and put the result into the channel
// until close context.
// Play:
func (c *Channel[T]) RepeatFn(ctx context.Context, fn func() T) <-chan T {
dataStream := make(chan T)
@@ -78,7 +78,7 @@ func (c *Channel[T]) RepeatFn(ctx context.Context, fn func() T) <-chan T {
return dataStream
}
// Take return a channel whose values are taken from another channel.
// Take create a channel whose values are taken from another channel with limit number.
// Play:
func (c *Channel[T]) Take(ctx context.Context, valueStream <-chan T, number int) <-chan T {
takeStream := make(chan T)
@@ -126,7 +126,7 @@ func (c *Channel[T]) FanIn(ctx context.Context, channels ...<-chan T) <-chan T {
return out
}
// Tee split one chanel into two channels.
// Tee split one chanel into two channels, until cancel the context.
// Play:
func (c *Channel[T]) Tee(ctx context.Context, in <-chan T) (<-chan T, <-chan T) {
out1 := make(chan T)
+41 -4
View File
@@ -159,7 +159,7 @@ func main() {
### <span id="Repeat">Repeat</span>
<p>Return a channel, put param `values` into the channel repeatly until cancel the context.</p>
<p>Create channel, put values into the channel repeatly until cancel the context.</p>
<b>Signature:</b>
@@ -197,10 +197,47 @@ func main() {
### <span id="Generate">Generate</span>
<p>Creates a channel, then put values into the channel.</p>
<b>Signature:</b>
```go
func (c *Channel[T]) Generate(ctx context.Context, values ...T) <-chan T
```
<b>Example:</b>
```go
package main
import (
"context"
"fmt"
"github.com/duke-git/lancet/v2/concurrency"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c := concurrency.NewChannel[int]()
intStream := c.Generate(ctx, 1, 2, 3)
fmt.Println(<-intStream)
fmt.Println(<-intStream)
fmt.Println(<-intStream)
// Output:
// 1
// 2
// 3
}
```
### <span id="RepeatFn">RepeatFn</span>
<p>Return a channel, excutes fn repeatly, and put the result into retruned channel until cancel context.</p>
<p>Create a channel, excutes fn repeatly, and put the result into the channel, until close context.</p>
<b>Signature:</b>
@@ -329,7 +366,7 @@ func main() {
### <span id="Take">Take</span>
<p>Return a channel whose values are tahken from another channel until cancel context.</p>
<p>Create a channel whose values are taken from another channel with limit number.</p>
<b>Signature:</b>
@@ -376,7 +413,7 @@ func main() {
### <span id="Tee">Tee</span>
<p>Split one channel into two channels until cancel context.</p>
<p>Split one chanel into two channels, until cancel the context.</p>
<b>Signature:</b>
+39 -1
View File
@@ -157,6 +157,44 @@ func main() {
```
### <span id="Generate">Generate</span>
<p>根据传入的值,生成channel.</p>
<b>函数签名:</b>
```go
func (c *Channel[T]) Generate(ctx context.Context, values ...T) <-chan T
```
<b>例子:</b>
```go
package main
import (
"context"
"fmt"
"github.com/duke-git/lancet/v2/concurrency"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c := concurrency.NewChannel[int]()
intStream := c.Generate(ctx, 1, 2, 3)
fmt.Println(<-intStream)
fmt.Println(<-intStream)
fmt.Println(<-intStream)
// Output:
// 1
// 2
// 3
}
```
### <span id="Repeat">Repeat</span>
<p>返回一个channel,将参数`values`重复放入channel,直到取消上下文。</p>
@@ -289,7 +327,7 @@ func main() {
### <span id="OrDone">OrDone</span>
<p>将一个channel读入另一个channel,直到取消上下文。.</p>
<p>将一个channel读入另一个channel,直到取消上下文。</p>
<b>函数签名:</b>