identity

package
v2.3.96 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2026 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

Of wraps a value (but it is just the identity), Map transforms it, and Chain sequences computations. See the ExampleOf, ExampleMap, and ExampleChain functions for runnable demonstrations.

Functor Operations

Map transforms values. MapTo replaces a value with a constant. See ExampleMap and ExampleMapTo for runnable demonstrations.

Applicative Operations

Ap applies wrapped functions. See ExampleAp for a runnable demonstration.

Monad Operations

Chain composes computations sequentially. ChainFirst executes a computation for its side effect and returns the original value unchanged. See ExampleChain and ExampleChainFirst for runnable demonstrations.

Do Notation

The package provides "do notation" for imperative-style composition using Do, Bind, Let, LetTo, BindTo, and ApS. See ExampleBind, ExampleLet, ExampleBindTo, and ExampleApS for runnable demonstrations.

Sequence and Traverse

SequenceTuple and TraverseTuple convert tuples of Identity values. See ExampleSequenceTuple2 and ExampleTraverseTuple2 for runnable demonstrations.

Monad Interface

Monad returns a monad.Monad instance for use in generic code that works with any monad.

Why Identity?

The Identity monad might seem pointless, but it is 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

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

Examples

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

ExampleApS demonstrates combining independent values into a context using the Applicative (rather than Monadic) interface.

package main

import (
	"fmt"

	F "github.com/IBM/fp-go/v2/function"
	I "github.com/IBM/fp-go/v2/identity"
)

func main() {
	type State struct{ X, Y int }

	result := F.Pipe2(
		I.Do(State{}),
		I.ApS(
			func(x int) func(State) State {
				return func(s State) State { s.X = x; return s }
			},
			42,
		),
		I.ApS(
			func(y int) func(State) State {
				return func(s State) State { s.Y = y; return s }
			},
			100,
		),
	)
	fmt.Println(result)
}
Output:
{42 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

ExampleBind demonstrates sequential do-notation composition where each step can read the accumulated state.

package main

import (
	"fmt"

	F "github.com/IBM/fp-go/v2/function"
	I "github.com/IBM/fp-go/v2/identity"
)

func main() {
	type State struct{ X, Y int }

	result := F.Pipe2(
		I.Do(State{}),
		I.Bind(
			func(x int) func(State) State {
				return func(s State) State { s.X = x; return s }
			},
			func(State) int { return 42 },
		),
		I.Bind(
			func(y int) func(State) State {
				return func(s State) State { s.Y = y; return s }
			},
			func(s State) int { return s.X * 2 },
		),
	)
	fmt.Println(result)
}
Output:
{42 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. This is typically used as the first operation in a do-notation chain to convert a plain value into a context that can be used with subsequent Bind operations.

Example

ExampleBindTo demonstrates lifting an initial value into a do-notation context.

package main

import (
	"fmt"

	F "github.com/IBM/fp-go/v2/function"
	I "github.com/IBM/fp-go/v2/identity"
)

func main() {
	type State struct{ X, Y int }

	result := F.Pipe2(
		42,
		I.BindTo(func(x int) State { return State{X: x} }),
		I.Bind(
			func(y int) func(State) State {
				return func(s State) State { s.Y = y; return s }
			},
			func(s State) int { return s.X * 2 },
		),
	)
	fmt.Println(result)
}
Output:
{42 84}

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

ExampleDo demonstrates initialising a do-notation context.

package main

import (
	"fmt"

	I "github.com/IBM/fp-go/v2/identity"
)

func main() {
	type State struct{ X int }
	s := I.Do(State{})
	fmt.Println(s)
}
Output:
{0}

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

ExampleExtract demonstrates that Extract is the identity function for the Comonad interface.

package main

import (
	"fmt"

	I "github.com/IBM/fp-go/v2/identity"
)

func main() {
	value := I.Extract(42)
	fmt.Println(value)
}
Output:
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. Similar to Bind, but uses the Functor's Map operation instead of the Monad's Chain. This is useful when you want to add a computed value to the context without needing the full power of monadic composition.

Example

ExampleLet demonstrates computing a derived value from the accumulated state.

package main

import (
	"fmt"

	F "github.com/IBM/fp-go/v2/function"
	I "github.com/IBM/fp-go/v2/identity"
)

func main() {
	type State struct{ X, Y, Sum int }

	result := F.Pipe1(
		I.Do(State{X: 10, Y: 20}),
		I.Let(
			func(sum int) func(State) State {
				return func(s State) State { s.Sum = sum; return s }
			},
			func(s State) int { return s.X + s.Y },
		),
	)
	fmt.Println(result)
}
Output:
{10 20 30}

func LetTo

func LetTo[S1, S2, B any](
	key func(B) func(S1) S2,
	b B,
) func(S1) S2

LetTo attaches a constant value to a context S1 to produce a context S2. This is a specialized version of Let that doesn't require a computation function, useful when you want to add a known value to the context.

Example

ExampleLetTo demonstrates attaching a constant value to the context.

package main

import (
	"fmt"

	F "github.com/IBM/fp-go/v2/function"
	I "github.com/IBM/fp-go/v2/identity"
)

func main() {
	type State struct {
		X        int
		Constant string
	}

	result := F.Pipe1(
		I.Do(State{X: 10}),
		I.LetTo(
			func(c string) func(State) State {
				return func(s State) State { s.Constant = c; return s }
			},
			"fixed",
		),
	)
	fmt.Println(result)
}
Output:
{10 fixed}

func MakeTraversable added in v2.3.12

func MakeTraversable[A, B, HKTB any]() func(func(A) HKTB) func(A) HKTB

MakeTraversable creates a traversal function for Identity types. Since Identity has no computational context, traversing is equivalent to mapping.

This function enables traversing an Identity value by applying a transformation that produces a higher-kinded type. Because Identity is just a value with no wrapper, the traversal simply applies the transformation function directly.

Type Parameters:

  • A: The input element type
  • B: The output element type after transformation
  • HKTB: The higher-kinded type containing B (e.g., IO[B], Option[B])

Returns:

  • A function that takes a transformation function and returns a function that applies it

See Also:

  • Map: The underlying implementation (Identity traversal is just mapping)
Example

ExampleMakeTraversable demonstrates that traversing an Identity value is equivalent to mapping: the transformation function is applied directly.

package main

import (
	"fmt"
	"strconv"

	I "github.com/IBM/fp-go/v2/identity"
)

func main() {
	traverseWithItoa := I.MakeTraversable[int, string, string]()
	result := traverseWithItoa(strconv.Itoa)(42)
	fmt.Println(result)
}
Output:
42

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

ExampleMapTo demonstrates replacing any value with a constant using Pipe.

package main

import (
	"fmt"

	F "github.com/IBM/fp-go/v2/function"
	I "github.com/IBM/fp-go/v2/identity"
)

func main() {
	result := F.Pipe1("ignored", I.MapTo[string](42))
	fmt.Println(result)
}
Output:
42

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

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

ExampleMonadAp demonstrates uncurried function application in the Identity monad.

package main

import (
	"fmt"

	I "github.com/IBM/fp-go/v2/identity"
)

func main() {
	result := I.MonadAp(func(n int) int { return n * 2 }, 21)
	fmt.Println(result)
}
Output:
42

func MonadChain

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

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

ExampleMonadChain demonstrates uncurried monadic bind in the Identity monad.

package main

import (
	"fmt"

	I "github.com/IBM/fp-go/v2/identity"
)

func main() {
	result := I.MonadChain(21, func(n int) int { return n * 2 })
	fmt.Println(result)
}
Output:
42

func MonadChainFirst

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

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

ExampleMonadChainFirst demonstrates that ChainFirst executes a side-effecting computation but preserves the original value (uncurried).

package main

import (
	"fmt"
	"strconv"

	I "github.com/IBM/fp-go/v2/identity"
)

func main() {
	result := I.MonadChainFirst(42, func(n int) string {
		return strconv.Itoa(n) // side effect: produces a string, but is discarded
	})
	fmt.Println(result)
}
Output:
42

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

ExampleMonadFlap demonstrates uncurried flapped application.

package main

import (
	"fmt"

	I "github.com/IBM/fp-go/v2/identity"
)

func main() {
	double := func(n int) int { return n * 2 }
	result := I.MonadFlap(double, 21)
	fmt.Println(result)
}
Output:
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

ExampleMonadMap demonstrates uncurried value transformation in the Identity monad.

package main

import (
	"fmt"

	I "github.com/IBM/fp-go/v2/identity"
)

func main() {
	result := I.MonadMap(21, func(n int) int { return n * 2 })
	fmt.Println(result)
}
Output:
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

ExampleMonadMapTo demonstrates replacing a value with a constant (uncurried).

package main

import (
	"fmt"

	I "github.com/IBM/fp-go/v2/identity"
)

func main() {
	result := I.MonadMapTo("ignored", 42)
	fmt.Println(result)
}
Output:
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

ExampleOf demonstrates that Of is the identity function — it returns its argument unchanged.

package main

import (
	"fmt"

	I "github.com/IBM/fp-go/v2/identity"
)

func main() {
	value := I.Of(42)
	fmt.Println(value)
}
Output:
42

func SequenceT1

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

SequenceT1 converts 1 parameters of [T] into a [Tuple1].

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

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

SequenceTuple1 converts a [Tuple1] of [T] into an [Tuple1].

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

Example

ExampleSequenceTuple2 demonstrates sequencing a 2-tuple of Identity values.

package main

import (
	"fmt"

	I "github.com/IBM/fp-go/v2/identity"

	T "github.com/IBM/fp-go/v2/tuple"
)

func main() {
	tuple := T.MakeTuple2(1, 2)
	result := I.SequenceTuple2(tuple)
	fmt.Println(result)
}
Output:
Tuple2[int, int](1, 2)

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

Example

ExampleTraverseTuple2 demonstrates traversing a 2-tuple with per-element transformations.

package main

import (
	"fmt"

	I "github.com/IBM/fp-go/v2/identity"

	N "github.com/IBM/fp-go/v2/number"

	T "github.com/IBM/fp-go/v2/tuple"
)

func main() {
	result := I.TraverseTuple2(N.Mul(2), N.Mul(3))(T.MakeTuple2(1, 2))
	fmt.Println(result)
}
Output:
Tuple2[int, int](2, 6)

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

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

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

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

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

ExampleAp demonstrates applying a wrapped function to a value using Pipe.

package main

import (
	"fmt"

	F "github.com/IBM/fp-go/v2/function"
	I "github.com/IBM/fp-go/v2/identity"
)

func main() {
	double := func(n int) int { return n * 2 }
	result := F.Pipe1(double, I.Ap[int](21))
	fmt.Println(result)
}
Output:
42

func Chain

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

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

ExampleChain demonstrates sequential composition using Pipe.

package main

import (
	"fmt"

	F "github.com/IBM/fp-go/v2/function"
	I "github.com/IBM/fp-go/v2/identity"

	N "github.com/IBM/fp-go/v2/number"
)

func main() {
	result := F.Pipe2(
		10,
		I.Chain(N.Mul(2)),
		I.Chain(N.Add(5)),
	)
	fmt.Println(result)
}
Output:
25

func ChainFirst

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

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

ExampleChainFirst demonstrates executing a computation for its effect while keeping the original value.

package main

import (
	"fmt"
	"strconv"

	F "github.com/IBM/fp-go/v2/function"
	I "github.com/IBM/fp-go/v2/identity"
)

func main() {
	result := F.Pipe1(
		42,
		I.ChainFirst(func(n int) string {
			return strconv.Itoa(n) // side effect discarded
		}),
	)
	fmt.Println(result)
}
Output:
42

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

ExampleExtend demonstrates the Comonad extend operation, which is just function application.

package main

import (
	"fmt"

	F "github.com/IBM/fp-go/v2/function"
	I "github.com/IBM/fp-go/v2/identity"
)

func main() {
	result := F.Pipe1(21, I.Extend(func(n int) int { return n * 2 }))
	fmt.Println(result)
}
Output:
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

ExampleFlap demonstrates applying a fixed value to a function using Pipe.

package main

import (
	"fmt"

	F "github.com/IBM/fp-go/v2/function"
	I "github.com/IBM/fp-go/v2/identity"
)

func main() {
	double := func(n int) int { return n * 2 }
	result := F.Pipe1(double, I.Flap[int](21))
	fmt.Println(result)
}
Output:
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

ExampleMap demonstrates transforming a value using Pipe.

package main

import (
	"fmt"

	F "github.com/IBM/fp-go/v2/function"
	I "github.com/IBM/fp-go/v2/identity"
)

func main() {
	result := F.Pipe1(21, I.Map(func(n int) int { return n * 2 }))
	fmt.Println(result)
}
Output:
42

Jump to

Keyboard shortcuts

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