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

test&doc: add example and update doc for function package

This commit is contained in:
dudaodong
2023-01-06 17:12:32 +08:00
parent be444f521d
commit ca2a51b37e
8 changed files with 136 additions and 70 deletions

View File

@@ -414,14 +414,32 @@ import "github.com/duke-git/lancet/v2/function"
#### Function list: #### Function list:
- [After](https://github.com/duke-git/lancet/blob/main/docs/function.md#After) - **<big>After</big>** : return a function that invokes passed funcation once the returned function is called more than n times.
- [Before](https://github.com/duke-git/lancet/blob/main/docs/function.md#Before) [[doc](https://github.com/duke-git/lancet/blob/main/docs/function.md#After)]
- [Curry](https://github.com/duke-git/lancet/blob/main/docs/function.md#Curry) [[play](https://go.dev/play/p/eRD5k2vzUVX)]
- [Compose](https://github.com/duke-git/lancet/blob/main/docs/function.md#Compose) - **<big>Before</big>** : return a function that invokes passed funcation once the returned function is called less than n times
- [Debounced](https://github.com/duke-git/lancet/blob/main/docs/function.md#Debounced) [[doc](https://github.com/duke-git/lancet/blob/main/docs/function.md#Before)]
- [Delay](https://github.com/duke-git/lancet/blob/main/docs/function.md#Delay) [[play](https://go.dev/play/p/0HqUDIFZ3IL)]
- [Pipeline](https://github.com/duke-git/lancet/blob/main/docs/function.md#Pipeline) - **<big>CurryFn</big>** : make a curry function.
- [Watcher](https://github.com/duke-git/lancet/blob/main/docs/function.md#Watcher) [[doc](https://github.com/duke-git/lancet/blob/main/docs/function.md#CurryFn)]
- **<big>Compose</big>** : compose the functions from right to left.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/function.md#Compose)]
- **<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)]
- **<big>Debounced</big>** : 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)]
- **<big>Schedule</big>** : 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)]
- **<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/function.md#Pipeline)]
[[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)]
### 11. Maputil package includes some functions to manipulate map. ### 11. Maputil package includes some functions to manipulate map.

View File

@@ -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) - **<big>After</big>** : 创建一个函数当该函数被调用n或更多次之后将执行传入的函数。
- [Before](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Before) [[doc](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#After)]
- [Curry](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Curry) [[play](https://go.dev/play/p/eRD5k2vzUVX)]
- [Compose](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Compose) - **<big>Before</big>** : 创建一个函数当该函数被调用不超过n次时将执行执行传入的函数。
- [Debounced](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Debounced) [[doc](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Before)]
- [Delay](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Delay) [[play](https://go.dev/play/p/0HqUDIFZ3IL)]
- [Pipeline](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Pipeline) - **<big>CurryFn</big>** : 创建柯里化函数。
- [Watcher](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Watcher) [[doc](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#CurryFn)]
- **<big>Compose</big>** : 从右至左组合函数列表fnList返回组合后的函数。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Compose)]
- **<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)]
- **<big>Debounced</big>** : 创建一个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)]
- **<big>Schedule</big>** : 每次持续时间调用函数直到关闭返回的channel。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Schedule)]
[[play](https://go.dev/play/p/hbON-Xeyn5N)]
- **<big>Pipeline</big>** : 从右至左执行函数列表。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Pipeline)]
[[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)]
### 11. maputil 包括一些操作 map 的函数. ### 11. maputil 包括一些操作 map 的函数.

View File

@@ -22,7 +22,7 @@ import (
## Index ## Index
- [After](#After) - [After](#After)
- [Before](#Before) - [Before](#Before)
- [Curry](#Curry) - [CurryFn](#CurryFn)
- [Compose](#Compose) - [Compose](#Compose)
- [Debounced](#Debounced) - [Debounced](#Debounced)
- [Delay](#Delay) - [Delay](#Delay)
@@ -125,15 +125,15 @@ func main() {
### <span id="Curry">Curry</span> ### <span id="CurryFn">CurryFn</span>
<p>Make a curry function.</p> <p>Make curry function.</p>
<b>Signature:</b> <b>Signature:</b>
```go ```go
type Fn func(...any) any type CurryFn[T any] func(...T) T
func (f Fn) Curry(i any) func(...any) any func (cf CurryFn[T]) New(val T) func(...T) T
``` ```
<b>Example:</b> <b>Example:</b>
@@ -149,12 +149,15 @@ func main() {
add := func(a, b int) int { add := func(a, b int) int {
return a + b 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) result := add1(2)
fmt.Println(result) //3
fmt.Println(result) //3
} }
``` ```
@@ -167,7 +170,7 @@ func main() {
<b>Signature:</b> <b>Signature:</b>
```go ```go
func Compose(fnList ...func(...any) any) func(...any) any func Compose[T any](fnList ...func(...T) T) func(...T) T
``` ```
<b>Example:</b> <b>Example:</b>
@@ -180,17 +183,17 @@ import (
) )
func main() { func main() {
add1 := func(v ...any) any { toUpper := func(strs ...string) string {
return v[0].(int) + 1 return strings.ToUpper(strs[0])
} }
add2 := func(v ...any) any { toLower := func(strs ...string) string {
return v[0].(int) + 2 return strings.ToLower(strs[0])
} }
transform := Compose(toUpper, toLower)
add3 := function.Compose(add1, add2) result := transform("aBCde")
result := add3(1)
fmt.Println(result) //4 fmt.Println(result) //ABCDE
} }
``` ```
@@ -259,9 +262,9 @@ import (
func main() { func main() {
var print = func(s string) { 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 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 stopTime int64
excuting bool excuting bool
} }
func NewWatcher() *Watcher
func (w *Watcher) Start() //start the watcher func (w *Watcher) Start() //start the watcher
func (w *Watcher) Stop() //stop the watcher func (w *Watcher) Stop() //stop the watcher
func (w *Watcher) Reset() //reset the watcher func (w *Watcher) Reset() //reset the watcher
@@ -367,7 +371,8 @@ import (
) )
func main() { func main() {
w := &function.Watcher{} w := function.NewWatcher()
w.Start() w.Start()
longRunningTask() longRunningTask()
@@ -377,14 +382,10 @@ func main() {
w.Stop() w.Stop()
eapsedTime := w.GetElapsedTime().Milliseconds() eapsedTime := w.GetElapsedTime().Milliseconds()
fmt.Println(eapsedTime) fmt.Println(eapsedTime)
w.Reset() w.Reset()
fmt.Println(w.excuting) //false
fmt.Println(w.startTime) //0
fmt.Println(w.stopTime) //0
} }
func longRunningTask() { func longRunningTask() {

View File

@@ -22,7 +22,7 @@ import (
## 目录 ## 目录
- [After](#After) - [After](#After)
- [Before](#Before) - [Before](#Before)
- [Curry](#Curry) - [CurryFn](#CurryFn)
- [Compose](#Compose) - [Compose](#Compose)
- [Debounced](#Debounced) - [Debounced](#Debounced)
- [Delay](#Delay) - [Delay](#Delay)
@@ -124,15 +124,15 @@ func main() {
### <span id="Curry">Curry</span> ### <span id="CurryFn">CurryFn</span>
<p>创建一个柯里化函数</p> <p>创建柯里化函数</p>
<b>函数签名:</b> <b>函数签名:</b>
```go ```go
type Fn func(...any) any type CurryFn[T any] func(...T) T
func (f Fn) Curry(i any) func(...any) any func (cf CurryFn[T]) New(val T) func(...T) T
``` ```
<b>例子:</b> <b>例子:</b>
@@ -148,11 +148,14 @@ func main() {
add := func(a, b int) int { add := func(a, b int) int {
return a + b 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) result := add1(2)
fmt.Println(result) //3 fmt.Println(result) //3
} }
``` ```
@@ -161,12 +164,12 @@ func main() {
### <span id="Compose">Compose</span> ### <span id="Compose">Compose</span>
<p>从右至左组合函数列表fnList 返回组合后的函数</p> <p>从右至左组合函数列表fnList返回组合后的函数</p>
<b>函数签名:</b> <b>函数签名:</b>
```go ```go
func Compose(fnList ...func(...any) any) func(...any) any func Compose[T any](fnList ...func(...T) T) func(...T) T
``` ```
<b>例子:</b> <b>例子:</b>
@@ -179,17 +182,17 @@ import (
) )
func main() { func main() {
add1 := func(v ...any) any { toUpper := func(strs ...string) string {
return v[0].(int) + 1 return strings.ToUpper(strs[0])
} }
add2 := func(v ...any) any { toLower := func(strs ...string) string {
return v[0].(int) + 2 return strings.ToLower(strs[0])
} }
transform := Compose(toUpper, toLower)
add3 := function.Compose(add1, add2) result := transform("aBCde")
result := add3(1)
fmt.Println(result) //4 fmt.Println(result) //ABCDE
} }
``` ```
@@ -197,7 +200,7 @@ func main() {
### <span id="Debounced">Debounced</span> ### <span id="Debounced">Debounced</span>
<p>创建一个 debounced 函数,该函数延迟调用 fn 直到自上次调用 debounced 函数后等待持续时间过去。</p> <p>创建一个debounced函数该函数延迟调用fn直到自上次调用debounced函数后等待持续时间过去。</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -333,7 +336,7 @@ func main() {
f := Pipeline(addOne, double, square) f := Pipeline(addOne, double, square)
fmt.Println(f(2)) //36 fmt.Println(fn(2)) //36
} }
``` ```
@@ -341,7 +344,7 @@ func main() {
### <span id="Watcher">Watcher</span> ### <span id="Watcher">Watcher</span>
<p>Watcher 用于记录代码执行时间。可以启动/停止/重置手表定时器。获取函数执行的时间。 </p> <p>Watcher用于记录代码执行时间。可以启动/停止/重置手表定时器。获取函数执行的时间。</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -351,6 +354,7 @@ type Watcher struct {
stopTime int64 stopTime int64
excuting bool excuting bool
} }
func NewWatcher() *Watcher
func (w *Watcher) Start() //start the watcher func (w *Watcher) Start() //start the watcher
func (w *Watcher) Stop() //stop the watcher func (w *Watcher) Stop() //stop the watcher
func (w *Watcher) Reset() //reset the watcher func (w *Watcher) Reset() //reset the watcher
@@ -367,7 +371,8 @@ import (
) )
func main() { func main() {
w := &function.Watcher{} w := function.NewWatcher()
w.Start() w.Start()
longRunningTask() longRunningTask()
@@ -377,14 +382,11 @@ func main() {
w.Stop() w.Stop()
eapsedTime := w.GetElapsedTime().Milliseconds() eapsedTime := w.GetElapsedTime().Milliseconds()
fmt.Println(eapsedTime) fmt.Println(eapsedTime)
w.Reset() w.Reset()
fmt.Println(w.excuting) //false
fmt.Println(w.startTime) //0
fmt.Println(w.stopTime) //0
} }
func longRunningTask() { func longRunningTask() {

View File

@@ -83,7 +83,7 @@ func Delay(delay time.Duration, fn any, args ...any) {
unsafeInvokeFunc(fn, args...) 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 // Play: https://go.dev/play/p/absuEGB_GN7
func Debounced(fn func(), duration time.Duration) func() { func Debounced(fn func(), duration time.Duration) func() {
// Catch programming error while constructing the closure // Catch programming error while constructing the closure

View File

@@ -3,12 +3,18 @@ package function
import "time" import "time"
// Watcher is used for record code excution time // Watcher is used for record code excution time
// Play: Todo
type Watcher struct { type Watcher struct {
startTime int64 startTime int64
stopTime int64 stopTime int64
excuting bool excuting bool
} }
// Start the watch timer.
func NewWatcher() *Watcher {
return &Watcher{}
}
// Start the watch timer. // Start the watch timer.
func (w *Watcher) Start() { func (w *Watcher) Start() {
w.startTime = time.Now().UnixNano() w.startTime = time.Now().UnixNano()

View File

@@ -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
}

View File

@@ -9,7 +9,7 @@ import (
func TestWatcher(t *testing.T) { func TestWatcher(t *testing.T) {
assert := internal.NewAssert(t, "TestWatcher") assert := internal.NewAssert(t, "TestWatcher")
w := &Watcher{} w := NewWatcher()
w.Start() w.Start()
longRunningTask() longRunningTask()