state

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: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Applicative

func Applicative[S, A, B any]() applicative.Applicative[A, B, State[S, A], State[S, B], State[S, func(A) B]]

Applicative implements the applicative operations for State

func Eq

func Eq[S, A any](w eq.Eq[S], a eq.Eq[A]) func(S) eq.Eq[State[S, A]]

Constructs an equal predicate for a State

func Evaluate

func Evaluate[A, S any](s S) func(State[S, A]) A

func Execute

func Execute[A, S any](s S) func(State[S, A]) S

func FromStrictEquals

func FromStrictEquals[S, A comparable]() func(S) eq.Eq[State[S, A]]

FromStrictEquals constructs an eq.Eq from the canonical comparison function

func Functor

func Functor[S, A, B any]() functor.Functor[A, B, State[S, A], State[S, B]]

Functor implements the functor operations for State

func Monad

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

Monad implements the monadic operations for State

func Pointed

func Pointed[S, A any]() pointed.Pointed[A, State[S, A]]

Pointed implements the pointed operations for State

Types

type Endomorphism

type Endomorphism[A any] = endomorphism.Endomorphism[A]

Endomorphism represents a function from a type to itself (A -> A). It's an alias for endomorphism.Endomorphism[A] and is commonly used for state transformations and updates.

func ApSL

func ApSL[ST, S, T any](
	lens Lens[S, T],
	fa State[ST, T],
) Endomorphism[State[ST, S]]

func BindL

func BindL[ST, S, T any](
	lens Lens[S, T],
	f Kleisli[ST, T, T],
) Endomorphism[State[ST, S]]

func LetL

func LetL[ST, S, T any](
	lens Lens[S, T],
	f Endomorphism[T],
) Endomorphism[State[ST, S]]

func LetToL

func LetToL[ST, S, T any](
	lens Lens[S, T],
	b T,
) Endomorphism[State[ST, S]]

type Kleisli

type Kleisli[S, A, B any] = Reader[A, State[S, B]]

Kleisli represents a Kleisli arrow for the State monad. It's a function from A to State[S, B], which allows composition of stateful computations. This is the fundamental building block for chaining operations that both depend on and modify state.

type Lens

type Lens[S, A any] = lens.Lens[S, A]

Lens represents a functional reference to a part of a data structure. It's an alias for lens.Lens[S, A] where S is the whole structure and A is the part. Lenses provide composable getters and setters for immutable data structures.

type Operator

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

Operator is a specialized Kleisli arrow that operates on State values. It transforms a State[S, A] into a State[S, B], making it useful for building pipelines of stateful transformations while maintaining the state type S.

func Ap

func Ap[B, S, A any](ga State[S, A]) Operator[S, func(A) B, B]

func ApS

func ApS[ST, S1, S2, T any](
	setter func(T) func(S1) S2,
	fa State[ST, T],
) Operator[ST, S1, S2]

func Bind

func Bind[ST, S1, S2, T any](
	setter func(T) func(S1) S2,
	f Kleisli[ST, S1, T],
) Operator[ST, S1, S2]

func BindTo

func BindTo[ST, S1, T any](
	setter func(T) S1,
) Operator[ST, T, S1]

func Chain

func Chain[S any, FCT ~func(A) State[S, B], A, B any](f FCT) Operator[S, A, B]

func ChainFirst

func ChainFirst[S any, FCT ~func(A) State[S, B], A, B any](f FCT) Operator[S, A, A]

func Flap

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

func Let

func Let[ST, S1, S2, T any](
	key func(T) func(S1) S2,
	f func(S1) T,
) Operator[ST, S1, S2]

func LetTo

func LetTo[ST, S1, S2, T any](
	key func(T) func(S1) S2,
	b T,
) Operator[ST, S1, S2]

func Map

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

type Pair

type Pair[L, R any] = pair.Pair[L, R]

Pair represents a tuple of two values of types L and R. It's an alias for pair.Pair[L, R] where L is the head (left) and R is the tail (right).

type Reader

type Reader[R, A any] = reader.Reader[R, A]

Reader represents a computation that depends on an environment of type R and produces a value of type A. It's an alias for reader.Reader[R, A] and is used for dependency injection patterns.

type State

type State[S, A any] = Reader[S, pair.Pair[S, A]]

State represents a stateful computation that takes an initial state of type S, performs some operation, and returns both a new state and a value of type A. It's defined as Reader[S, Pair[S, A]], meaning it's a function that: 1. Takes an initial state S as input 2. Returns a Pair where the head is the new state S and the tail is the computed value A The new state is in the head position and the value in the tail position because the pair monad operates on the tail, allowing monadic operations to transform the computed value while threading the state through the computation.

func Do

func Do[ST, A any](
	empty A,
) State[ST, A]

func Flatten

func Flatten[S, A any](mma State[S, State[S, A]]) State[S, A]

func Get

func Get[S any]() State[S, S]

func Gets

func Gets[FCT ~func(S) A, A, S any](f FCT) State[S, A]

func Modify

func Modify[FCT ~func(S) S, S any](f FCT) State[S, any]

func MonadAp

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

func MonadChain

func MonadChain[S any, FCT ~func(A) State[S, B], A, B any](fa State[S, A], f FCT) State[S, B]

func MonadChainFirst

func MonadChainFirst[S any, FCT ~func(A) State[S, B], A, B any](ma State[S, A], f FCT) State[S, A]

func MonadFlap

func MonadFlap[FAB ~func(A) B, S, A, B any](fab State[S, FAB], a A) State[S, B]

func MonadMap

func MonadMap[S any, FCT ~func(A) B, A, B any](fa State[S, A], f FCT) State[S, B]

func Of

func Of[S, A any](a A) State[S, A]

func Put

func Put[S any]() State[S, any]

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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