From f6cd98086ff0bded68ff512ae8296db15777fa3c Mon Sep 17 00:00:00 2001 From: dudaodong Date: Thu, 23 Feb 2023 10:52:07 +0800 Subject: [PATCH] doc: add doc for Pipeline function --- docs/function.md | 45 +++++++++++++++++++++++++++++++++++++++ docs/function_zh-CN.md | 44 ++++++++++++++++++++++++++++++++++++++ function/function.go | 12 +++++++++++ function/function_test.go | 18 ++++++++++++++++ 4 files changed, 119 insertions(+) diff --git a/docs/function.md b/docs/function.md index 8308f29..d295b51 100644 --- a/docs/function.md +++ b/docs/function.md @@ -29,6 +29,8 @@ import ( - [Compose](#Compose) - [Debounced](#Debounced) - [Delay](#Delay) +- [Pipeline](#Pipeline) +- [Schedule](#Schedule) - [Watcher](#Watcher)
@@ -298,6 +300,49 @@ func main() { } ``` +### Pipeline + +

Pipeline takes a list of functions and returns a function whose param will be passed into +the functions one by one.

+ +Signature: + +```go +func Pipeline(funcs ...func(interface{}) interface{}) func(interface{}) interface{} +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/function" +) + +func main() { + addOne := func(x interface{}) interface{} { + return x.(int) + 1 + } + double := func(x interface{}) interface{} { + return 2 * x.(int) + } + square := func(x interface{}) interface{} { + return x.(int) * x.(int) + } + + f := function.Pipeline(addOne, double, square) + + result := fn(2) + + fmt.Println(result) + + // Output: + // 36 +} +``` + ### Watcher

Watcher is used for record code excution time. can start/stop/reset the watch timer. get the elapsed time of function execution.

diff --git a/docs/function_zh-CN.md b/docs/function_zh-CN.md index 7761567..caad855 100644 --- a/docs/function_zh-CN.md +++ b/docs/function_zh-CN.md @@ -29,6 +29,8 @@ import ( - [Compose](#Compose) - [Debounced](#Debounced) - [Delay](#Delay) +- [Pipeline](#Pipeline) +- [Schedule](#Schedule) - [Watcher](#Watcher)
@@ -298,6 +300,48 @@ func main() { } ``` +### Pipeline + +

管道执行多个函数

+ +函数签名: + +```go +func Pipeline(funcs ...func(interface{}) interface{}) func(interface{}) interface{} +``` + +例子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/function" +) + +func main() { + addOne := func(x interface{}) interface{} { + return x.(int) + 1 + } + double := func(x interface{}) interface{} { + return 2 * x.(int) + } + square := func(x interface{}) interface{} { + return x.(int) * x.(int) + } + + f := function.Pipeline(addOne, double, square) + + result := fn(2) + + fmt.Println(result) + + // Output: + // 36 +} +``` + ### Watcher

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

diff --git a/function/function.go b/function/function.go index 6e2cddb..07dcc22 100644 --- a/function/function.go +++ b/function/function.go @@ -113,3 +113,15 @@ func Schedule(d time.Duration, fn interface{}, args ...interface{}) chan bool { return quit } + +// Pipeline takes a list of functions and returns a function whose param will be passed into +// the functions one by one. +func Pipeline(funcs ...func(interface{}) interface{}) func(interface{}) interface{} { + return func(arg interface{}) (result interface{}) { + result = arg + for _, fn := range funcs { + result = fn(result) + } + return + } +} diff --git a/function/function_test.go b/function/function_test.go index 98f53f5..dfd900e 100644 --- a/function/function_test.go +++ b/function/function_test.go @@ -130,3 +130,21 @@ func TestSchedule(t *testing.T) { expected := []string{"*", "*", "*", "*", "*"} assert.Equal(expected, res) } + +func TestPipeline(t *testing.T) { + assert := internal.NewAssert(t, "TestPipeline") + + addOne := func(x interface{}) interface{} { + return x.(int) + 1 + } + double := func(x interface{}) interface{} { + return 2 * x.(int) + } + square := func(x interface{}) interface{} { + return x.(int) * x.(int) + } + + f := Pipeline(addOne, double, square) + + assert.Equal(36, f(2)) +}