Reference
API Reference
Complete API reference for fp-go v2 covering core types, collections, function utilities, and advanced patterns.
01
Core Types
The fundamental building blocks for functional programming in Go.
Essential Types
| Symbol | Description |
|---|---|
| Option | Represent optional values without null pointers |
| Result | Type-safe error handling with Ok/Err variants |
| Either | Represent a value of one of two possible types (Left/Right) |
| IO | Lazy side effects that execute when called |
| IOResult | Combine IO with Result for effectful error handling |
| IOEither | Combine IO with Either for lazy error handling |
| IOOption | Combine IO with Option for optional effects |
Reader Types
Dependency injection and environment management:
| Symbol | Description |
|---|---|
| Reader | Computations that depend on a shared environment |
| ReaderEither | Reader with Either for error handling |
| ReaderIO | Reader with IO for side effects |
| ReaderIOEither | Reader with IO and Either (most powerful combination) |
| ReaderIOResult | Reader with IO and Result |
| ReaderOption | Reader with Option for optional dependencies |
State & Advanced Types
Stateful computations and advanced patterns:
| Symbol | Description |
|---|---|
| State | Stateful computations with get/put operations |
| StateReaderIOEither | State with Reader, IO, and Either (ultimate monad stack) |
| Lazy | Lazy evaluation with memoization |
| Identity | Simplest monad wrapper |
| Constant | Constant functor |
| Endomorphism | Functions from type to itself |
core_example.go
package main
import (
"fmt"
O "github.com/IBM/fp-go/option"
E "github.com/IBM/fp-go/either"
R "github.com/IBM/fp-go/result"
)
func main() {
// Option for optional values
opt := O.Some(42)
fmt.Println(O.IsSome(opt)) // true
// Either for errors
either := E.Right[error](42)
fmt.Println(E.IsRight(either)) // true
// Result for Ok/Err
result := R.Ok[error](42)
fmt.Println(R.IsOk(result)) // true
}
02
Collections & Arrays
Functional operations on slices and maps.
Array Operations
| Symbol | Description |
|---|---|
| Array | Core array operations (Map, Filter, Reduce) |
| Array.Ap | Applicative operations for arrays |
| Array.Eq | Equality checking for arrays |
| Array.Find | Search and lookup operations |
| Array.Monoid | Monoid instance for arrays |
| Array.Sort | Sorting with custom comparators |
| Array.Uniq | Remove duplicates |
| Array.Zip | Combine multiple arrays |
| NonEmptyArray | Arrays guaranteed to have at least one element |
Record Operations
| Symbol | Description |
|---|---|
| Record | Core map operations (Map, Filter, Keys, Values) |
| Record.Ap | Applicative operations for records |
| Record.Chain | Monadic chaining for records |
| Record.Conversion | Convert between records and arrays |
| Record.Eq | Equality checking for records |
| Record.Monoid | Monoid instance for records |
| Record.Ord | Ordering for records |
| Record.Traverse | Traverse records with effects |
| Sequence.Traverse | Generic sequence traversal |
collections_example.go
package main
import (
"fmt"
A "github.com/IBM/fp-go/array"
R "github.com/IBM/fp-go/record"
)
func main() {
// Array operations
numbers := []int{1, 2, 3, 4, 5}
doubled := A.Map(func(n int) int { return n * 2 })(numbers)
fmt.Println(doubled) // [2 4 6 8 10]
// Record operations
ages := map[string]int{"Alice": 30, "Bob": 25}
names := R.Keys(ages)
fmt.Println(names) // [Alice Bob]
}
03
Function Utilities
Core function manipulation and composition utilities.
Composition & Flow
| Symbol | Description |
|---|---|
| Pipe & Flow | Left-to-right function composition |
| Compose | Right-to-left function composition |
| Function | Core function utilities (Identity, Constant, Flip) |
Currying & Binding
| Symbol | Description |
|---|---|
| Bind & Curry | Currying and partial application |
Type Classes
| Symbol | Description |
|---|---|
| Eq | Equality type class |
| Ord | Ordering type class |
| Semigroup | Associative binary operation |
| Monoid | Semigroup with identity element |
| Magma | Binary operation without laws |
Primitive Utilities
| Symbol | Description |
|---|---|
| Boolean | Boolean operations and combinators |
| Number | Numeric operations and instances |
| String | String operations and instances |
| Predicate | Predicate combinators (And, Or, Not) |
| Tuple | Tuple operations and utilities |
utilities_example.go
package main
import (
"fmt"
F "github.com/IBM/fp-go/function"
)
func main() {
// Pipe: left-to-right composition
add10 := func(n int) int { return n + 10 }
double := func(n int) int { return n * 2 }
result := F.Pipe2(
5,
add10, // 5 + 10 = 15
double, // 15 * 2 = 30
)
fmt.Println(result) // 30
}
04
Type Hierarchy
Understanding the relationships between types:
hierarchy.txt
Functor ├─ Applicative │ └─ Monad │ ├─ Option │ ├─ Either │ ├─ Result │ ├─ IO │ ├─ IOEither │ ├─ IOResult │ ├─ IOOption │ ├─ Reader │ ├─ ReaderEither │ ├─ ReaderIO │ ├─ ReaderIOEither │ ├─ ReaderIOResult │ ├─ ReaderOption │ ├─ State │ └─ StateReaderIOEither └─ Array
Key Concepts:
- Functor - Types that can be mapped over (
Map) - Applicative - Functors with
Of(pure) andAp(apply) - Monad - Applicatives with
Chain(flatMap/bind) - Array - Functor but not a monad (multiple values)
05
Common Patterns
Frequently used patterns and idioms.
Error Handling
Before
traditional.go
// ❌ Traditional Go error handling
func getUser(id string) (*User, error) {
user, err := db.FindUser(id)
if err != nil {
return nil, err
}
return user, nil
}
After
functional.go
// ✅ Functional error handling
func getUser(id string) either.Either[error, User] {
return db.FindUser(id)
}
// Chain operations
result := pipe.Pipe2(
getUser("123"),
E.Chain(validateUser),
E.Map(enrichUser),
)
Side Effects
effects.go
// IO for lazy effects
io := IO.Of(func() int {
fmt.Println("Computing...")
return 42
})
// Execute when ready
result := io() // Prints "Computing..." and returns 42
// IOEither for effects with errors
ioe := IOE.TryCatch(func() (int, error) {
return readFile("config.json")
})
Dependency Injection
reader.go
type Config struct {
DBUrl string
Port int
}
// Reader for dependencies
getDBUrl := R.Asks(func(c Config) string {
return c.DBUrl
})
// ReaderIOEither for full power
program := RIOE.Chain(
getDBUrl,
func(url string) RIOE.ReaderIOEither[Config, error, *DB] {
return RIOE.FromIO(connectDB(url))
},
)
// Run with config
config := Config{DBUrl: "localhost:5432", Port: 8080}
result := program(config)()
06
Quick Links
Getting Started
- Introduction - What is fp-go?
- Installation - Get up and running
- Quick Start - Your first functional program
Learning Resources
- Core Concepts - Fundamental FP concepts
- Recipes - Practical examples
- Migration Guide - Upgrade from v1
Advanced Topics
- Patterns - Monad transformers, Free monads, Tagless final
- Type Theory - Category theory foundations
- Performance - Optimization techniques
- Architecture - Application design patterns
Legacy Version
- v1.x Documentation - Previous version docs