identity

package
v2.0.4 Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2025 License: Apache-2.0 Imports: 6 Imported by: 0

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:

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
  • function: Function composition utilities
  • monad: Monad interface definition
  • tuple: Tuple types for sequence operations

Index

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

func Monad

func Monad[A, B any]() monad.Monad[A, B, A, B, func(A) B]

Monad implements the monadic operations for [Option]

func MonadAp

func MonadAp[B, A any](fab func(A) B, fa A) B

func MonadChain

func MonadChain[A, B any](ma A, f Kleisli[A, B]) B

func MonadChainFirst

func MonadChainFirst[A, B any](fa A, f Kleisli[A, B]) A

func MonadFlap

func MonadFlap[B, A any](fab func(A) B, a A) B

func MonadMap

func MonadMap[A, B any](fa A, f func(A) B) B

func MonadMapTo

func MonadMapTo[A, B any](_ A, b B) B

func Of

func Of[A any](a A) A

func SequenceT1

func SequenceT1[T1 any](t1 T1) T.Tuple1[T1]

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

func SequenceT2[T1, T2 any](t1 T1, t2 T2) T.Tuple2[T1, T2]

SequenceT2 converts 2 parameters of [T] into a [Tuple2].

func SequenceT3

func SequenceT3[T1, T2, T3 any](t1 T1, t2 T2, t3 T3) T.Tuple3[T1, T2, T3]

SequenceT3 converts 3 parameters of [T] into a [Tuple3].

func SequenceT4

func SequenceT4[T1, T2, T3, T4 any](t1 T1, t2 T2, t3 T3, t4 T4) T.Tuple4[T1, T2, T3, T4]

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

func SequenceTuple1[T1 any](t T.Tuple1[T1]) T.Tuple1[T1]

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

func SequenceTuple2[T1, T2 any](t T.Tuple2[T1, T2]) T.Tuple2[T1, T2]

SequenceTuple2 converts a [Tuple2] of [T] into an [Tuple2].

func SequenceTuple3

func SequenceTuple3[T1, T2, T3 any](t T.Tuple3[T1, T2, T3]) T.Tuple3[T1, T2, T3]

SequenceTuple3 converts a [Tuple3] of [T] into an [Tuple3].

func SequenceTuple4

func SequenceTuple4[T1, T2, T3, T4 any](t T.Tuple4[T1, T2, T3, T4]) T.Tuple4[T1, T2, T3, T4]

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

func TraverseTuple1[F1 ~func(A1) T1, A1, T1 any](f1 F1) func(T.Tuple1[A1]) T.Tuple1[T1]

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

type Operator

type Operator[A, B any] = Kleisli[A, B]

func Ap

func Ap[B, A any](fa A) Operator[func(A) B, B]

func Chain

func Chain[A, B any](f Kleisli[A, B]) Operator[A, B]

func ChainFirst

func ChainFirst[A, B any](f Kleisli[A, B]) Operator[A, A]

func Flap

func Flap[B, A any](a A) Operator[func(A) B, B]

func Map

func Map[A, B any](f func(A) B) Operator[A, B]

Jump to

Keyboard shortcuts

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