Documentation
¶
Overview ¶
Package fn provides utility functions for function manipulation.
Index ¶
- func After[T any](n int, fn func() T) func() T
- func Before[T any](n int, fn func() T) func() T
- func Compose[T any](fns ...func(T) T) func(T) T
- func Debounce(fn func(), wait time.Duration) func()
- func Delay(fn func(), wait time.Duration)
- func Memoize[T comparable, R any](fn func(T) R) func(T) R
- func Negate[T any](predicate func(T) bool) func(T) bool
- func Once[T any](fn func() T) func() T
- func Partial[T, R any](fn func(T, T) R, partial T) func(T) R
- func Pipe[T any](fns ...func(T) T) func(T) T
- func Rearg[T, R any](fn func(T, T) R) func(T, T) R
- func Retry[T any](fn func() (T, error), maxRetries int, delay time.Duration) func() (T, error)
- func Spread[T, R any](fn func(T, T) R) func([]T) R
- func Throttle(fn func(), wait time.Duration) func()
- func TransformBatch[T any, R any](records []T, transformerFn func([]T) []R, batchSize int) []R
- func TransformConcurrent[T any, R any](records []T, transformerFn func(T) R, numWorkers int) []R
- func TransformList[T any, R any](records []T, transformerFn func(T) R) []R
- func TransformListWithError[T any, R any](records []T, transformerFn func(T) (R, error)) ([]R, []error)
- func TransformMap[K comparable, V any, R any](m map[K]V, transformerFn func(V) R) map[K]R
- func Wrap[T, R, S any](fn func(T) R, wrapper func(func(T) R, T) S) func(T) S
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func After ¶
After creates a function that invokes the provided function once it's called n or more times.
Parameters:
- n: The number of calls before invoking the function
- fn: The function to invoke after n calls
Returns:
- func() T: A function that will invoke fn after being called n or more times
Example:
counter := 0 f := After(3, func() int { counter++ return counter }) // First two calls return 0 result := f() // result: 0 result = f() // result: 0 // Third call executes the function and returns 1 result = f() // result: 1 // Fourth call returns 2 result = f() // result: 2
func Before ¶
Before creates a function that invokes the provided function at most n times.
Parameters:
- n: The maximum number of times to invoke the function
- fn: The function to invoke at most n times
Returns:
- func() T: A function that will invoke fn at most n times and return the last result afterwards
Example:
counter := 0 f := Before(3, func() int { counter++ return counter }) // First two calls return incrementing values result := f() // result: 1 result = f() // result: 2 // Third and subsequent calls return the last result result = f() // result: 3 result = f() // result: 3 (function not called again)
func Compose ¶
func Compose[T any](fns ...func(T) T) func(T) T
Compose creates a function that is the composition of the provided functions. The resulting function executes from right to left (last to first).
Parameters:
- fns: The functions to compose
Returns:
- func(T) T: A function that is the composition of the provided functions
Example: addOne := func(x int) int { return x + 1 }; double := func(x int) int { return x * 2 }; addOneThenDouble := Compose(double, addOne); addOneThenDouble(3) -> 8
func Debounce ¶
Debounce creates a debounced function that delays invoking the provided function until after wait milliseconds have elapsed since the last time the debounced function was invoked.
Parameters:
- fn: The function to debounce
- wait: The duration to wait before invoking the function
Returns:
- func(): A debounced function that will only execute after wait duration has passed since its last invocation
Example:
counter := 0 f := func() { counter++ } debounced := Debounce(f, 50*time.Millisecond) // Call multiple times in quick succession debounced() debounced() debounced() // Wait for the debounce period time.Sleep(100 * time.Millisecond) // Counter is incremented only once // counter == 1
func Delay ¶
Delay invokes func after wait milliseconds.
Parameters:
- fn: The function to delay
- wait: The duration to wait before invoking the function
Example: Delay(func() { fmt.Println("called") }, 100) // prints "called" after 100ms
func Memoize ¶
func Memoize[T comparable, R any](fn func(T) R) func(T) R
Memoize creates a function that memoizes the result of func.
Parameters:
- fn: The function to memoize
Returns:
- func(T) R: A memoized function that caches its results based on the input arguments
Example: fibonacci := Memoize(func(n int) int { if n <= 1 { return n }; return fibonacci(n-1) + fibonacci(n-2) })
func Negate ¶
Negate creates a function that negates the result of the predicate function.
Parameters:
- predicate: The predicate function to negate
Returns:
- func(T) bool: A function that returns the logical NOT of the result of predicate
Example: isEven := func(n int) bool { return n % 2 == 0 }; isOdd := Negate(isEven); isOdd(3) -> true
func Once ¶
func Once[T any](fn func() T) func() T
Once creates a function that is restricted to invoking func once.
Parameters:
- fn: The function to restrict to a single invocation
Returns:
- func() T: A function that will only invoke fn on the first call and return the result for subsequent calls
Example: initialize := Once(func() { fmt.Println("initialized") }); initialize(); initialize() // prints "initialized" only once
func Partial ¶
func Partial[T, R any](fn func(T, T) R, partial T) func(T) R
Partial creates a function that invokes func with partials prepended to the arguments it receives.
Parameters:
- fn: The function to partially apply
- partial: The value to prepend to the argument list
Returns:
- func(T) R: A function that invokes fn with partial as the first argument and the provided argument as the second
Example: greet := func(greeting, name string) string { return greeting + " " + name }; sayHello := Partial(greet, "Hello"); sayHello("John") -> "Hello John"
func Pipe ¶
func Pipe[T any](fns ...func(T) T) func(T) T
Pipe creates a function that is the composition of the provided functions, where each function consumes the return value of the previous. The resulting function executes from left to right (first to last).
Parameters:
- fns: The functions to pipe
Returns:
- func(T) T: A function that passes its input through the provided functions in sequence
Example: addOne := func(x int) int { return x + 1 }; double := func(x int) int { return x * 2 }; doubleTheAddOne := Pipe(addOne, double); doubleTheAddOne(3) -> 11
func Rearg ¶
func Rearg[T, R any](fn func(T, T) R) func(T, T) R
Rearg creates a function that invokes func with arguments arranged according to the specified indexes. This is a simplified version that swaps the first two arguments.
Parameters:
- fn: The function whose arguments to rearrange
Returns:
- func(T, T) R: A function that invokes fn with the first two arguments swapped
Example: multiply := func(a, b int) int { return a * b }; divideInstead := Rearg(multiply); divideInstead(10, 2) -> 20
func Retry ¶
Retry creates a function that retries the given function until it succeeds or reaches the maximum number of retries.
Parameters:
- fn: The function to retry
- maxRetries: The maximum number of retry attempts
- delay: The duration to wait between retry attempts
Returns:
- func() (T, error): A function that will retry fn up to maxRetries times with the specified delay between attempts
Example: fn := Retry(func() (int, error) { return 0, errors.New("error") }, 3, 100*time.Millisecond); fn() // retries 3 times with 100ms delay
func Spread ¶
func Spread[T, R any](fn func(T, T) R) func([]T) R
Spread creates a function that invokes func with the array of arguments it receives. This is a simplified version that works with two arguments.
Parameters:
- fn: The function to spread arguments over
Returns:
- func([]T) R: A function that accepts an array and calls fn with elements from the array as individual arguments
Example: add := func(a, b int) int { return a + b }; addArray := Spread(add); addArray([]int{1, 2}) -> 3
func Throttle ¶
Throttle creates a throttled function that only invokes func at most once per every wait milliseconds.
Parameters:
- fn: The function to throttle
- wait: The minimum duration between function invocations
Returns:
- func(): A throttled function that will only execute at most once per wait duration
Example: fn := Throttle(func() { fmt.Println("called") }, 100*time.Millisecond); fn(); fn(); fn() // prints "called" only once per 100ms
func TransformBatch ¶ added in v1.0.2
TransformBatch transforms a slice in batches and returns the combined results.
Parameters:
- records: The slice of elements to transform
- transformerFn: The function to apply to each batch of elements
- batchSize: The size of each batch
Returns:
- []R: A new slice containing the combined results of all batch transformations
Example:
numbers := []int{1, 2, 3, 4, 5} doubled := TransformBatch(numbers, func(batch []int) []int { var result []int for _, n := range batch { result = append(result, n*2) } return result }, 2) ===> doubled = []int{2, 4, 6, 8, 10}
func TransformConcurrent ¶ added in v1.0.2
TransformConcurrent transforms a slice concurrently using a specified number of workers.
Parameters:
- records: The slice of elements to transform
- transformerFn: The function to apply to each element
- numWorkers: The number of concurrent workers to use for transformation
Returns:
- []R: A new slice containing the transformed elements
Example:
numbers := []int{1, 2, 3, 4, 5} squares := TransformConcurrent(numbers, func(x int) int { return x * x }, 2) ===> squares = []int{1, 4, 9, 16, 25}
func TransformList ¶ added in v1.0.2
TransformList generic function takes a list of records and applies a transformer function to each element, returning a slice of transformed elements.
Parameters:
- records: The slice of elements to transform
- transformerFn: The function to apply to each element
Returns:
- []R: A new slice containing the transformed elements
Example:
numbers := []int{1, 2, 3} squares := TransformList(numbers, func(x int) int { return x * x }) ===> squares = []int{1, 4, 9}
func TransformListWithError ¶ added in v1.0.2
func TransformListWithError[T any, R any](records []T, transformerFn func(T) (R, error)) ([]R, []error)
TransformListWithError transforms a slice and collects any errors that occur during transformation.
Parameters:
- records: The slice of elements to transform
- transformerFn: The function to apply to each element, which may return an error
Returns:
- []R: A new slice containing the successfully transformed elements
- []error: A slice containing any errors that occurred during transformation
Example:
numbers := []string{"1", "2", "abc", "3"} parsed, errors := TransformListWithError(numbers, func(s string) (int, error) { return strconv.Atoi(s) }) ===> parsed = []int{1, 2, 3}, errors contains the error from parsing "abc"
func TransformMap ¶ added in v1.0.2
func TransformMap[K comparable, V any, R any](m map[K]V, transformerFn func(V) R) map[K]R
TransformMap transforms a map of one type to a map of another type using a transformer function.
Parameters:
- m: The map to transform
- transformerFn: The function to apply to each value in the map
Returns:
- map[K]R: A new map with the same keys but transformed values
Example:
ages := map[string]int{"John": 30, "Jane": 25} doubled := TransformMap(ages, func(age int) int { return age * 2 }) ===> doubled = map[string]int{"John": 60, "Jane": 50}
func Wrap ¶
func Wrap[T, R, S any](fn func(T) R, wrapper func(func(T) R, T) S) func(T) S
Wrap creates a function that provides value to the wrapper function as its first argument.
Parameters:
- fn: The function to wrap
- wrapper: The wrapper function that receives fn as its first argument
Returns:
- func(T) S: A function that invokes wrapper with fn and the provided argument
Example: hello := func(name string) string { return "Hello " + name }; withExclamation := Wrap(hello, func(hello func(string) string, name string) string { return hello(name) + "!" }); withExclamation("John") -> "Hello John!"
Types ¶
This section is empty.