fn

package
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 13, 2025 License: MIT Imports: 2 Imported by: 1

Documentation

Overview

Package fn provides utility functions for function manipulation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func After

func After[T any](n int, fn func() T) func() T

After creates a function that invokes func once it's called n or more times. Example: fn := After(3, func() { fmt.Println("done") }); fn(); fn(); fn() // prints "done" on the third call

func Before

func Before[T any](n int, fn func() T) func() T

Before creates a function that invokes func, with the arguments provided, at most n times. Example: fn := Before(3, func() { fmt.Println("called") }); fn(); fn(); fn(); fn() // prints "called" only 3 times

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. 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 Curry

func Curry[T, R any](fn func(T, T) R, arity int) func(T) func(T) R

Curry creates a function that accepts arguments of func and either invokes func returning its result, if at least arity number of arguments have been provided, or returns a function that accepts the remaining arguments. Example: add := func(a, b int) int { return a + b }; addCurried := Curry(add, 2); add1 := addCurried(1); add1(2) -> 3

func Debounce

func Debounce(fn func(), wait time.Duration) func()

Debounce creates a debounced function that delays invoking func until after wait milliseconds have elapsed since the last time the debounced function was invoked. Example: fn := Debounce(func() { fmt.Println("called") }, 100); fn(); fn(); fn() // prints "called" only once after 100ms

func Delay

func Delay(fn func(), wait time.Duration)

Delay invokes func after wait milliseconds. 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. Example: fibonacci := Memoize(func(n int) int { if n <= 1 { return n }; return fibonacci(n-1) + fibonacci(n-2) })

func Negate

func Negate[T any](predicate func(T) bool) func(T) bool

Negate creates a function that negates the result of the predicate function. 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. 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. 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. Example: addOne := func(x int) int { return x + 1 }; double := func(x int) int { return x * 2 }; doubleTheAddOne := Pipe(addOne, double); doubleTheAddOne(3) -> 8

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 complex function that's hard to implement in Go due to type system limitations. For now, we'll just provide a simplified version that swaps the first two arguments.

func Retry

func Retry[T any](fn func() (T, error), maxRetries int, delay time.Duration) func() (T, error)

Retry creates a function that retries the given function until it succeeds or reaches the maximum number of retries. Example: fn := Retry(func() error { return errors.New("error") }, 3, 100); 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 complex function that's hard to implement in Go due to type system limitations. For now, we'll just provide a simplified version that works with two arguments.

func Throttle

func Throttle(fn func(), wait time.Duration) func()

Throttle creates a throttled function that only invokes func at most once per every wait milliseconds. Example: fn := Throttle(func() { fmt.Println("called") }, 100*time.Millisecond); fn(); fn(); fn() // prints "called" only once per 100ms

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. 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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL