Documentation
¶
Overview ¶
Package fn is the package you should use for manipulating functions.
It has several usages, including "composing" (combining) functions together and "currying" (splitting) functions.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Compose ¶
func Compose[T, U, V any]( fn1 func(T) U, fn2 func(U) V, ) func(T) V
Compose takes two functions and returns one function that
calls both The returned function calls fn2 with the
output of fn1.
Compose is roughly the opposite of Curry. Where Curry "expands" one function into two, Compose "compresses" two functions down into one.
Example usage:
composedFn := Compose( func(t int) string { return strconv.Itoa(t) }, func(u string) string { return fmt.Sprintf("str-%s", u) }, ) answer := composedFn(123) // answer will be "str-123"
func Curry ¶
func Curry[T, U, V any]( fn func(T, U) V, ) func(T) func(U) V
Curry takes one function with two parameters and returns a single-parameter function that returns a second single parameter function, which then returns the value of the original function.
Example usage:
curriedFn := Curry(func(t int, u string) string { return fmt.Sprintf("%d-%s", t, u) } fn1 := curriedFn(1) answer := fn1("two") // answer will be "1-2"
Compose is the roughly the opposite of Curry. Curry "expands" one function into two, but Compose "compresses" two functions down into one.
Types ¶
This section is empty.