Documentation ¶
Overview ¶
Package function implements some functions for control the function execution and some is for functional programming.
Index ¶
- func After(n int, fn any) func(args ...any) []reflect.Value
- func Before(n int, fn any) func(args ...any) []reflect.Value
- func Compose[T any](fnList ...func(...T) T) func(...T) T
- func Debounced(fn func(), duration time.Duration) func()
- func Delay(delay time.Duration, fn any, args ...any)
- func Pipeline[T any](funcs ...func(T) T) func(T) T
- func Schedule(d time.Duration, fn any, args ...any) chan bool
- type CurryFn
- type Watcher
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func After ¶
After creates a function that invokes func once it's called n or more times. Play: https://go.dev/play/p/8mQhkFmsgqs
Example ¶
fn := After(2, func() { fmt.Println("test") }) fn() fn()
Output: test
func Before ¶
Before creates a function that invokes func once it's called less than n times. Play: https://go.dev/play/p/0HqUDIFZ3IL
Example ¶
fn := Before(2, func() { fmt.Println("test") }) fn() fn() fn() fn()
Output: test test
func Compose ¶
func Compose[T any](fnList ...func(...T) T) func(...T) T
Compose compose the functions from right to left. Play: Todo
Example ¶
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 Debounced ¶
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
Example ¶
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 Delay ¶
Delay make the function execution after delayed time. Play: https://go.dev/play/p/Ivtc2ZE-Tye
Example ¶
var print = func(s string) { fmt.Println(s) } Delay(2*time.Second, print, "hello")
Output: hello
func Pipeline ¶
func Pipeline[T any](funcs ...func(T) T) func(T) T
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
Example ¶
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
func Schedule ¶
Schedule invoke function every duration time, util close the returned bool channel. Play: https://go.dev/play/p/hbON-Xeyn5N
Example ¶
count := 0 increase := func() { count++ } stop := Schedule(1*time.Second, increase) time.Sleep(3 * time.Second) close(stop) fmt.Println(count)
Output: 3
Types ¶
type CurryFn ¶
type CurryFn[T any] func(...T) T
CurryFn is for make curry function
func (CurryFn[T]) New ¶
func (cf CurryFn[T]) New(val T) func(...T) T
New make a curry function for specific value. Play: Todo
Example ¶
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
type Watcher ¶
type Watcher struct {
// contains filtered or unexported fields
}
Watcher is used for record code excution time Play: Todo
Example ¶
w := NewWatcher() w.Start() longRunningTask() w.Stop() // eapsedTime := w.GetElapsedTime().Milliseconds() fmt.Println("foo") w.Reset()
Output: foo
func (*Watcher) GetElapsedTime ¶
GetElapsedTime get excute elapsed time.