Functional Go glossary.
Every functional programming term you'll see in this documentation — defined with a real fp-go example.
A
Applicative
A type class that allows applying a function wrapped in a context to a value wrapped in a context.
// Apply a function in Result to a value in Result
funcResult := result.Ok(func(x int) int { return x * 2 })
valueResult := result.Ok(21)
applied := result.Ap(valueResult)(funcResult) // Result[42]Arity
The number of arguments a function takes.
// Arity 0 (nullary)
func getValue() int { return 42 }
// Arity 1 (unary)
func double(x int) int { return x * 2 }
// Arity 2 (binary)
func add(x, y int) int { return x + y }
// Arity 3 (ternary)
func sum3(x, y, z int) int { return x + y + z }C
Chain
Also known as FlatMap or Bind. Transforms a value in a context and flattens the result.
// Chain flattens nested Results
func divide(a, b int) result.Result[int] {
if b == 0 {
return result.Err[int](errors.New("division by zero"))
}
return result.Ok(a / b)
}
result := function.Pipe2(
result.Ok(10),
result.Chain(func(x int) result.Result[int] {
return divide(x, 2) // Returns Result[int], not Result[Result[int]]
}),
)See also: Map, FlatMap
Compose
Combines functions right-to-left (mathematical composition).
// f ∘ g = f(g(x))
composed := function.Compose2(
func(x int) int { return x * 2 }, // Applied second
func(x int) int { return x + 1 }, // Applied first
)
result := composed(5) // (5 + 1) * 2 = 12Currying
Transforming a function with multiple arguments into a sequence of functions each taking a single argument.
// Uncurried
func add(x, y int) int {
return x + y
}
// Curried
func addCurried(x int) func(int) int {
return func(y int) int {
return x + y
}
}
add5 := addCurried(5)
result := add5(3) // 8E
Effect
A computation that interacts with the outside world (I/O, network, database, etc.).
// Pure function - no effects
func add(x, y int) int {
return x + y
}
// Effectful function - reads from disk
func readFile(path string) ioresult.IOResult[[]byte] {
return func() result.Result[[]byte] {
data, err := os.ReadFile(path)
return result.FromGoError(data, err)
}
}See also: IO, Pure Function, Side Effect
Either
A type representing a value that can be one of two types: Left (typically error) or Right (typically success).
// Either[error, int] - Left for errors, Right for success
func divide(a, b int) either.Either[error, int] {
if b == 0 {
return either.Left[int](errors.New("division by zero"))
}
return either.Right[error](a / b)
}Prefer Result for error handling.
Endomorphism
A function where the input and output types are the same.
// Endomorphism: int -> int
func double(x int) int {
return x * 2
}
// Endomorphism: string -> string
func uppercase(s string) string {
return strings.ToUpper(s)
}
// Compose endomorphisms
composed := function.Compose2(double, increment)F
FlatMap
See Chain.
Flow
Combines functions left-to-right (pipeline composition).
// g → f = f(g(x))
pipeline := function.Flow2(
func(x int) int { return x + 1 }, // Applied first
func(x int) int { return x * 2 }, // Applied second
)
result := pipeline(5) // (5 + 1) * 2 = 12Fold
Reduces a structure to a single value by applying a function.
// Fold Result to extract value
result := divide(10, 2)
value := result.Fold(
func(err error) int { return 0 }, // Handle error
func(val int) int { return val }, // Handle success
)
// Fold array
sum := array.Reduce(
func(acc, x int) int { return acc + x },
0,
)(numbers)See also: Reduce
Functor
A type that can be mapped over. Implements Map operation.
// Result is a Functor
result := result.Ok(21)
doubled := result.Map(func(x int) int { return x * 2 }) // Result[42]
// Array is a Functor
numbers := []int{1, 2, 3}
doubled := array.Map(func(x int) int { return x * 2 })(numbers) // [2, 4, 6]See also: Map, Applicative, Monad
H
Higher-Order Function
A function that takes functions as arguments or returns functions.
// Takes function as argument
func applyTwice(f func(int) int, x int) int {
return f(f(x))
}
// Returns function
func makeAdder(x int) func(int) int {
return func(y int) int {
return x + y
}
}Higher-Kinded Type (HKT)
A type that abstracts over type constructors. Go doesn't support HKT natively, but fp-go works around this limitation.
// HKT concept: F[_] where F is a type constructor // Result[_], Option[_], Array[_] are all type constructors // fp-go uses workarounds for HKT-like behavior // See advanced documentation for details
See also: Type Constructor
I
Identity
A monad that wraps a value without adding any computational context. Mainly used for teaching monad laws.
// Identity just wraps a value
id := identity.Of(42)
doubled := identity.Map(func(x int) int { return x * 2 })(id)
value := identity.Unwrap(doubled) // 42Immutability
Data that cannot be changed after creation. All transformations create new values.
// Immutable transformation
original := []int{1, 2, 3}
doubled := array.Map(func(x int) int { return x * 2 })(original)
// original is unchanged: [1, 2, 3]
// doubled is new: [2, 4, 6]IO
A type representing a lazy computation that performs side effects.
// IO wraps side effects
func readFile(path string) io.IO[[]byte] {
return func() []byte {
data, _ := os.ReadFile(path)
return data
}
}
// Compose IO operations
program := function.Pipe2(
readFile("config.json"),
io.Map(parseJSON),
io.Map(validateConfig),
)
// Execute when ready (side effect happens here)
config := program()IOResult
Combines IO (lazy evaluation) with Result (error handling).
func readFile(path string) ioresult.IOResult[[]byte] {
return func() result.Result[[]byte] {
data, err := os.ReadFile(path)
return result.FromGoError(data, err)
}
}
// Compose with automatic error handling
program := function.Pipe2(
readFile("config.json"),
ioresult.Chain(parseJSON),
ioresult.Chain(validateConfig),
)
// Execute and get Result
result := program()L
Lazy Evaluation
Delaying computation until the result is needed.
// Eager - computed immediately
result := expensiveComputation()
// Lazy - computed when called
lazyResult := lazy.Of(func() int {
return expensiveComputation()
})
// Computation happens here
value := lazyResult()See also: IO, Lazy Type
Lazy Type
A type that defers computation and memoizes the result.
// Create lazy value
lazyValue := lazy.Of(func() int {
fmt.Println("Computing...")
return 42
})
// First call - computes and caches
value1 := lazyValue() // Prints "Computing...", returns 42
// Second call - returns cached value
value2 := lazyValue() // Returns 42 (no print)Lens
An optic for focusing on a part of a data structure for immutable updates.
// Lens for accessing nested fields
type User struct {
Name string
Address Address
}
type Address struct {
Street string
City string
}
// Create lens for Address.City
cityLens := lens.Compose(
userAddressLens,
addressCityLens,
)
// Update city immutably
updated := lens.Set(cityLens, "New York")(user)
// user is unchanged, updated is new User with new cityM
Map
Transforms the value inside a context without changing the context.
// Map over Result
result := result.Ok(21)
doubled := result.Map(func(x int) int { return x * 2 }) // Result[42]
// Map over Array
numbers := []int{1, 2, 3}
doubled := array.Map(func(x int) int { return x * 2 })(numbers) // [2, 4, 6]
// Map over Option
opt := option.Some(21)
doubled := option.Map(func(x int) int { return x * 2 })(opt) // Some(42)Monad
A type class that supports Map (Functor), Ap (Applicative), and Chain (Monad) operations.
// Result is a Monad
result := result.Ok(10)
// Map (Functor)
mapped := result.Map(func(x int) int { return x * 2 })
// Chain (Monad)
chained := result.Chain(func(x int) result.Result[int] {
return divide(x, 2)
})- Left identity:
return a >>= f≡f a - Right identity:
m >>= return≡m - Associativity:
(m >>= f) >>= g≡m >>= (\x -> f x >>= g)
See also: Functor, Applicative
Monoid
A type with an associative binary operation and an identity element.
// Monoid for addition
// Identity: 0
// Operation: +
sum := monoid.Concat(
monoid.Sum,
[]int{1, 2, 3, 4},
) // 10
// Monoid for string concatenation
// Identity: ""
// Operation: +
combined := monoid.Concat(
monoid.String,
[]string{"Hello", " ", "World"},
) // "Hello World"See also: Semigroup
O
Optics
A family of composable tools for accessing and updating immutable data structures.
- Lens: focus on a field
- Prism: focus on a variant of a sum type
- Optional: focus on an optional field
- Traversal: focus on multiple elements
// Lens example updated := lens.Set(nameLens, "Alice")(user) // Prism example value := prism.GetOption(rightPrism)(either) // Traversal example updated := traversal.Modify(arrayTraversal, double)(numbers)
Option
A type representing an optional value: Some(value) or None.
// Some - has a value
some := option.Some(42)
// None - no value
none := option.None[int]()
// Safe operations
value := option.GetOrElse(func() int { return 0 })(some) // 42
value = option.GetOrElse(func() int { return 0 })(none) // 0P
Partial Application
Fixing some arguments of a function to create a new function with fewer arguments.
// Original function
func add(x, y int) int {
return x + y
}
// Partially applied
add5 := func(y int) int {
return add(5, y)
}
result := add5(3) // 8See also: Currying
Pipe
Applies a value to a sequence of functions left-to-right.
// x |> f |> g = g(f(x))
result := function.Pipe3(
10, // Start with 10
func(x int) int { return x + 1 }, // 11
func(x int) int { return x * 2 }, // 22
func(x int) int { return x - 2 }, // 20
)Predicate
A function that returns a boolean.
// Predicate: int -> bool
isPositive := func(x int) bool {
return x > 0
}
// Compose predicates
isEvenAndPositive := predicate.And(isEven, isPositive)
// Use in filter
filtered := array.Filter(isPositive)(numbers)Prism
An optic for focusing on a variant of a sum type.
// Prism for Either rightPrism := prism.Right[error, int]() // Get value if Right value := prism.GetOption(rightPrism)(either.Right[error](42)) // Some(42) // Returns None if Left value = prism.GetOption(rightPrism)(either.Left[int](err)) // None
Pure Function
A function that:
- Always returns the same output for the same input
- Has no side effects
// Pure - deterministic, no side effects
func add(x, y int) int {
return x + y
}
// Impure - depends on external state
var counter int
func increment() int {
counter++
return counter
}
// Impure - has side effects
func logAndAdd(x, y int) int {
fmt.Println("Adding", x, y) // Side effect!
return x + y
}See also: Side Effect, Referential Transparency
R
Reader
A monad for dependency injection. Represents a computation that depends on an environment.
// Reader[Config, User] - computation that needs Config to produce User
func getUser(id string) reader.Reader[Config, User] {
return func(config Config) User {
return config.DB.QueryUser(id)
}
}
// Compose readers
program := function.Pipe2(
getUser("123"),
reader.Map(enrichUser),
reader.Chain(validateUser),
)
// Provide environment and run
user := program(config)See also: ReaderIOResult
ReaderIOResult
Combines Reader (dependency injection), IO (lazy evaluation), and Result (error handling).
func fetchUser(id string) readerioresult.ReaderIOResult[Database, error, User] {
return func(db Database) ioresult.IOResult[User] {
return func() result.Result[User] {
user, err := db.Query(id)
return result.FromGoError(user, err)
}
}
}
// Compose with automatic error handling and dependency injection
program := function.Pipe2(
fetchUser("123"),
readerioresult.Chain(enrichUser),
readerioresult.Chain(validateUser),
)
// Provide database and execute
result := program(db)()Reduce
Combines elements of a collection into a single value.
// Sum array
sum := array.Reduce(
func(acc, x int) int { return acc + x },
0, // Initial value
)(numbers)
// Product array
product := array.Reduce(
func(acc, x int) int { return acc * x },
1, // Initial value
)(numbers)See also: Fold
Referential Transparency
An expression is referentially transparent if it can be replaced with its value without changing program behavior.
// Referentially transparent
func add(x, y int) int {
return x + y
}
// add(2, 3) can always be replaced with 5
// Not referentially transparent
func random() int {
return rand.Int()
}
// random() cannot be replaced with a fixed valueSee also: Pure Function
Result
A type representing success (Ok) or failure (Err). Recommended for error handling in v2.
// Ok - success
success := result.Ok(42)
// Err - failure
failure := result.Err[int](errors.New("error"))
// Safe operations
value := result.GetOrElse(func() int { return 0 })(success) // 42
value = result.GetOrElse(func() int { return 0 })(failure) // 0S
Semigroup
A type with an associative binary operation.
// Semigroup for addition sum := semigroup.Concat( semigroup.Sum, 1, 2, 3, ) // 6 // Semigroup for string concatenation combined := semigroup.Concat( semigroup.String, "Hello", " ", "World", ) // "Hello World"
See also: Monoid
Side Effect
An observable interaction with the outside world.
Reading/writing files. Network requests. Database queries. Printing to console. Modifying global state. Random number generation.
// Has side effects
func saveUser(user User) error {
return db.Save(user) // Side effect: database write
}
// Wrap in IO to make explicit
func saveUser(user User) ioresult.IOResult[Unit] {
return func() result.Result[Unit] {
err := db.Save(user)
return result.FromGoError(unit.Unit, err)
}
}See also: Pure Function, IO
State
A monad for stateful computations.
// State[S, A] - computation that transforms state S and produces value A
func increment() state.State[int, int] {
return func(s int) (int, int) {
newState := s + 1
return newState, newState // (new state, value)
}
}
// Compose stateful computations
program := function.Pipe2(
increment(),
state.Chain(func(x int) state.State[int, int] {
return increment()
}),
)
// Run with initial state
finalState, value := program(0) // (2, 2)T
Traverse
Transforms a collection of values in a context, collecting the results.
// Traverse array with Result
results := array.TraverseResult(func(x int) result.Result[int] {
if x < 0 {
return result.Err[int](errors.New("negative"))
}
return result.Ok(x * 2)
})(numbers)
// Returns Result[[]int] - all or nothingSee also: Sequence
Type Constructor
A type that takes type parameters to produce concrete types.
// Result is a type constructor
// Result[_] takes a type parameter
type Result[A any] interface {
// ...
}
// Concrete types:
// Result[int]
// Result[string]
// Result[User]See also: Higher-Kinded Type
U
Unit
A type with only one value, representing "no meaningful value".
// Unit type
type Unit struct{}
// Used when you need a value but don't care what it is
func doSomething() result.Result[Unit] {
// Do something...
return result.Ok(Unit{})
}