chain

package
v2.0.2 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bind

func Bind[S1, S2, B, HKTS1, HKTS2, HKTB any](
	mchain func(func(S1) HKTS2) func(HKTS1) HKTS2,
	mmap func(func(B) S2) func(HKTB) HKTS2,
	key func(B) func(S1) S2,
	f func(S1) HKTB,
) func(HKTS1) HKTS2

func BindTo

func BindTo[S1, B, HKTS1, HKTB any](
	mmap func(func(B) S1) func(HKTB) HKTS1,
	key func(B) S1,
) func(fa HKTB) HKTS1

func MonadBind

func MonadBind[S1, S2, B, HKTS1, HKTS2, HKTB any](
	mchain func(HKTS1, func(S1) HKTS2) HKTS2,
	mmap func(HKTB, func(B) S2) HKTS2,
	first HKTS1,
	key func(B) func(S1) S2,
	f func(S1) HKTB,
) HKTS2

func MonadBindTo

func MonadBindTo[S1, B, HKTS1, HKTB any](
	mmap func(HKTB, func(B) S1) HKTS1,
	first HKTB,
	key func(B) S1,
) HKTS1

func MonadChain

func MonadChain[A, B, HKTA, HKTB any](
	mchain func(HKTA, Kleisli[A, HKTB]) HKTB,
	first HKTA,
	f Kleisli[A, HKTB],
) HKTB

func MonadChainFirst

func MonadChainFirst[A, B, HKTA, HKTB any](
	mchain func(HKTA, Kleisli[A, HKTA]) HKTA,
	mmap func(HKTB, func(B) A) HKTA,
	first HKTA,
	f Kleisli[A, HKTB],
) HKTA

HKTA=HKT[A] HKTB=HKT[B]

func ToApply

func ToApply[A, B, HKTA, HKTB, HKTFAB any](ap Chainable[A, B, HKTA, HKTB, HKTFAB]) apply.Apply[A, B, HKTA, HKTB, HKTFAB]

ToApply converts from Chainable to functor.Functor

func ToFunctor

func ToFunctor[A, B, HKTA, HKTB, HKTFAB any](ap Chainable[A, B, HKTA, HKTB, HKTFAB]) functor.Functor[A, B, HKTA, HKTB]

ToFunctor converts from Chainable to functor.Functor

Types

type Chainable

type Chainable[A, B, HKTA, HKTB, HKTFAB any] interface {
	apply.Apply[A, B, HKTA, HKTB, HKTFAB]

	// Chain sequences computations where the second computation depends on the
	// value produced by the first.
	//
	// Takes a function that produces a new context-wrapped value based on the
	// unwrapped input, and returns a function that applies this to a context-wrapped
	// input, flattening the nested context.
	Chain(func(A) HKTB) func(HKTA) HKTB
}

Chainable represents a type that supports sequential composition of computations, where each computation depends on the result of the previous one.

Chainable extends Apply by adding the Chain method (also known as flatMap or bind), which allows for dependent sequencing of computations. Unlike Ap, which combines independent computations, Chain allows the structure of the second computation to depend on the value produced by the first.

A Chainable must satisfy the following laws:

Associativity:

Chain(f)(Chain(g)(m)) == Chain(x => Chain(f)(g(x)))(m)

Type Parameters:

  • A: The input value type
  • B: The output value type after chaining
  • HKTA: The higher-kinded type containing A
  • HKTB: The higher-kinded type containing B
  • HKTFAB: The higher-kinded type containing a function from A to B

Example:

// Given a Chainable for Option
var c Chainable[int, string, Option[int], Option[string], Option[func(int) string]]
chainFn := c.Chain(func(x int) Option[string] {
  if x > 0 {
    return Some(strconv.Itoa(x))
  }
  return None[string]()
})
result := chainFn(Some(42)) // Returns Some("42")

type Kleisli

type Kleisli[A, HKTB any] = func(A) HKTB

type Operator

type Operator[HKTA, HKTB any] = func(HKTA) HKTB

func Chain

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

func ChainFirst

func ChainFirst[A, B, HKTA, HKTB any](
	mchain func(Kleisli[A, HKTA]) Operator[HKTA, HKTA],
	mmap func(func(B) A) func(HKTB) HKTA,
	f Kleisli[A, HKTB]) Operator[HKTA, HKTA]

HKTA=HKT[A] HKTB=HKT[B]

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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