mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-06 13:42:28 +08:00
test&doc: add example and update doc for function package
This commit is contained in:
@@ -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() {
|
||||
|
||||
|
||||
|
||||
### <span id="Curry">Curry</span>
|
||||
### <span id="CurryFn">CurryFn</span>
|
||||
|
||||
<p>Make a curry function.</p>
|
||||
<p>Make curry function.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
|
||||
```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
|
||||
```
|
||||
<b>Example:</b>
|
||||
|
||||
@@ -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() {
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func Compose(fnList ...func(...any) any) func(...any) any
|
||||
func Compose[T any](fnList ...func(...T) T) func(...T) T
|
||||
```
|
||||
<b>Example:</b>
|
||||
|
||||
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user