mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-11 16:22:26 +08:00
feat: add Pipeline function
This commit is contained in:
@@ -113,3 +113,15 @@ func Schedule(d time.Duration, fn any, args ...any) chan bool {
|
|||||||
|
|
||||||
return quit
|
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[T any](funcs ...func(T) T) func(T) T {
|
||||||
|
return func(arg T) (result T) {
|
||||||
|
result = arg
|
||||||
|
for _, fn := range funcs {
|
||||||
|
result = fn(result)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -134,3 +134,21 @@ func TestSchedule(t *testing.T) {
|
|||||||
// expected := []string{"*", "*", "*", "*", "*"}
|
// expected := []string{"*", "*", "*", "*", "*"}
|
||||||
// assert.Equal(expected, res)
|
// assert.Equal(expected, res)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPipeline(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestPipeline")
|
||||||
|
|
||||||
|
addOne := func(x int) int {
|
||||||
|
return x + 1
|
||||||
|
}
|
||||||
|
double := func(x int) int {
|
||||||
|
return 2 * x
|
||||||
|
}
|
||||||
|
square := func(x int) int {
|
||||||
|
return x * x
|
||||||
|
}
|
||||||
|
|
||||||
|
f := Pipeline(addOne, double, square)
|
||||||
|
|
||||||
|
assert.Equal(36, f(2))
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package function
|
package function
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/duke-git/lancet/v2/internal"
|
"github.com/duke-git/lancet/v2/internal"
|
||||||
@@ -34,4 +35,5 @@ func longRunningTask() {
|
|||||||
for i := 0; i < 10000000; i++ {
|
for i := 0; i < 10000000; i++ {
|
||||||
slice = append(slice, int64(i))
|
slice = append(slice, int64(i))
|
||||||
}
|
}
|
||||||
|
fmt.Println(slice)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user