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

Compare commits

...

2 Commits

Author SHA1 Message Date
dudaodong
ca2a51b37e test&doc: add example and update doc for function package 2023-01-06 17:12:32 +08:00
dudaodong
be444f521d test: add examples for function package 2023-01-06 16:12:44 +08:00
11 changed files with 346 additions and 134 deletions

View File

@@ -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)
- **<big>After</big>** : 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)]
- **<big>Before</big>** : 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)]
- **<big>CurryFn</big>** : make a curry function.
[[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.

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)
- [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)
- **<big>After</big>** : 创建一个函数当该函数被调用n或更多次之后将执行传入的函数。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#After)]
[[play](https://go.dev/play/p/eRD5k2vzUVX)]
- **<big>Before</big>** : 创建一个函数当该函数被调用不超过n次时将执行执行传入的函数。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Before)]
[[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)]
- **<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 的函数.

View File

@@ -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() {

View File

@@ -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() {
### <span id="Curry">Curry</span>
### <span id="CurryFn">CurryFn</span>
<p>创建一个柯里化函数</p>
<p>创建柯里化函数</p>
<b>函数签名:</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>例子:</b>
@@ -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() {
### <span id="Compose">Compose</span>
<p>从右至左组合函数列表fnList 返回组合后的函数</p>
<p>从右至左组合函数列表fnList返回组合后的函数</p>
<b>函数签名:</b>
```go
func Compose(fnList ...func(...any) any) func(...any) any
func Compose[T any](fnList ...func(...T) T) func(...T) T
```
<b>例子:</b>
@@ -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() {
### <span id="Debounced">Debounced</span>
<p>创建一个 debounced 函数,该函数延迟调用 fn 直到自上次调用 debounced 函数后等待持续时间过去。</p>
<p>创建一个debounced函数该函数延迟调用fn直到自上次调用debounced函数后等待持续时间过去。</p>
<b>函数签名:</b>
@@ -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() {
### <span id="Watcher">Watcher</span>
<p>Watcher 用于记录代码执行时间。可以启动/停止/重置手表定时器。获取函数执行的时间。 </p>
<p>Watcher用于记录代码执行时间。可以启动/停止/重置手表定时器。获取函数执行的时间。</p>
<b>函数签名:</b>
@@ -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() {

View File

@@ -5,11 +5,13 @@
package function
import (
"fmt"
"reflect"
"time"
)
// After creates a function that invokes func once it's called n or more times
// After creates a function that invokes func once it's called n or more times.
// Play: https://go.dev/play/p/8mQhkFmsgqs
func After(n int, fn any) func(args ...any) []reflect.Value {
// Catch programming error while constructing the closure
mustBeFunction(fn)
@@ -23,7 +25,8 @@ func After(n int, fn any) func(args ...any) []reflect.Value {
}
}
// Before creates a function that invokes func once it's called less than n times
// Before creates a function that invokes func once it's called less than n times.
// Play: https://go.dev/play/p/0HqUDIFZ3IL
func Before(n int, fn any) func(args ...any) []reflect.Value {
// Catch programming error while constructing the closure
mustBeFunction(fn)
@@ -40,41 +43,48 @@ func Before(n int, fn any) func(args ...any) []reflect.Value {
}
}
// Fn is for curry function which is func(...any) any
type Fn func(...any) any
// CurryFn is for make curry function
type CurryFn[T any] func(...T) T
// Curry make a curry function
func (f Fn) Curry(i any) func(...any) any {
return func(values ...any) any {
v := append([]any{i}, values...)
return f(v...)
// New make a curry function for specific value.
// Play: Todo
func (cf CurryFn[T]) New(val T) func(...T) T {
return func(vals ...T) T {
args := append([]T{val}, vals...)
return cf(args...)
}
}
// Compose compose the functions from right to left
func Compose(fnList ...func(...any) any) func(...any) any {
return func(s ...any) any {
f := fnList[0]
restFn := fnList[1:]
// Compose compose the functions from right to left.
// Play: Todo
func Compose[T any](fnList ...func(...T) T) func(...T) T {
return func(args ...T) T {
firstFn := fnList[0]
restFns := fnList[1:]
if len(fnList) == 1 {
return f(s...)
return firstFn(args...)
}
return f(Compose(restFn...)(s...))
fn := Compose(restFns...)
arg := fn(args...)
return firstFn(arg)
}
}
// Delay make the function execution after delayed time
// Delay make the function execution after delayed time.
// Play: https://go.dev/play/p/Ivtc2ZE-Tye
func Delay(delay time.Duration, fn any, args ...any) {
// Catch programming error while constructing the closure
mustBeFunction(fn)
time.Sleep(delay)
invokeFunc(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.
// Play: https://go.dev/play/p/absuEGB_GN7
func Debounced(fn func(), duration time.Duration) func() {
// Catch programming error while constructing the closure
mustBeFunction(fn)
@@ -92,7 +102,8 @@ func Debounced(fn func(), duration time.Duration) func() {
return func() { timer.Reset(duration) }
}
// Schedule invoke function every duration time, util close the returned bool chan
// Schedule invoke function every duration time, util close the returned bool channel.
// Play: https://go.dev/play/p/hbON-Xeyn5N
func Schedule(d time.Duration, fn any, args ...any) chan bool {
// Catch programming error while constructing the closure
mustBeFunction(fn)
@@ -114,6 +125,7 @@ func Schedule(d time.Duration, fn any, args ...any) chan bool {
// Pipeline takes a list of functions and returns a function whose param will be passed into
// the functions one by one.
// Play: https://go.dev/play/p/mPdUVvj6HD6
func Pipeline[T any](funcs ...func(T) T) func(T) T {
return func(arg T) (result T) {
result = arg
@@ -123,3 +135,27 @@ func Pipeline[T any](funcs ...func(T) T) func(T) T {
return
}
}
func unsafeInvokeFunc(fn any, args ...any) []reflect.Value {
fv := reflect.ValueOf(fn)
params := make([]reflect.Value, len(args))
for i, item := range args {
params[i] = reflect.ValueOf(item)
}
return fv.Call(params)
}
func functionValue(function any) reflect.Value {
v := reflect.ValueOf(function)
if v.Kind() != reflect.Func {
panic(fmt.Sprintf("Invalid function type, value of type %T", function))
}
return v
}
func mustBeFunction(function any) {
v := reflect.ValueOf(function)
if v.Kind() != reflect.Func {
panic(fmt.Sprintf("Invalid function type, value of type %T", function))
}
}

View File

@@ -0,0 +1,147 @@
package function
import (
"fmt"
"strings"
"time"
)
func ExampleAfter() {
fn := After(2, func() {
fmt.Println("test")
})
fn()
fn()
// Output:
// test
}
func ExampleBefore() {
fn := Before(2, func() {
fmt.Println("test")
})
fn()
fn()
fn()
fn()
// Output:
// test
// test
}
func ExampleCurryFn_New() {
add := func(a, b int) int {
return a + b
}
var addCurry CurryFn[int] = func(values ...int) int {
return add(values[0], values[1])
}
add1 := addCurry.New(1)
result := add1(2)
fmt.Println(result)
// Output:
// 3
}
func ExampleCompose() {
toUpper := func(strs ...string) string {
return strings.ToUpper(strs[0])
}
toLower := func(strs ...string) string {
return strings.ToLower(strs[0])
}
transform := Compose(toUpper, toLower)
result := transform("aBCde")
fmt.Println(result)
// Output:
// ABCDE
}
func ExampleDelay() {
var print = func(s string) {
fmt.Println(s)
}
Delay(2*time.Second, print, "hello")
// Output:
// hello
}
func ExampleDebounced() {
count := 0
add := func() {
count++
}
debouncedAdd := Debounced(add, 50*time.Microsecond)
debouncedAdd()
debouncedAdd()
debouncedAdd()
debouncedAdd()
time.Sleep(100 * time.Millisecond)
fmt.Println(count)
debouncedAdd()
time.Sleep(100 * time.Millisecond)
fmt.Println(count)
// Output:
// 1
// 2
}
func ExampleSchedule() {
var result []string
appendFn := func(s string) {
result = append(result, s)
}
stop := Schedule(1*time.Second, appendFn, "*")
time.Sleep(3 * time.Second)
close(stop)
fmt.Println(result)
// Output:
// [* * *]
}
func ExamplePipeline() {
addOne := func(x int) int {
return x + 1
}
double := func(x int) int {
return 2 * x
}
square := func(x int) int {
return x * x
}
fn := Pipeline(addOne, double, square)
result := fn(2)
fmt.Println(result)
// Output:
// 36
}

View File

@@ -1,39 +0,0 @@
package function
import (
"fmt"
"reflect"
)
func invokeFunc(fn any, args ...any) []reflect.Value {
fv := functionValue(fn)
params := make([]reflect.Value, len(args))
for i, item := range args {
params[i] = reflect.ValueOf(item)
}
return fv.Call(params)
}
func unsafeInvokeFunc(fn any, args ...any) []reflect.Value {
fv := reflect.ValueOf(fn)
params := make([]reflect.Value, len(args))
for i, item := range args {
params[i] = reflect.ValueOf(item)
}
return fv.Call(params)
}
func functionValue(function any) reflect.Value {
v := reflect.ValueOf(function)
if v.Kind() != reflect.Func {
panic(fmt.Sprintf("Invalid function type, value of type %T", function))
}
return v
}
func mustBeFunction(function any) {
v := reflect.ValueOf(function)
if v.Kind() != reflect.Func {
panic(fmt.Sprintf("Invalid function type, value of type %T", function))
}
}

View File

@@ -62,24 +62,26 @@ func TestCurry(t *testing.T) {
add := func(a, b int) int {
return a + b
}
var addCurry 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)
assert.Equal(3, add1(2))
}
func TestCompose(t *testing.T) {
assert := internal.NewAssert(t, "TestCompose")
toUpper := func(a ...any) any {
return strings.ToUpper(a[0].(string))
toUpper := func(strs ...string) string {
return strings.ToUpper(strs[0])
}
toLower := func(a ...any) any {
return strings.ToLower(a[0].(string))
toLower := func(strs ...string) string {
return strings.ToLower(strs[0])
}
expected := toUpper(toLower("aBCde"))
cf := Compose(toUpper, toLower)
res := cf("aBCde")

View File

@@ -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()

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) {
assert := internal.NewAssert(t, "TestWatcher")
w := &Watcher{}
w := NewWatcher()
w.Start()
longRunningTask()