Documentation
¶
Overview ¶
Package identity implements the Identity monad, the simplest possible monad.
Fantasy Land Specification ¶
This implementation corresponds to the Fantasy Land Identity type: https://github.com/fantasyland/fantasy-land
Implemented Fantasy Land algebras:
- Functor: https://github.com/fantasyland/fantasy-land#functor
- Apply: https://github.com/fantasyland/fantasy-land#apply
- Applicative: https://github.com/fantasyland/fantasy-land#applicative
- Chain: https://github.com/fantasyland/fantasy-land#chain
- Monad: https://github.com/fantasyland/fantasy-land#monad
Overview ¶
The Identity monad is a trivial monad that simply wraps a value without adding any computational context. It's the identity element in the category of monads, meaning it doesn't add any effects or behavior - it just passes values through.
While seemingly useless, the Identity monad serves several important purposes:
- As a baseline for understanding more complex monads
- For testing monad transformers
- As a default when no specific monad is needed
- For generic code that works with any monad
In this implementation, Identity[A] is simply represented as type A itself, making it a zero-cost abstraction.
Core Concepts ¶
The Identity monad implements the standard monadic operations:
- Of: Wraps a value (identity function)
- Map: Transforms the wrapped value
- Chain (FlatMap): Chains computations
- Ap: Applies a wrapped function to a wrapped value
Since Identity adds no context, all these operations reduce to simple function application.
Basic Usage ¶
Creating and transforming Identity values:
// Of wraps a value (but it's just the identity)
x := identity.Of(42)
// x is just 42
// Map transforms the value
doubled := identity.Map(func(n int) int {
return n * 2
})(x)
// doubled is 84
// Chain for monadic composition
result := identity.Chain(func(n int) int {
return n + 10
})(doubled)
// result is 94
Functor Operations ¶
Map transforms values:
import F "github.com/IBM/fp-go/v2/function"
// Simple mapping
result := F.Pipe1(
5,
identity.Map(func(n int) int { return n * n }),
)
// result is 25
// MapTo replaces with a constant
result := F.Pipe1(
"ignored",
identity.MapTo[string, int](100),
)
// result is 100
Applicative Operations ¶
Ap applies wrapped functions:
add := func(a int) func(int) int {
return func(b int) int {
return a + b
}
}
// Apply a curried function
result := F.Pipe1(
add(10),
identity.Ap[int, int](5),
)
// result is 15
Monad Operations ¶
Chain for sequential composition:
// Chain multiple operations
result := F.Pipe2(
10,
identity.Chain(N.Mul(2)),
identity.Chain(N.Add(5)),
)
// result is 25
// ChainFirst executes for side effects but keeps original value
result := F.Pipe1(
42,
identity.ChainFirst(func(n int) string {
return fmt.Sprintf("Value: %d", n)
}),
)
// result is still 42
Do Notation ¶
The package provides "do notation" for imperative-style composition:
type Result struct {
X int
Y int
Sum int
}
result := F.Pipe3(
identity.Do(Result{}),
identity.Bind(
func(r Result) func(int) Result {
return func(x int) Result {
r.X = x
return r
}
},
func(Result) int { return 10 },
),
identity.Bind(
func(r Result) func(int) Result {
return func(y int) Result {
r.Y = y
return r
}
},
func(Result) int { return 20 },
),
identity.Let(
func(r Result) func(int) Result {
return func(sum int) Result {
r.Sum = sum
return r
}
},
func(r Result) int { return r.X + r.Y },
),
)
// result is Result{X: 10, Y: 20, Sum: 30}
Sequence and Traverse ¶
Convert tuples of Identity values:
import T "github.com/IBM/fp-go/v2/tuple"
// Sequence a tuple
tuple := T.MakeTuple2(1, 2)
result := identity.SequenceTuple2(tuple)
// result is T.Tuple2[int, int]{1, 2}
// Traverse with transformation
tuple := T.MakeTuple2(1, 2)
result := identity.TraverseTuple2(
N.Mul(2),
N.Mul(3),
)(tuple)
// result is T.Tuple2[int, int]{2, 6}
Monad Interface ¶
Get a monad instance for generic code:
m := identity.Monad[int, string]()
// Use monad operations
value := m.Of(42)
mapped := m.Map(func(n int) string {
return fmt.Sprintf("Number: %d", n)
})(value)
Why Identity? ¶
The Identity monad might seem pointless, but it's useful for:
1. Testing: Test monad transformers with a simple base monad 2. Defaults: Provide a default when no specific monad is needed 3. Learning: Understand monad laws without additional complexity 4. Abstraction: Write generic code that works with any monad
Example of generic code:
func ProcessWithMonad[M any](
monad monad.Monad[int, string, M, M, func(int) M],
value int,
) M {
return F.Pipe2(
monad.Of(value),
monad.Map(N.Mul(2)),
monad.Map(func(n int) string { return fmt.Sprintf("%d", n) }),
)
}
// Works with Identity
result := ProcessWithMonad(identity.Monad[int, string](), 21)
// result is "42"
Type Alias ¶
The package defines:
type Operator[A, B any] = func(A) B
This represents an Identity computation from A to B, which is just a function.
Functions ¶
Core operations:
- Of[A any](A) A - Wrap a value (identity)
- Map[A, B any](func(A) B) func(A) B - Transform value
- Chain[A, B any](func(A) B) func(A) B - Monadic bind
- Ap[B, A any](A) func(func(A) B) B - Apply function
Monad variants:
- MonadMap, MonadChain, MonadAp - Uncurried versions
Additional operations:
- MapTo[A, B any](B) func(A) B - Replace with constant
- ChainFirst[A, B any](func(A) B) func(A) A - Execute for effect
- Flap[B, A any](A) func(func(A) B) B - Flip application
Do notation:
- Do[S any](S) S - Initialize context
- Bind[S1, S2, T any] - Bind computation result
- Let[S1, S2, T any] - Bind pure value
- LetTo[S1, S2, B any] - Bind constant
- BindTo[S1, T any] - Initialize from value
- ApS[S1, S2, T any] - Apply in context
Sequence/Traverse:
- SequenceT1-10 - Sequence tuples of size 1-10
- SequenceTuple1-10 - Sequence tuple types
- TraverseTuple1-10 - Traverse with transformations
Monad instance:
- Monad[A, B any]() - Get monad interface
Related Packages ¶
- function: Function composition utilities
- monad: Monad interface definition
- tuple: Tuple types for sequence operations
Index ¶
- func ApS[S1, S2, T any](setter func(T) func(S1) S2, fa T) func(S1) S2
- func Bind[S1, S2, T any](setter func(T) func(S1) S2, f func(S1) T) func(S1) S2
- func BindTo[S1, T any](setter func(T) S1) func(T) S1
- func Do[S any](empty S) S
- func Extract[A any](a A) A
- func Let[S1, S2, T any](key func(T) func(S1) S2, f func(S1) T) func(S1) S2
- func LetTo[S1, S2, B any](key func(B) func(S1) S2, b B) func(S1) S2
- func MapTo[A, B any](b B) func(A) B
- func Monad[A, B any]() monad.Monad[A, B, A, B, func(A) B]
- func MonadAp[B, A any](fab func(A) B, fa A) B
- func MonadChain[A, B any](ma A, f Kleisli[A, B]) B
- func MonadChainFirst[A, B any](fa A, f Kleisli[A, B]) A
- func MonadFlap[B, A any](fab func(A) B, a A) B
- func MonadMap[A, B any](fa A, f func(A) B) B
- func MonadMapTo[A, B any](_ A, b B) B
- func Of[A any](a A) A
- func SequenceT1[T1 any](t1 T1) T.Tuple1[T1]
- func SequenceT10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10 any](t1 T1, t2 T2, t3 T3, t4 T4, t5 T5, t6 T6, t7 T7, t8 T8, t9 T9, t10 T10) T.Tuple10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]
- func SequenceT2[T1, T2 any](t1 T1, t2 T2) T.Tuple2[T1, T2]
- func SequenceT3[T1, T2, T3 any](t1 T1, t2 T2, t3 T3) T.Tuple3[T1, T2, T3]
- func SequenceT4[T1, T2, T3, T4 any](t1 T1, t2 T2, t3 T3, t4 T4) T.Tuple4[T1, T2, T3, T4]
- func SequenceT5[T1, T2, T3, T4, T5 any](t1 T1, t2 T2, t3 T3, t4 T4, t5 T5) T.Tuple5[T1, T2, T3, T4, T5]
- func SequenceT6[T1, T2, T3, T4, T5, T6 any](t1 T1, t2 T2, t3 T3, t4 T4, t5 T5, t6 T6) T.Tuple6[T1, T2, T3, T4, T5, T6]
- func SequenceT7[T1, T2, T3, T4, T5, T6, T7 any](t1 T1, t2 T2, t3 T3, t4 T4, t5 T5, t6 T6, t7 T7) T.Tuple7[T1, T2, T3, T4, T5, T6, T7]
- func SequenceT8[T1, T2, T3, T4, T5, T6, T7, T8 any](t1 T1, t2 T2, t3 T3, t4 T4, t5 T5, t6 T6, t7 T7, t8 T8) T.Tuple8[T1, T2, T3, T4, T5, T6, T7, T8]
- func SequenceT9[T1, T2, T3, T4, T5, T6, T7, T8, T9 any](t1 T1, t2 T2, t3 T3, t4 T4, t5 T5, t6 T6, t7 T7, t8 T8, t9 T9) T.Tuple9[T1, T2, T3, T4, T5, T6, T7, T8, T9]
- func SequenceTuple1[T1 any](t T.Tuple1[T1]) T.Tuple1[T1]
- func SequenceTuple10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10 any](t T.Tuple10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]) T.Tuple10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]
- func SequenceTuple2[T1, T2 any](t T.Tuple2[T1, T2]) T.Tuple2[T1, T2]
- func SequenceTuple3[T1, T2, T3 any](t T.Tuple3[T1, T2, T3]) T.Tuple3[T1, T2, T3]
- func SequenceTuple4[T1, T2, T3, T4 any](t T.Tuple4[T1, T2, T3, T4]) T.Tuple4[T1, T2, T3, T4]
- func SequenceTuple5[T1, T2, T3, T4, T5 any](t T.Tuple5[T1, T2, T3, T4, T5]) T.Tuple5[T1, T2, T3, T4, T5]
- func SequenceTuple6[T1, T2, T3, T4, T5, T6 any](t T.Tuple6[T1, T2, T3, T4, T5, T6]) T.Tuple6[T1, T2, T3, T4, T5, T6]
- func SequenceTuple7[T1, T2, T3, T4, T5, T6, T7 any](t T.Tuple7[T1, T2, T3, T4, T5, T6, T7]) T.Tuple7[T1, T2, T3, T4, T5, T6, T7]
- func SequenceTuple8[T1, T2, T3, T4, T5, T6, T7, T8 any](t T.Tuple8[T1, T2, T3, T4, T5, T6, T7, T8]) T.Tuple8[T1, T2, T3, T4, T5, T6, T7, T8]
- func SequenceTuple9[T1, T2, T3, T4, T5, T6, T7, T8, T9 any](t T.Tuple9[T1, T2, T3, T4, T5, T6, T7, T8, T9]) T.Tuple9[T1, T2, T3, T4, T5, T6, T7, T8, T9]
- func TraverseTuple1[F1 ~func(A1) T1, A1, T1 any](f1 F1) func(T.Tuple1[A1]) T.Tuple1[T1]
- func TraverseTuple10[F1 ~func(A1) T1, F2 ~func(A2) T2, F3 ~func(A3) T3, F4 ~func(A4) T4, ...](f1 F1, f2 F2, f3 F3, f4 F4, f5 F5, f6 F6, f7 F7, f8 F8, f9 F9, f10 F10) ...
- func TraverseTuple2[F1 ~func(A1) T1, F2 ~func(A2) T2, A1, T1, A2, T2 any](f1 F1, f2 F2) func(T.Tuple2[A1, A2]) T.Tuple2[T1, T2]
- func TraverseTuple3[F1 ~func(A1) T1, F2 ~func(A2) T2, F3 ~func(A3) T3, A1, T1, A2, T2, A3, T3 any](f1 F1, f2 F2, f3 F3) func(T.Tuple3[A1, A2, A3]) T.Tuple3[T1, T2, T3]
- func TraverseTuple4[F1 ~func(A1) T1, F2 ~func(A2) T2, F3 ~func(A3) T3, F4 ~func(A4) T4, ...](f1 F1, f2 F2, f3 F3, f4 F4) func(T.Tuple4[A1, A2, A3, A4]) T.Tuple4[T1, T2, T3, T4]
- func TraverseTuple5[F1 ~func(A1) T1, F2 ~func(A2) T2, F3 ~func(A3) T3, F4 ~func(A4) T4, ...](f1 F1, f2 F2, f3 F3, f4 F4, f5 F5) func(T.Tuple5[A1, A2, A3, A4, A5]) T.Tuple5[T1, T2, T3, T4, T5]
- func TraverseTuple6[F1 ~func(A1) T1, F2 ~func(A2) T2, F3 ~func(A3) T3, F4 ~func(A4) T4, ...](f1 F1, f2 F2, f3 F3, f4 F4, f5 F5, f6 F6) func(T.Tuple6[A1, A2, A3, A4, A5, A6]) T.Tuple6[T1, T2, T3, T4, T5, T6]
- func TraverseTuple7[F1 ~func(A1) T1, F2 ~func(A2) T2, F3 ~func(A3) T3, F4 ~func(A4) T4, ...](f1 F1, f2 F2, f3 F3, f4 F4, f5 F5, f6 F6, f7 F7) ...
- func TraverseTuple8[F1 ~func(A1) T1, F2 ~func(A2) T2, F3 ~func(A3) T3, F4 ~func(A4) T4, ...](f1 F1, f2 F2, f3 F3, f4 F4, f5 F5, f6 F6, f7 F7, f8 F8) ...
- func TraverseTuple9[F1 ~func(A1) T1, F2 ~func(A2) T2, F3 ~func(A3) T3, F4 ~func(A4) T4, ...](f1 F1, f2 F2, f3 F3, f4 F4, f5 F5, f6 F6, f7 F7, f8 F8, f9 F9) ...
- type Kleisli
- type Operator
- func Ap[B, A any](fa A) Operator[func(A) B, B]
- func Chain[A, B any](f Kleisli[A, B]) Operator[A, B]
- func ChainFirst[A, B any](f Kleisli[A, B]) Operator[A, A]
- func Extend[A, B any](f func(A) B) Operator[A, B]
- func Flap[B, A any](a A) Operator[func(A) B, B]
- func Map[A, B any](f func(A) B) Operator[A, B]
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ApS ¶
func ApS[S1, S2, T any]( setter func(T) func(S1) S2, fa T, ) func(S1) S2
ApS attaches a value to a context [S1] to produce a context [S2] by considering the context and the value concurrently (using Applicative rather than Monad). This allows independent computations to be combined without one depending on the result of the other.
Unlike Bind, which sequences operations, ApS can be used when operations are independent and can conceptually run in parallel.
Example:
type State struct {
X int
Y int
}
// These operations are independent and can be combined with ApS
result := F.Pipe2(
identity.Do(State{}),
identity.ApS(
func(x int) func(State) State {
return func(s State) State { s.X = x; return s }
},
42,
),
identity.ApS(
func(y int) func(State) State {
return func(s State) State { s.Y = y; return s }
},
100,
),
) // State{X: 42, Y: 100}
func Bind ¶
func Bind[S1, S2, T any]( setter func(T) func(S1) S2, f func(S1) T, ) func(S1) S2
Bind attaches the result of a computation to a context [S1] to produce a context [S2]. This enables sequential composition where each step can depend on the results of previous steps.
The setter function takes the result of the computation and returns a function that updates the context from S1 to S2.
Example:
type State struct {
X int
Y int
}
result := F.Pipe2(
identity.Do(State{}),
identity.Bind(
func(x int) func(State) State {
return func(s State) State { s.X = x; return s }
},
func(s State) int {
return 42
},
),
identity.Bind(
func(y int) func(State) State {
return func(s State) State { s.Y = y; return s }
},
func(s State) int {
// This can access s.X from the previous step
return s.X * 2
},
),
) // State{X: 42, Y: 84}
func BindTo ¶
func BindTo[S1, T any]( setter func(T) S1, ) func(T) S1
BindTo initializes a new state [S1] from a value [T]
func Do ¶
func Do[S any]( empty S, ) S
Do creates an empty context of type [S] to be used with the Bind operation. This is the starting point for do-notation style composition.
Example:
type State struct {
X int
Y int
}
result := identity.Do(State{})
func Extract ¶ added in v2.1.6
func Extract[A any](a A) A
Extract extracts the value from the Identity monad. Since Identity has no computational context, this is just the identity function.
This is the Comonad "extract" operation.
Implements the Fantasy Land Comonad specification: https://github.com/fantasyland/fantasy-land?tab=readme-ov-file#comonad
Example:
value := identity.Extract(42) // value is 42
func Let ¶
func Let[S1, S2, T any]( key func(T) func(S1) S2, f func(S1) T, ) func(S1) S2
Let attaches the result of a computation to a context [S1] to produce a context [S2]
func LetTo ¶
func LetTo[S1, S2, B any]( key func(B) func(S1) S2, b B, ) func(S1) S2
LetTo attaches the a value to a context [S1] to produce a context [S2]
func MapTo ¶
func MapTo[A, B any](b B) func(A) B
MapTo replaces any value with a constant value. Returns a function that ignores its input and returns the constant.
This is the curried version of MonadMapTo, useful for composition with Pipe.
Example:
import F "github.com/IBM/fp-go/v2/function"
result := F.Pipe1("ignored", identity.MapTo[string](42))
// result is 42
func MonadAp ¶
func MonadAp[B, A any](fab func(A) B, fa A) B
MonadAp applies a function to a value in the Identity monad context. Since Identity has no computational context, this is just function application.
This is the uncurried version of Ap.
Implements the Fantasy Land Apply specification: https://github.com/fantasyland/fantasy-land?tab=readme-ov-file#apply
Example:
result := identity.MonadAp(func(n int) int { return n * 2 }, 21)
// result is 42
func MonadChain ¶
MonadChain applies a Kleisli arrow to a value in the Identity monad context. Since Identity has no computational context, this is just function application.
This is the uncurried version of Chain, also known as "bind" or "flatMap".
Implements the Fantasy Land Chain specification: https://github.com/fantasyland/fantasy-land?tab=readme-ov-file#chain
Example:
result := identity.MonadChain(21, func(n int) int { return n * 2 })
// result is 42
func MonadChainFirst ¶
MonadChainFirst executes a computation for its effect but returns the original value. Useful for side effects like logging while preserving the original value.
This is the uncurried version of ChainFirst.
Example:
result := identity.MonadChainFirst(42, func(n int) string {
fmt.Printf("Value: %d\n", n)
return "logged"
})
// result is 42 (original value preserved)
func MonadFlap ¶
func MonadFlap[B, A any](fab func(A) B, a A) B
MonadFlap applies a value to a function, flipping the normal application order. Instead of applying a function to a value, it applies a value to a function.
This is the uncurried version of Flap.
Example:
double := func(n int) int { return n * 2 }
result := identity.MonadFlap(double, 21)
// result is 42
func MonadMap ¶
func MonadMap[A, B any](fa A, f func(A) B) B
MonadMap transforms a value using a function in the Identity monad context. Since Identity has no computational context, this is just function application.
This is the uncurried version of Map.
Implements the Fantasy Land Functor specification: https://github.com/fantasyland/fantasy-land?tab=readme-ov-file#functor
Example:
result := identity.MonadMap(21, func(n int) int { return n * 2 })
// result is 42
func MonadMapTo ¶
func MonadMapTo[A, B any](_ A, b B) B
MonadMapTo replaces a value with a constant, ignoring the input.
This is the uncurried version of MapTo.
Example:
result := identity.MonadMapTo("ignored", 42)
// result is 42
func Of ¶
func Of[A any](a A) A
Of wraps a value in the Identity monad. Since Identity has no computational context, this is just the identity function.
This is the Pointed/Applicative "pure" operation.
Implements the Fantasy Land Applicative specification: https://github.com/fantasyland/fantasy-land?tab=readme-ov-file#applicative
Example:
value := identity.Of(42) // value is 42
func SequenceT1 ¶
SequenceT1 converts 1 parameters of [T] into a [Tuple1].
func SequenceT10 ¶
func SequenceT10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10 any](t1 T1, t2 T2, t3 T3, t4 T4, t5 T5, t6 T6, t7 T7, t8 T8, t9 T9, t10 T10) T.Tuple10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]
SequenceT10 converts 10 parameters of [T] into a [Tuple10].
func SequenceT2 ¶
SequenceT2 converts 2 parameters of [T] into a [Tuple2].
func SequenceT3 ¶
SequenceT3 converts 3 parameters of [T] into a [Tuple3].
func SequenceT4 ¶
SequenceT4 converts 4 parameters of [T] into a [Tuple4].
func SequenceT5 ¶
func SequenceT5[T1, T2, T3, T4, T5 any](t1 T1, t2 T2, t3 T3, t4 T4, t5 T5) T.Tuple5[T1, T2, T3, T4, T5]
SequenceT5 converts 5 parameters of [T] into a [Tuple5].
func SequenceT6 ¶
func SequenceT6[T1, T2, T3, T4, T5, T6 any](t1 T1, t2 T2, t3 T3, t4 T4, t5 T5, t6 T6) T.Tuple6[T1, T2, T3, T4, T5, T6]
SequenceT6 converts 6 parameters of [T] into a [Tuple6].
func SequenceT7 ¶
func SequenceT7[T1, T2, T3, T4, T5, T6, T7 any](t1 T1, t2 T2, t3 T3, t4 T4, t5 T5, t6 T6, t7 T7) T.Tuple7[T1, T2, T3, T4, T5, T6, T7]
SequenceT7 converts 7 parameters of [T] into a [Tuple7].
func SequenceT8 ¶
func SequenceT8[T1, T2, T3, T4, T5, T6, T7, T8 any](t1 T1, t2 T2, t3 T3, t4 T4, t5 T5, t6 T6, t7 T7, t8 T8) T.Tuple8[T1, T2, T3, T4, T5, T6, T7, T8]
SequenceT8 converts 8 parameters of [T] into a [Tuple8].
func SequenceT9 ¶
func SequenceT9[T1, T2, T3, T4, T5, T6, T7, T8, T9 any](t1 T1, t2 T2, t3 T3, t4 T4, t5 T5, t6 T6, t7 T7, t8 T8, t9 T9) T.Tuple9[T1, T2, T3, T4, T5, T6, T7, T8, T9]
SequenceT9 converts 9 parameters of [T] into a [Tuple9].
func SequenceTuple1 ¶
SequenceTuple1 converts a [Tuple1] of [T] into an [Tuple1].
func SequenceTuple10 ¶
func SequenceTuple10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10 any](t T.Tuple10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]) T.Tuple10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]
SequenceTuple10 converts a [Tuple10] of [T] into an [Tuple10].
func SequenceTuple2 ¶
SequenceTuple2 converts a [Tuple2] of [T] into an [Tuple2].
func SequenceTuple3 ¶
SequenceTuple3 converts a [Tuple3] of [T] into an [Tuple3].
func SequenceTuple4 ¶
SequenceTuple4 converts a [Tuple4] of [T] into an [Tuple4].
func SequenceTuple5 ¶
func SequenceTuple5[T1, T2, T3, T4, T5 any](t T.Tuple5[T1, T2, T3, T4, T5]) T.Tuple5[T1, T2, T3, T4, T5]
SequenceTuple5 converts a [Tuple5] of [T] into an [Tuple5].
func SequenceTuple6 ¶
func SequenceTuple6[T1, T2, T3, T4, T5, T6 any](t T.Tuple6[T1, T2, T3, T4, T5, T6]) T.Tuple6[T1, T2, T3, T4, T5, T6]
SequenceTuple6 converts a [Tuple6] of [T] into an [Tuple6].
func SequenceTuple7 ¶
func SequenceTuple7[T1, T2, T3, T4, T5, T6, T7 any](t T.Tuple7[T1, T2, T3, T4, T5, T6, T7]) T.Tuple7[T1, T2, T3, T4, T5, T6, T7]
SequenceTuple7 converts a [Tuple7] of [T] into an [Tuple7].
func SequenceTuple8 ¶
func SequenceTuple8[T1, T2, T3, T4, T5, T6, T7, T8 any](t T.Tuple8[T1, T2, T3, T4, T5, T6, T7, T8]) T.Tuple8[T1, T2, T3, T4, T5, T6, T7, T8]
SequenceTuple8 converts a [Tuple8] of [T] into an [Tuple8].
func SequenceTuple9 ¶
func SequenceTuple9[T1, T2, T3, T4, T5, T6, T7, T8, T9 any](t T.Tuple9[T1, T2, T3, T4, T5, T6, T7, T8, T9]) T.Tuple9[T1, T2, T3, T4, T5, T6, T7, T8, T9]
SequenceTuple9 converts a [Tuple9] of [T] into an [Tuple9].
func TraverseTuple1 ¶
TraverseTuple1 converts a [Tuple1] of [A] via transformation functions transforming [A] to [A] into a [Tuple1].
func TraverseTuple10 ¶
func TraverseTuple10[F1 ~func(A1) T1, F2 ~func(A2) T2, F3 ~func(A3) T3, F4 ~func(A4) T4, F5 ~func(A5) T5, F6 ~func(A6) T6, F7 ~func(A7) T7, F8 ~func(A8) T8, F9 ~func(A9) T9, F10 ~func(A10) T10, A1, T1, A2, T2, A3, T3, A4, T4, A5, T5, A6, T6, A7, T7, A8, T8, A9, T9, A10, T10 any](f1 F1, f2 F2, f3 F3, f4 F4, f5 F5, f6 F6, f7 F7, f8 F8, f9 F9, f10 F10) func(T.Tuple10[A1, A2, A3, A4, A5, A6, A7, A8, A9, A10]) T.Tuple10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]
TraverseTuple10 converts a [Tuple10] of [A] via transformation functions transforming [A] to [A] into a [Tuple10].
func TraverseTuple2 ¶
func TraverseTuple2[F1 ~func(A1) T1, F2 ~func(A2) T2, A1, T1, A2, T2 any](f1 F1, f2 F2) func(T.Tuple2[A1, A2]) T.Tuple2[T1, T2]
TraverseTuple2 converts a [Tuple2] of [A] via transformation functions transforming [A] to [A] into a [Tuple2].
func TraverseTuple3 ¶
func TraverseTuple3[F1 ~func(A1) T1, F2 ~func(A2) T2, F3 ~func(A3) T3, A1, T1, A2, T2, A3, T3 any](f1 F1, f2 F2, f3 F3) func(T.Tuple3[A1, A2, A3]) T.Tuple3[T1, T2, T3]
TraverseTuple3 converts a [Tuple3] of [A] via transformation functions transforming [A] to [A] into a [Tuple3].
func TraverseTuple4 ¶
func TraverseTuple4[F1 ~func(A1) T1, F2 ~func(A2) T2, F3 ~func(A3) T3, F4 ~func(A4) T4, A1, T1, A2, T2, A3, T3, A4, T4 any](f1 F1, f2 F2, f3 F3, f4 F4) func(T.Tuple4[A1, A2, A3, A4]) T.Tuple4[T1, T2, T3, T4]
TraverseTuple4 converts a [Tuple4] of [A] via transformation functions transforming [A] to [A] into a [Tuple4].
func TraverseTuple5 ¶
func TraverseTuple5[F1 ~func(A1) T1, F2 ~func(A2) T2, F3 ~func(A3) T3, F4 ~func(A4) T4, F5 ~func(A5) T5, A1, T1, A2, T2, A3, T3, A4, T4, A5, T5 any](f1 F1, f2 F2, f3 F3, f4 F4, f5 F5) func(T.Tuple5[A1, A2, A3, A4, A5]) T.Tuple5[T1, T2, T3, T4, T5]
TraverseTuple5 converts a [Tuple5] of [A] via transformation functions transforming [A] to [A] into a [Tuple5].
func TraverseTuple6 ¶
func TraverseTuple6[F1 ~func(A1) T1, F2 ~func(A2) T2, F3 ~func(A3) T3, F4 ~func(A4) T4, F5 ~func(A5) T5, F6 ~func(A6) T6, A1, T1, A2, T2, A3, T3, A4, T4, A5, T5, A6, T6 any](f1 F1, f2 F2, f3 F3, f4 F4, f5 F5, f6 F6) func(T.Tuple6[A1, A2, A3, A4, A5, A6]) T.Tuple6[T1, T2, T3, T4, T5, T6]
TraverseTuple6 converts a [Tuple6] of [A] via transformation functions transforming [A] to [A] into a [Tuple6].
func TraverseTuple7 ¶
func TraverseTuple7[F1 ~func(A1) T1, F2 ~func(A2) T2, F3 ~func(A3) T3, F4 ~func(A4) T4, F5 ~func(A5) T5, F6 ~func(A6) T6, F7 ~func(A7) T7, A1, T1, A2, T2, A3, T3, A4, T4, A5, T5, A6, T6, A7, T7 any](f1 F1, f2 F2, f3 F3, f4 F4, f5 F5, f6 F6, f7 F7) func(T.Tuple7[A1, A2, A3, A4, A5, A6, A7]) T.Tuple7[T1, T2, T3, T4, T5, T6, T7]
TraverseTuple7 converts a [Tuple7] of [A] via transformation functions transforming [A] to [A] into a [Tuple7].
func TraverseTuple8 ¶
func TraverseTuple8[F1 ~func(A1) T1, F2 ~func(A2) T2, F3 ~func(A3) T3, F4 ~func(A4) T4, F5 ~func(A5) T5, F6 ~func(A6) T6, F7 ~func(A7) T7, F8 ~func(A8) T8, A1, T1, A2, T2, A3, T3, A4, T4, A5, T5, A6, T6, A7, T7, A8, T8 any](f1 F1, f2 F2, f3 F3, f4 F4, f5 F5, f6 F6, f7 F7, f8 F8) func(T.Tuple8[A1, A2, A3, A4, A5, A6, A7, A8]) T.Tuple8[T1, T2, T3, T4, T5, T6, T7, T8]
TraverseTuple8 converts a [Tuple8] of [A] via transformation functions transforming [A] to [A] into a [Tuple8].
func TraverseTuple9 ¶
func TraverseTuple9[F1 ~func(A1) T1, F2 ~func(A2) T2, F3 ~func(A3) T3, F4 ~func(A4) T4, F5 ~func(A5) T5, F6 ~func(A6) T6, F7 ~func(A7) T7, F8 ~func(A8) T8, F9 ~func(A9) T9, A1, T1, A2, T2, A3, T3, A4, T4, A5, T5, A6, T6, A7, T7, A8, T8, A9, T9 any](f1 F1, f2 F2, f3 F3, f4 F4, f5 F5, f6 F6, f7 F7, f8 F8, f9 F9) func(T.Tuple9[A1, A2, A3, A4, A5, A6, A7, A8, A9]) T.Tuple9[T1, T2, T3, T4, T5, T6, T7, T8, T9]
TraverseTuple9 converts a [Tuple9] of [A] via transformation functions transforming [A] to [A] into a [Tuple9].
Types ¶
type Kleisli ¶
type Kleisli[A, B any] = func(A) B
Kleisli represents a Kleisli arrow for the Identity monad. It's simply a function from A to B, as Identity has no computational context.
type Operator ¶
Operator represents a function that transforms values. In the Identity monad, it's equivalent to Kleisli since there's no wrapping context.
func Ap ¶
func Ap[B, A any](fa A) Operator[func(A) B, B]
Ap applies a wrapped function to a wrapped value. Returns a function that takes a function and applies the value to it.
This is the curried version of MonadAp, useful for composition with Pipe.
Implements the Fantasy Land Apply specification: https://github.com/fantasyland/fantasy-land?tab=readme-ov-file#apply
Example:
import F "github.com/IBM/fp-go/v2/function"
double := func(n int) int { return n * 2 }
result := F.Pipe1(double, identity.Ap[int](21))
// result is 42
func Chain ¶
Chain applies a Kleisli arrow to a value. Returns the function itself since Identity adds no context.
This is the curried version of MonadChain, also known as "bind" or "flatMap". Useful for composition with Pipe.
Implements the Fantasy Land Chain specification: https://github.com/fantasyland/fantasy-land?tab=readme-ov-file#chain
Example:
import F "github.com/IBM/fp-go/v2/function"
result := F.Pipe1(21, identity.Chain(func(n int) int { return n * 2 }))
// result is 42
func ChainFirst ¶
ChainFirst executes a computation for its effect but returns the original value. Useful for side effects like logging while preserving the original value.
This is the curried version of MonadChainFirst, useful for composition with Pipe.
Example:
import F "github.com/IBM/fp-go/v2/function"
result := F.Pipe1(
42,
identity.ChainFirst(func(n int) string {
fmt.Printf("Value: %d\n", n)
return "logged"
}),
)
// result is 42 (original value preserved)
func Extend ¶ added in v2.1.6
func Extend[A, B any](f func(A) B) Operator[A, B]
Extend extends a computation over the Identity monad. Since Identity has no computational context, this is just function application.
This is the Comonad "extend" operation, also known as "cobind".
Implements the Fantasy Land Extend specification: https://github.com/fantasyland/fantasy-land?tab=readme-ov-file#extend
Example:
import F "github.com/IBM/fp-go/v2/function"
result := F.Pipe1(21, identity.Extend(func(n int) int { return n * 2 }))
// result is 42
func Flap ¶
func Flap[B, A any](a A) Operator[func(A) B, B]
Flap applies a value to a function, flipping the normal application order. Returns a function that takes a function and applies the value to it.
This is the curried version of MonadFlap, useful for composition with Pipe. Useful when you have a value and want to apply it to multiple functions.
Example:
import F "github.com/IBM/fp-go/v2/function"
double := func(n int) int { return n * 2 }
result := F.Pipe1(double, identity.Flap[int](21))
// result is 42
func Map ¶
func Map[A, B any](f func(A) B) Operator[A, B]
Map transforms a value using a function. Returns the function itself since Identity adds no context.
This is the curried version of MonadMap, useful for composition with Pipe.
Implements the Fantasy Land Functor specification: https://github.com/fantasyland/fantasy-land?tab=readme-ov-file#functor
Example:
import F "github.com/IBM/fp-go/v2/function"
result := F.Pipe1(21, identity.Map(func(n int) int { return n * 2 }))
// result is 42