From c85d91004490529c354c3be8784a1a3570b95c16 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Mon, 9 Jan 2023 11:02:41 +0800 Subject: [PATCH] add playground demo --- README.md | 17 +++++++++++++++++ README_zh-CN.md | 16 ++++++++++++++++ algorithm/search.go | 2 +- concurrency/channel.go | 18 +++++++++--------- function/function.go | 4 ++-- function/watcher.go | 2 +- mathutil/mathutil.go | 2 +- strutil/string.go | 2 +- 8 files changed, 48 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index f514b65..414c5ef 100644 --- a/README.md +++ b/README.md @@ -113,6 +113,7 @@ import "github.com/duke-git/lancet/v2/algorithm" [[play](https://go.dev/play/p/Anozfr8ZLH3)] - **LinearSearch** : returns the index of target in slice base on equal function. [[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#LinearSearch)] + [[play](https://go.dev/play/p/IsS7rgn5s3x)] - **LRUCache** : implements memory cache with lru algorithm. [[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#LRUCache)] [[play](https://go.dev/play/p/-EZjgOURufP)] @@ -127,24 +128,34 @@ import "github.com/duke-git/lancet/v2/concurrency" - **NewChannel** : create a Channel pointer instance. [[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#NewChannel)] + [[play](https://go.dev/play/p/7aB4KyMMp9A)] - **Bridge** : link multiply channels into one channel. [[doc](https://github.com/duke-git/lancet/blob/main/docs/Bridge.md#NewChannel)] + [[play](https://go.dev/play/p/qmWSy1NVF-Y)] - **FanIn** : merge multiple channels into one channel. [[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#FanIn)] + [[play](https://go.dev/play/p/2VYFMexEvTm)] - **Generate** : creates a channel, then put values into the channel. [[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Generate)] + [[play](https://go.dev/play/p/7aB4KyMMp9A)] - **Or** : 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)] + [[play](https://go.dev/play/p/Wqz9rwioPww)] - **OrDone** : read a channel into another channel, will close until cancel context. [[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#OrDone)] + [[play](https://go.dev/play/p/lm_GoS6aDjo)] - **Repeat** : 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)] + [[play](https://go.dev/play/p/k5N_ALVmYjE)] - **RepeatFn** : 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)] + [[play](https://go.dev/play/p/4J1zAWttP85)] - **Take** : 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)] + [[play](https://go.dev/play/p/9Utt-1pDr2J)] - **Tee** : split one chanel into two channels, until cancel the context. [[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Tee)] + [[play](https://go.dev/play/p/3TQPKnCirrP)] ### 3. Condition package contains some functions for conditional judgment. eg. And, Or, TernaryOperator... @@ -556,8 +567,10 @@ import "github.com/duke-git/lancet/v2/function" [[play](https://go.dev/play/p/0HqUDIFZ3IL)] - **CurryFn** : make a curry function. [[doc](https://github.com/duke-git/lancet/blob/main/docs/function.md#CurryFn)] + [[play](https://go.dev/play/p/5HopfDwANKX)] - **Compose** : compose the functions from right to left. [[doc](https://github.com/duke-git/lancet/blob/main/docs/function.md#Compose)] + [[play](https://go.dev/play/p/KKfugD4PKYF)] - **Delay** : call the function after delayed time. [[doc](https://github.com/duke-git/lancet/blob/main/docs/function.md#Delay)] [[play](https://go.dev/play/p/Ivtc2ZE-Tye)] @@ -572,6 +585,8 @@ import "github.com/duke-git/lancet/v2/function" [[play](https://go.dev/play/p/mPdUVvj6HD6)] - **Watcher** : Watcher is used for record code excution 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/function.md#Watcher)] + [[play](https://go.dev/play/p/l2yrOpCLd1I)] + ### 11. Maputil package includes some functions to manipulate map. @@ -640,6 +655,7 @@ import "github.com/duke-git/lancet/v2/mathutil" [[play](https://go.dev/play/p/N9qgYg_Ho6f)] - **Percent** : calculate the percentage of value to total. [[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Percent)] + [[play](https://go.dev/play/p/QQM9B13coSP)] - **RoundToFloat** : round up to n decimal places for float64. [[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#RoundToFloat)] [[play](https://go.dev/play/p/ghyb528JRJL)] @@ -1000,6 +1016,7 @@ import "github.com/duke-git/lancet/v2/strutil" [[play](https://go.dev/play/p/Us-ySSbWh-3)] - **Substring** : returns a substring of the specific length starting at the specific offset position. [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Substring)] + [[play](https://go.dev/play/p/q3sM6ehnPDp)] - **Wrap** : wrap a string with given string. [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Wrap)] [[play](https://go.dev/play/p/KoZOlZDDt9y)] diff --git a/README_zh-CN.md b/README_zh-CN.md index a9d4ac1..08cd827 100644 --- a/README_zh-CN.md +++ b/README_zh-CN.md @@ -112,6 +112,7 @@ import "github.com/duke-git/lancet/v2/algorithm" [[play](https://go.dev/play/p/Anozfr8ZLH3)] - **LinearSearch** : 基于传入的相等函数返回切片中目标值的索引。(线性查找) [[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#LinearSearch)] + [[play](https://go.dev/play/p/IsS7rgn5s3x)] - **LRUCache** : 应用 lru 算法实现内存缓存. [[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#LRUCache)] [[play](https://go.dev/play/p/-EZjgOURufP)] @@ -126,24 +127,34 @@ import "github.com/duke-git/lancet/v2/concurrency" - **NewChannel** : 返回一个 Channel 指针实例。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#NewChannel)] + [[play](https://go.dev/play/p/7aB4KyMMp9A)] - **Bridge** : 将多个 channel 链接到一个 channel,直到取消上下文。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/Bridge.md#NewChannel)] + [[play](https://go.dev/play/p/qmWSy1NVF-Y)] - **FanIn** : 将多个 channel 合并为一个 channel,直到取消上下文。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#FanIn)] + [[play](https://go.dev/play/p/2VYFMexEvTm)] - **Generate** : 根据传入的值,生成 channel。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Generate)] + [[play](https://go.dev/play/p/7aB4KyMMp9A)] - **Or** : 将一个或多个 channel 读取到一个 channel 中,当任何读取 channel 关闭时将结束读取。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Or)] + [[play](https://go.dev/play/p/Wqz9rwioPww)] - **OrDone** : 将一个 channel 读入另一个 channel,直到取消上下文。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#OrDone)] + [[play](https://go.dev/play/p/lm_GoS6aDjo)] - **Repeat** : 返回一个 channel,将参数`values`重复放入 channel,直到取消上下文。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Repeat)] + [[play](https://go.dev/play/p/k5N_ALVmYjE)] - **RepeatFn** : 返回一个 channel,重复执行函数 fn,并将结果放入返回的 channel,直到取消上下文。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#RepeatFn)] + [[play](https://go.dev/play/p/4J1zAWttP85)] - **Take** : 返回一个 channel,其值从另一个 channel 获取,直到取消上下文。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Take)] + [[play](https://go.dev/play/p/9Utt-1pDr2J)] - **Tee** : 将一个 channel 分成两个 channel,直到取消上下文。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Tee)] + [[play](https://go.dev/play/p/3TQPKnCirrP)] ### 3. condition 包含一些用于条件判断的函数。 @@ -559,8 +570,10 @@ import "github.com/duke-git/lancet/v2/function" [[play](https://go.dev/play/p/0HqUDIFZ3IL)] - **CurryFn** : 创建柯里化函数。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#CurryFn)] + [[play](https://go.dev/play/p/5HopfDwANKX)] - **Compose** : 从右至左组合函数列表fnList,返回组合后的函数。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Compose)] + [[play](https://go.dev/play/p/KKfugD4PKYF)] - **Delay** : 延迟delay时间后调用函数。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Delay)] [[play](https://go.dev/play/p/Ivtc2ZE-Tye)] @@ -575,6 +588,7 @@ import "github.com/duke-git/lancet/v2/function" [[play](https://go.dev/play/p/mPdUVvj6HD6)] - **Watcher** : Watcher用于记录代码执行时间。可以启动/停止/重置手表定时器。获取函数执行的时间。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Watcher)] + [[play](https://go.dev/play/p/l2yrOpCLd1I)] ### 11. maputil 包括一些操作 map 的函数. @@ -644,6 +658,7 @@ import "github.com/duke-git/lancet/v2/mathutil" [[play](https://go.dev/play/p/N9qgYg_Ho6f)] - **Percent** : 计算百分比,可以指定保留 n 位小数。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Percent)] + [[play](https://go.dev/play/p/QQM9B13coSP)] - **RoundToFloat** : 四舍五入,保留 n 位小数,返回 float64。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#RoundToFloat)] [[play](https://go.dev/play/p/ghyb528JRJL)] @@ -1010,6 +1025,7 @@ import "github.com/duke-git/lancet/v2/strutil" [[play](https://go.dev/play/p/Us-ySSbWh-3)] - **Substring** : 根据指定的位置和长度截取子字符串。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Substring)] + [[play](https://go.dev/play/p/q3sM6ehnPDp)] - **Wrap** : 用给定字符包裹传入的字符串 [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#Wrap)] [[play](https://go.dev/play/p/KoZOlZDDt9y)] diff --git a/algorithm/search.go b/algorithm/search.go index 0d85cad..5243e2c 100644 --- a/algorithm/search.go +++ b/algorithm/search.go @@ -10,7 +10,7 @@ import "github.com/duke-git/lancet/v2/lancetconstraints" // LinearSearch return the index of target in slice base on equal function. // If not found return -1 -// Play: Todo +// Play: https://go.dev/play/p/IsS7rgn5s3x func LinearSearch[T any](slice []T, target T, equal func(a, b T) bool) int { for i, v := range slice { if equal(v, target) { diff --git a/concurrency/channel.go b/concurrency/channel.go index 8629e87..6c75176 100644 --- a/concurrency/channel.go +++ b/concurrency/channel.go @@ -20,7 +20,7 @@ func NewChannel[T any]() *Channel[T] { } // Generate creates channel, then put values into the channel. -// Play: Todo +// Play: https://go.dev/play/p/7aB4KyMMp9A func (c *Channel[T]) Generate(ctx context.Context, values ...T) <-chan T { dataStream := make(chan T) @@ -40,7 +40,7 @@ func (c *Channel[T]) Generate(ctx context.Context, values ...T) <-chan T { } // Repeat create channel, put values into the channel repeatly until cancel the context. -// Play: Todo +// Play: https://go.dev/play/p/k5N_ALVmYjE func (c *Channel[T]) Repeat(ctx context.Context, values ...T) <-chan T { dataStream := make(chan T) @@ -61,7 +61,7 @@ func (c *Channel[T]) Repeat(ctx context.Context, values ...T) <-chan T { // RepeatFn create a channel, excutes fn repeatly, and put the result into the channel // until close context. -// Play: Todo +// Play: https://go.dev/play/p/4J1zAWttP85 func (c *Channel[T]) RepeatFn(ctx context.Context, fn func() T) <-chan T { dataStream := make(chan T) @@ -79,7 +79,7 @@ func (c *Channel[T]) RepeatFn(ctx context.Context, fn func() T) <-chan T { } // Take create a channel whose values are taken from another channel with limit number. -// Play: Todo +// Play: https://go.dev/play/p/9Utt-1pDr2J func (c *Channel[T]) Take(ctx context.Context, valueStream <-chan T, number int) <-chan T { takeStream := make(chan T) @@ -99,7 +99,7 @@ func (c *Channel[T]) Take(ctx context.Context, valueStream <-chan T, number int) } // FanIn merge multiple channels into one channel. -// Play: Todo +// Play: https://go.dev/play/p/2VYFMexEvTm func (c *Channel[T]) FanIn(ctx context.Context, channels ...<-chan T) <-chan T { out := make(chan T) @@ -127,7 +127,7 @@ func (c *Channel[T]) FanIn(ctx context.Context, channels ...<-chan T) <-chan T { } // Tee split one chanel into two channels, until cancel the context. -// Play: Todo +// Play: https://go.dev/play/p/3TQPKnCirrP func (c *Channel[T]) Tee(ctx context.Context, in <-chan T) (<-chan T, <-chan T) { out1 := make(chan T) out2 := make(chan T) @@ -154,7 +154,7 @@ func (c *Channel[T]) Tee(ctx context.Context, in <-chan T) (<-chan T, <-chan T) } // Bridge link multiply channels into one channel. -// Play: Todo +// Play: https://go.dev/play/p/qmWSy1NVF-Y func (c *Channel[T]) Bridge(ctx context.Context, chanStream <-chan <-chan T) <-chan T { valStream := make(chan T) @@ -186,7 +186,7 @@ func (c *Channel[T]) Bridge(ctx context.Context, chanStream <-chan <-chan T) <-c } // Or read one or more channels into one channel, will close when any readin channel is closed. -// Play: Todo +// Play: https://go.dev/play/p/Wqz9rwioPww func (c *Channel[T]) Or(channels ...<-chan T) <-chan T { switch len(channels) { case 0: @@ -220,7 +220,7 @@ func (c *Channel[T]) Or(channels ...<-chan T) <-chan T { } // OrDone read a channel into another channel, will close until cancel context. -// Play: Todo +// Play: https://go.dev/play/p/lm_GoS6aDjo func (c *Channel[T]) OrDone(ctx context.Context, channel <-chan T) <-chan T { valStream := make(chan T) diff --git a/function/function.go b/function/function.go index e453572..4e8bb64 100644 --- a/function/function.go +++ b/function/function.go @@ -47,7 +47,7 @@ func Before(n int, fn any) func(args ...any) []reflect.Value { type CurryFn[T any] func(...T) T // New make a curry function for specific value. -// Play: Todo +// Play: https://go.dev/play/p/5HopfDwANKX func (cf CurryFn[T]) New(val T) func(...T) T { return func(vals ...T) T { args := append([]T{val}, vals...) @@ -56,7 +56,7 @@ func (cf CurryFn[T]) New(val T) func(...T) T { } // Compose compose the functions from right to left. -// Play: Todo +// Play: https://go.dev/play/p/KKfugD4PKYF func Compose[T any](fnList ...func(...T) T) func(...T) T { return func(args ...T) T { firstFn := fnList[0] diff --git a/function/watcher.go b/function/watcher.go index 51fbd3c..6028189 100644 --- a/function/watcher.go +++ b/function/watcher.go @@ -3,7 +3,7 @@ package function import "time" // Watcher is used for record code excution time -// Play: Todo +// Play: https://go.dev/play/p/l2yrOpCLd1I type Watcher struct { startTime int64 stopTime int64 diff --git a/mathutil/mathutil.go b/mathutil/mathutil.go index 4ce167e..954f22c 100644 --- a/mathutil/mathutil.go +++ b/mathutil/mathutil.go @@ -55,7 +55,7 @@ func Factorial(x uint) uint { } // Percent calculate the percentage of value to total. -// Play: Todo +// Play: https://go.dev/play/p/QQM9B13coSP func Percent(val, total float64, n int) float64 { if total == 0 { return float64(0) diff --git a/strutil/string.go b/strutil/string.go index 1dcc45d..b527b09 100644 --- a/strutil/string.go +++ b/strutil/string.go @@ -283,7 +283,7 @@ func SplitEx(s, sep string, removeEmptyString bool) []string { } // Substring returns a substring of the specified length starting at the specified offset position. -// Play: Todo +// Play: https://go.dev/play/p/q3sM6ehnPDp func Substring(s string, offset int, length uint) string { rs := []rune(s) size := len(rs)