From ca2a51b37ecc7dca9bc7096bd607e07a03262def Mon Sep 17 00:00:00 2001 From: dudaodong Date: Fri, 6 Jan 2023 17:12:32 +0800 Subject: [PATCH] test&doc: add example and update doc for function package --- README.md | 34 +++++++++++++++----- README_zh-CN.md | 33 ++++++++++++++----- docs/function.md | 55 ++++++++++++++++---------------- docs/function_zh-CN.md | 52 +++++++++++++++--------------- function/function.go | 2 +- function/watcher.go | 6 ++++ function/watcher_example_test.go | 22 +++++++++++++ function/watcher_test.go | 2 +- 8 files changed, 136 insertions(+), 70 deletions(-) create mode 100644 function/watcher_example_test.go diff --git a/README.md b/README.md index 39a70e3..61d31c4 100644 --- a/README.md +++ b/README.md @@ -414,14 +414,32 @@ import "github.com/duke-git/lancet/v2/function" #### Function list: -- [After](https://github.com/duke-git/lancet/blob/main/docs/function.md#After) -- [Before](https://github.com/duke-git/lancet/blob/main/docs/function.md#Before) -- [Curry](https://github.com/duke-git/lancet/blob/main/docs/function.md#Curry) -- [Compose](https://github.com/duke-git/lancet/blob/main/docs/function.md#Compose) -- [Debounced](https://github.com/duke-git/lancet/blob/main/docs/function.md#Debounced) -- [Delay](https://github.com/duke-git/lancet/blob/main/docs/function.md#Delay) -- [Pipeline](https://github.com/duke-git/lancet/blob/main/docs/function.md#Pipeline) -- [Watcher](https://github.com/duke-git/lancet/blob/main/docs/function.md#Watcher) +- **After** : return a function that invokes passed funcation once the returned function is called more than n times. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/function.md#After)] + [[play](https://go.dev/play/p/eRD5k2vzUVX)] +- **Before** : return a function that invokes passed funcation once the returned function is called less than n times + [[doc](https://github.com/duke-git/lancet/blob/main/docs/function.md#Before)] + [[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)] +- **Compose** : compose the functions from right to left. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/function.md#Compose)] +- **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)] +- **Debounced** : creates a debounced function that delays invoking fn until after wait duration have elapsed since the last time the debounced function was invoked. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/function.md#Debounced)] + [[play](https://go.dev/play/p/absuEGB_GN7)] +- **Schedule** : invoke function every duration time, util close the returned bool channel. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/function.md#Schedule)] + [[play](https://go.dev/play/p/hbON-Xeyn5N)] +- **Pipeline** : 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/function.md#Pipeline)] + [[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)] + + ### 11. Maputil package includes some functions to manipulate map. diff --git a/README_zh-CN.md b/README_zh-CN.md index 4511b6e..9096dcb 100644 --- a/README_zh-CN.md +++ b/README_zh-CN.md @@ -413,14 +413,31 @@ import "github.com/duke-git/lancet/v2/function" #### 函数列表: -- [After](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#After) -- [Before](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Before) -- [Curry](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Curry) -- [Compose](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Compose) -- [Debounced](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Debounced) -- [Delay](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Delay) -- [Pipeline](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Pipeline) -- [Watcher](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Watcher) +- **After** : 创建一个函数,当该函数被调用n或更多次之后将执行传入的函数。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#After)] + [[play](https://go.dev/play/p/eRD5k2vzUVX)] +- **Before** : 创建一个函数,当该函数被调用不超过n次时,将执行执行传入的函数。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Before)] + [[play](https://go.dev/play/p/0HqUDIFZ3IL)] +- **CurryFn** : 创建柯里化函数。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#CurryFn)] +- **Compose** : 从右至左组合函数列表fnList,返回组合后的函数。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Compose)] +- **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)] +- **Debounced** : 创建一个debounced函数,该函数延迟调用fn直到自上次调用debounced函数后等待持续时间过去。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Debounced)] + [[play](https://go.dev/play/p/absuEGB_GN7)] +- **Schedule** : 每次持续时间调用函数,直到关闭返回的channel。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Schedule)] + [[play](https://go.dev/play/p/hbON-Xeyn5N)] +- **Pipeline** : 从右至左执行函数列表。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Pipeline)] + [[play](https://go.dev/play/p/mPdUVvj6HD6)] +- **Watcher** : Watcher用于记录代码执行时间。可以启动/停止/重置手表定时器。获取函数执行的时间。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Watcher)] + ### 11. maputil 包括一些操作 map 的函数. diff --git a/docs/function.md b/docs/function.md index d9eb820..4abb9e9 100644 --- a/docs/function.md +++ b/docs/function.md @@ -22,7 +22,7 @@ import ( ## Index - [After](#After) - [Before](#Before) -- [Curry](#Curry) +- [CurryFn](#CurryFn) - [Compose](#Compose) - [Debounced](#Debounced) - [Delay](#Delay) @@ -125,15 +125,15 @@ func main() { -### Curry +### CurryFn -

Make a curry function.

+

Make curry function.

Signature: ```go -type Fn func(...any) any -func (f Fn) Curry(i any) func(...any) any +type CurryFn[T any] func(...T) T +func (cf CurryFn[T]) New(val T) func(...T) T ``` Example: @@ -149,12 +149,15 @@ func main() { add := func(a, b int) int { return a + b } - var addCurry function.Fn = func(values ...any) any { - return add(values[0].(int), values[1].(int)) + + var addCurry CurryFn[int] = func(values ...int) int { + return add(values[0], values[1]) } - add1 := addCurry.Curry(1) + add1 := addCurry.New(1) + result := add1(2) - fmt.Println(result) //3 + + fmt.Println(result) //3 } ``` @@ -167,7 +170,7 @@ func main() { Signature: ```go -func Compose(fnList ...func(...any) any) func(...any) any +func Compose[T any](fnList ...func(...T) T) func(...T) T ``` Example: @@ -180,17 +183,17 @@ import ( ) func main() { - add1 := func(v ...any) any { - return v[0].(int) + 1 + toUpper := func(strs ...string) string { + return strings.ToUpper(strs[0]) } - add2 := func(v ...any) any { - return v[0].(int) + 2 + toLower := func(strs ...string) string { + return strings.ToLower(strs[0]) } + transform := Compose(toUpper, toLower) - add3 := function.Compose(add1, add2) - result := add3(1) + result := transform("aBCde") - fmt.Println(result) //4 + fmt.Println(result) //ABCDE } ``` @@ -259,9 +262,9 @@ import ( func main() { var print = func(s string) { - fmt.Println(count) //test delay + fmt.Println(count) //delay 2 seconds } - function.Delay(2*time.Second, print, "test delay") + function.Delay(2*time.Second, print, "delay 2 seconds") } ``` @@ -332,9 +335,9 @@ func main() { return x * x } - f := Pipeline(addOne, double, square) + fn := Pipeline(addOne, double, square) - fmt.Println(f(2)) //36 + fmt.Println(fn(2)) //36 } ``` @@ -351,6 +354,7 @@ type Watcher struct { stopTime int64 excuting bool } +func NewWatcher() *Watcher func (w *Watcher) Start() //start the watcher func (w *Watcher) Stop() //stop the watcher func (w *Watcher) Reset() //reset the watcher @@ -367,7 +371,8 @@ import ( ) func main() { - w := &function.Watcher{} + w := function.NewWatcher() + w.Start() longRunningTask() @@ -377,14 +382,10 @@ func main() { w.Stop() eapsedTime := w.GetElapsedTime().Milliseconds() + fmt.Println(eapsedTime) w.Reset() - - fmt.Println(w.excuting) //false - - fmt.Println(w.startTime) //0 - fmt.Println(w.stopTime) //0 } func longRunningTask() { diff --git a/docs/function_zh-CN.md b/docs/function_zh-CN.md index 59b082c..ab88076 100644 --- a/docs/function_zh-CN.md +++ b/docs/function_zh-CN.md @@ -22,7 +22,7 @@ import ( ## 目录 - [After](#After) - [Before](#Before) -- [Curry](#Curry) +- [CurryFn](#CurryFn) - [Compose](#Compose) - [Debounced](#Debounced) - [Delay](#Delay) @@ -124,15 +124,15 @@ func main() { -### Curry +### CurryFn -

创建一个柯里化的函数

+

创建柯里化函数

函数签名: ```go -type Fn func(...any) any -func (f Fn) Curry(i any) func(...any) any +type CurryFn[T any] func(...T) T +func (cf CurryFn[T]) New(val T) func(...T) T ``` 例子: @@ -148,11 +148,14 @@ func main() { add := func(a, b int) int { return a + b } - var addCurry function.Fn = func(values ...any) any { - return add(values[0].(int), values[1].(int)) + + var addCurry CurryFn[int] = func(values ...int) int { + return add(values[0], values[1]) } - add1 := addCurry.Curry(1) + add1 := addCurry.New(1) + result := add1(2) + fmt.Println(result) //3 } ``` @@ -161,12 +164,12 @@ func main() { ### Compose -

从右至左组合函数列表fnList, 返回组合后的函数

+

从右至左组合函数列表fnList,返回组合后的函数

函数签名: ```go -func Compose(fnList ...func(...any) any) func(...any) any +func Compose[T any](fnList ...func(...T) T) func(...T) T ``` 例子: @@ -179,17 +182,17 @@ import ( ) func main() { - add1 := func(v ...any) any { - return v[0].(int) + 1 + toUpper := func(strs ...string) string { + return strings.ToUpper(strs[0]) } - add2 := func(v ...any) any { - return v[0].(int) + 2 + toLower := func(strs ...string) string { + return strings.ToLower(strs[0]) } + transform := Compose(toUpper, toLower) - add3 := function.Compose(add1, add2) - result := add3(1) + result := transform("aBCde") - fmt.Println(result) //4 + fmt.Println(result) //ABCDE } ``` @@ -197,7 +200,7 @@ func main() { ### Debounced -

创建一个 debounced 函数,该函数延迟调用 fn 直到自上次调用 debounced 函数后等待持续时间过去。

+

创建一个debounced函数,该函数延迟调用fn直到自上次调用debounced函数后等待持续时间过去。

函数签名: @@ -333,7 +336,7 @@ func main() { f := Pipeline(addOne, double, square) - fmt.Println(f(2)) //36 + fmt.Println(fn(2)) //36 } ``` @@ -341,7 +344,7 @@ func main() { ### Watcher -

Watcher 用于记录代码执行时间。可以启动/停止/重置手表定时器。获取函数执行的时间。

+

Watcher用于记录代码执行时间。可以启动/停止/重置手表定时器。获取函数执行的时间。

函数签名: @@ -351,6 +354,7 @@ type Watcher struct { stopTime int64 excuting bool } +func NewWatcher() *Watcher func (w *Watcher) Start() //start the watcher func (w *Watcher) Stop() //stop the watcher func (w *Watcher) Reset() //reset the watcher @@ -367,7 +371,8 @@ import ( ) func main() { - w := &function.Watcher{} + w := function.NewWatcher() + w.Start() longRunningTask() @@ -377,14 +382,11 @@ func main() { w.Stop() eapsedTime := w.GetElapsedTime().Milliseconds() + fmt.Println(eapsedTime) w.Reset() - fmt.Println(w.excuting) //false - - fmt.Println(w.startTime) //0 - fmt.Println(w.stopTime) //0 } func longRunningTask() { diff --git a/function/function.go b/function/function.go index d57fb6c..e453572 100644 --- a/function/function.go +++ b/function/function.go @@ -83,7 +83,7 @@ func Delay(delay time.Duration, fn any, args ...any) { unsafeInvokeFunc(fn, args...) } -// Debounced creates a debounced function that delays invoking fn until after wait duration have elapsed since the last time the debounced function was invoked.. +// Debounced creates a debounced function that delays invoking fn until after wait duration have elapsed since the last time the debounced function was invoked. // Play: https://go.dev/play/p/absuEGB_GN7 func Debounced(fn func(), duration time.Duration) func() { // Catch programming error while constructing the closure diff --git a/function/watcher.go b/function/watcher.go index a484acb..51fbd3c 100644 --- a/function/watcher.go +++ b/function/watcher.go @@ -3,12 +3,18 @@ package function import "time" // Watcher is used for record code excution time +// Play: Todo type Watcher struct { startTime int64 stopTime int64 excuting bool } +// Start the watch timer. +func NewWatcher() *Watcher { + return &Watcher{} +} + // Start the watch timer. func (w *Watcher) Start() { w.startTime = time.Now().UnixNano() diff --git a/function/watcher_example_test.go b/function/watcher_example_test.go new file mode 100644 index 0000000..9da542f --- /dev/null +++ b/function/watcher_example_test.go @@ -0,0 +1,22 @@ +package function + +import "fmt" + +func ExampleWatcher() { + w := NewWatcher() + + w.Start() + + longRunningTask() + + w.Stop() + + // eapsedTime := w.GetElapsedTime().Milliseconds() + + fmt.Println("foo") + + w.Reset() + + // Output: + // foo +} diff --git a/function/watcher_test.go b/function/watcher_test.go index 788f055..d278bd2 100644 --- a/function/watcher_test.go +++ b/function/watcher_test.go @@ -9,7 +9,7 @@ import ( func TestWatcher(t *testing.T) { assert := internal.NewAssert(t, "TestWatcher") - w := &Watcher{} + w := NewWatcher() w.Start() longRunningTask()