fn

package
v0.0.0-...-a8c43e7 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2022 License: Apache-2.0 Imports: 0 Imported by: 0

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.

func Identity

func Identity[T any](t T) T

ID is a function that takes in a value and immediately returns it without modification. This functionality is useless in most situations, but there are some cases in which it's useful.

You'll know them when you see them.

Types

This section is empty.

Jump to

Keyboard shortcuts

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