mirror of
https://github.com/duke-git/lancet.git
synced 2025-09-26 19:41:20 +08:00
add playground demo
This commit is contained in:
17
README.md
17
README.md
@@ -113,6 +113,7 @@ import "github.com/duke-git/lancet/v2/algorithm"
|
||||
[[play](https://go.dev/play/p/Anozfr8ZLH3)]
|
||||
- **<big>LinearSearch</big>** : 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)]
|
||||
- **<big>LRUCache</big>** : 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"
|
||||
|
||||
- **<big>NewChannel</big>** : 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)]
|
||||
- **<big>Bridge</big>** : 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)]
|
||||
- **<big>FanIn</big>** : 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)]
|
||||
- **<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)]
|
||||
[[play](https://go.dev/play/p/7aB4KyMMp9A)]
|
||||
- **<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)]
|
||||
[[play](https://go.dev/play/p/Wqz9rwioPww)]
|
||||
- **<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)]
|
||||
[[play](https://go.dev/play/p/lm_GoS6aDjo)]
|
||||
- **<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)]
|
||||
[[play](https://go.dev/play/p/k5N_ALVmYjE)]
|
||||
- **<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)]
|
||||
[[play](https://go.dev/play/p/4J1zAWttP85)]
|
||||
- **<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)]
|
||||
[[play](https://go.dev/play/p/9Utt-1pDr2J)]
|
||||
- **<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)]
|
||||
[[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)]
|
||||
- **<big>CurryFn</big>** : make a curry function.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/function.md#CurryFn)]
|
||||
[[play](https://go.dev/play/p/5HopfDwANKX)]
|
||||
- **<big>Compose</big>** : 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)]
|
||||
- **<big>Delay</big>** : 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)]
|
||||
- **<big>Watcher</big>** : 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)]
|
||||
- **<big>Percent</big>** : 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)]
|
||||
- **<big>RoundToFloat</big>** : 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)]
|
||||
- **<big>Substring</big>** : 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)]
|
||||
- **<big>Wrap</big>** : 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)]
|
||||
|
@@ -112,6 +112,7 @@ import "github.com/duke-git/lancet/v2/algorithm"
|
||||
[[play](https://go.dev/play/p/Anozfr8ZLH3)]
|
||||
- **<big>LinearSearch</big>** : 基于传入的相等函数返回切片中目标值的索引。(线性查找)
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#LinearSearch)]
|
||||
[[play](https://go.dev/play/p/IsS7rgn5s3x)]
|
||||
- **<big>LRUCache</big>** : 应用 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"
|
||||
|
||||
- **<big>NewChannel</big>** : 返回一个 Channel 指针实例。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#NewChannel)]
|
||||
[[play](https://go.dev/play/p/7aB4KyMMp9A)]
|
||||
- **<big>Bridge</big>** : 将多个 channel 链接到一个 channel,直到取消上下文。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/Bridge.md#NewChannel)]
|
||||
[[play](https://go.dev/play/p/qmWSy1NVF-Y)]
|
||||
- **<big>FanIn</big>** : 将多个 channel 合并为一个 channel,直到取消上下文。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#FanIn)]
|
||||
[[play](https://go.dev/play/p/2VYFMexEvTm)]
|
||||
- **<big>Generate</big>** : 根据传入的值,生成 channel。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Generate)]
|
||||
[[play](https://go.dev/play/p/7aB4KyMMp9A)]
|
||||
- **<big>Or</big>** : 将一个或多个 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)]
|
||||
- **<big>OrDone</big>** : 将一个 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)]
|
||||
- **<big>Repeat</big>** : 返回一个 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)]
|
||||
- **<big>RepeatFn</big>** : 返回一个 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)]
|
||||
- **<big>Take</big>** : 返回一个 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)]
|
||||
- **<big>Tee</big>** : 将一个 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)]
|
||||
- **<big>CurryFn</big>** : 创建柯里化函数。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#CurryFn)]
|
||||
[[play](https://go.dev/play/p/5HopfDwANKX)]
|
||||
- **<big>Compose</big>** : 从右至左组合函数列表fnList,返回组合后的函数。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Compose)]
|
||||
[[play](https://go.dev/play/p/KKfugD4PKYF)]
|
||||
- **<big>Delay</big>** : 延迟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)]
|
||||
- **<big>Watcher</big>** : 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)]
|
||||
- **<big>Percent</big>** : 计算百分比,可以指定保留 n 位小数。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Percent)]
|
||||
[[play](https://go.dev/play/p/QQM9B13coSP)]
|
||||
- **<big>RoundToFloat</big>** : 四舍五入,保留 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)]
|
||||
- **<big>Substring</big>** : 根据指定的位置和长度截取子字符串。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Substring)]
|
||||
[[play](https://go.dev/play/p/q3sM6ehnPDp)]
|
||||
- **<big>Wrap</big>** : 用给定字符包裹传入的字符串
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#Wrap)]
|
||||
[[play](https://go.dev/play/p/KoZOlZDDt9y)]
|
||||
|
@@ -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) {
|
||||
|
@@ -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)
|
||||
|
||||
|
@@ -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]
|
||||
|
@@ -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
|
||||
|
@@ -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)
|
||||
|
@@ -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)
|
||||
|
Reference in New Issue
Block a user