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

doc: add doc for Pipeline function

This commit is contained in:
dudaodong
2023-02-23 10:52:07 +08:00
parent 24eb2bbacd
commit f6cd98086f
4 changed files with 119 additions and 0 deletions

View File

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

View File

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