Documentation ¶
Overview ¶
Package function implements some functions for control the function execution and some is for functional programming.
Index ¶
- func AcceptIf[T any](predicate func(T) bool, apply func(T) T) func(T) (T, bool)
- func After(n int, fn any) func(args ...any) []reflect.Value
- func And[T any](predicates ...func(T) bool) func(T) bool
- func Before(n int, fn any) func(args ...any) []reflect.Value
- func Compose[T any](fnList ...func(...T) T) func(...T) T
- func Debounce(fn func(), delay time.Duration) (debouncedFn func(), cancelFn func())
- func Debounced(fn func(), delay time.Duration) func()
- func Delay(delay time.Duration, fn any, args ...any)
- func Nand[T any](predicates ...func(T) bool) func(T) bool
- func Negate[T any](predicate func(T) bool) func(T) bool
- func Nor[T any](predicates ...func(T) bool) func(T) bool
- func Or[T any](predicates ...func(T) bool) func(T) bool
- func Pipeline[T any](funcs ...func(T) T) func(T) T
- func Schedule(duration time.Duration, fn any, args ...any) chan bool
- func Throttle(fn func(), interval time.Duration) func()
- func Xnor[T any](predicates ...func(T) bool) func(T) bool
- type CurryFn
- type Watcher
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AcceptIf ¶ added in v2.3.0
AcceptIf returns another function of the same signature as the apply function but also includes a bool value to indicate success or failure. A predicate function that takes an argument of type T and returns a bool. An apply function that also takes an argument of type T and returns a modified value of the same type. Play: https://go.dev/play/p/XlXHHtzCf7d
Example ¶
adder := AcceptIf( And( func(x int) bool { return x > 10 }, func(x int) bool { return x%2 == 0 }), func(x int) int { return x + 1 }, ) result, ok := adder(20) fmt.Println(result) fmt.Println(ok) result, ok = adder(21) fmt.Println(result) fmt.Println(ok)
Output: 21 true 0 false
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 And ¶ added in v2.3.0
And returns a composed predicate that represents the logical AND of a list of predicates. It evaluates to true only if all predicates evaluate to true for the given value. Play: https://go.dev/play/p/dTBHJMQ0zD2
Example ¶
isNumericAndLength5 := And( func(s string) bool { return strings.ContainsAny(s, "0123456789") }, func(s string) bool { return len(s) == 5 }, ) fmt.Println(isNumericAndLength5("12345")) fmt.Println(isNumericAndLength5("1234")) fmt.Println(isNumericAndLength5("abcde"))
Output: true false false
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: https://go.dev/play/p/KKfugD4PKYF
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 Debounce ¶ added in v2.3.3
Debounce creates a debounced version of the provided function. Play: https://go.dev/play/p/-dGFrYn_1Zi
Example ¶
callCount := 0 fn := func() { callCount++ } debouncedFn, _ := Debounce(fn, 500*time.Millisecond) for i := 0; i < 10; i++ { debouncedFn() time.Sleep(50 * time.Millisecond) } time.Sleep(1 * time.Second) fmt.Println(callCount) debouncedFn() time.Sleep(1 * time.Second) fmt.Println(callCount)
Output: 1 2
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. Deprecated: Use Debounce function instead. 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 Nand ¶ added in v2.3.0
Nand returns a composed predicate that represents the logical NAND of a list of predicates. It evaluates to true only if all predicates evaluate to false for the given value. Play: https://go.dev/play/p/Rb-FdNGpgSO
Example ¶
isNumericAndLength5 := Nand( func(s string) bool { return strings.ContainsAny(s, "0123456789") }, func(s string) bool { return len(s) == 5 }, ) fmt.Println(isNumericAndLength5("12345")) fmt.Println(isNumericAndLength5("1234")) fmt.Println(isNumericAndLength5("abcdef"))
Output: false false true
func Negate ¶ added in v2.3.0
Negate returns a predicate that represents the logical negation of this predicate. Play: https://go.dev/play/p/jbI8BtgFnVE
Example ¶
// Define some simple predicates for demonstration isUpperCase := func(s string) bool { return strings.ToUpper(s) == s } isLowerCase := func(s string) bool { return strings.ToLower(s) == s } isMixedCase := Negate(Or(isUpperCase, isLowerCase)) fmt.Println(isMixedCase("ABC")) fmt.Println(isMixedCase("AbC"))
Output: false true
func Nor ¶ added in v2.3.0
Nor returns a composed predicate that represents the logical NOR of a list of predicates. It evaluates to true only if all predicates evaluate to false for the given value. Play: https://go.dev/play/p/2KdCoBEOq84
Example ¶
match := Nor( func(s string) bool { return strings.ContainsAny(s, "0123456789") }, func(s string) bool { return len(s) == 5 }, ) fmt.Println(match("dbcdckkeee")) match = Nor( func(s string) bool { return strings.ContainsAny(s, "0123456789") }, func(s string) bool { return len(s) == 5 }, ) fmt.Println(match("0123456789"))
Output: true false
func Or ¶ added in v2.3.0
Or returns a composed predicate that represents the logical OR of a list of predicates. It evaluates to true if at least one of the predicates evaluates to true for the given value. Play: https://go.dev/play/p/LitCIsDFNDA
Example ¶
containsDigitOrSpecialChar := Or( func(s string) bool { return strings.ContainsAny(s, "0123456789") }, func(s string) bool { return strings.ContainsAny(s, "!@#$%") }, ) fmt.Println(containsDigitOrSpecialChar("hello!")) fmt.Println(containsDigitOrSpecialChar("hello"))
Output: true false
func Pipeline ¶ added in v2.1.8
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(2*time.Second, increase) time.Sleep(2 * time.Second) close(stop) fmt.Println(count)
Output: 2
func Throttle ¶ added in v2.3.3
Throttle creates a throttled version of the provided function. The returned function guarantees that it will only be invoked at most once per interval. Play: https://go.dev/play/p/HpoMov-tJSN
Example ¶
callCount := 0 fn := func() { callCount++ } throttledFn := Throttle(fn, 1*time.Second) for i := 0; i < 5; i++ { throttledFn() } time.Sleep(1 * time.Second) fmt.Println(callCount)
Output: 1
func Xnor ¶ added in v2.3.0
Xnor returns a composed predicate that represents the logical XNOR of a list of predicates. It evaluates to true only if all predicates evaluate to true or false for the given value. Play: https://go.dev/play/p/FJxko8SFbqc
Example ¶
isEven := func(i int) bool { return i%2 == 0 } isPositive := func(i int) bool { return i > 0 } match := Xnor(isEven, isPositive) fmt.Println(match(2)) fmt.Println(match(-3)) fmt.Println(match(3))
Output: true true false
Types ¶
type CurryFn ¶ added in v2.1.13
type CurryFn[T any] func(...T) T
CurryFn is for make curry function
func (CurryFn[T]) New ¶ added in v2.1.13
func (cf CurryFn[T]) New(val T) func(...T) T
New make a curry function for specific value. Play: https://go.dev/play/p/5HopfDwANKX
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: https://go.dev/play/p/l2yrOpCLd1I
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.