generic

package
v2.0.0 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Ap

func Ap[GEA ~func(E) O.Option[A], GEB ~func(E) O.Option[B], GEFAB ~func(E) O.Option[func(A) B], E, A, B any](fa GEA) func(GEFAB) GEB

func ApS

func ApS[GS1 ~func(R) O.Option[S1], GS2 ~func(R) O.Option[S2], GT ~func(R) O.Option[T], R, S1, S2, T any](
	setter func(T) func(S1) S2,
	fa GT,
) func(GS1) GS2

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 {
    Config Config
    User   User
}
type Env struct {
    ConfigService ConfigService
    UserService   UserService
}

// These operations are independent and can be combined with ApS
getConfig := func(env Env) either.Either[error, Config] {
    return env.ConfigService.Load()
}
getUser := func(env Env) either.Either[error, User] {
    return env.UserService.GetCurrent()
}

result := F.Pipe2(
    generic.Do[ReaderEither[Env, error, State], Env, error, State](State{}),
    generic.ApS[...](
        func(cfg Config) func(State) State {
            return func(s State) State { s.Config = cfg; return s }
        },
        getConfig,
    ),
    generic.ApS[...](
        func(user User) func(State) State {
            return func(s State) State { s.User = user; return s }
        },
        getUser,
    ),
)

func Ask

func Ask[GEE ~func(E) O.Option[E], E any]() GEE

func Asks

func Asks[GA ~func(E) A, GEA ~func(E) O.Option[A], E, A any](r GA) GEA

func Bind

func Bind[GS1 ~func(R) O.Option[S1], GS2 ~func(R) O.Option[S2], GT ~func(R) O.Option[T], R, S1, S2, T any](
	setter func(T) func(S1) S2,
	f func(S1) GT,
) func(GS1) GS2

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 and access the shared environment.

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 {
    Config Config
    User   User
}
type Env struct {
    ConfigService ConfigService
    UserService   UserService
}

result := F.Pipe2(
    generic.Do[ReaderEither[Env, error, State], Env, error, State](State{}),
    generic.Bind[ReaderEither[Env, error, State], ReaderEither[Env, error, State], ReaderEither[Env, error, Config], Env, error, State, State, Config](
        func(cfg Config) func(State) State {
            return func(s State) State { s.Config = cfg; return s }
        },
        func(s State) ReaderEither[Env, error, Config] {
            return func(env Env) either.Either[error, Config] {
                return env.ConfigService.Load()
            }
        },
    ),
    generic.Bind[ReaderEither[Env, error, State], ReaderEither[Env, error, State], ReaderEither[Env, error, User], Env, error, State, State, User](
        func(user User) func(State) State {
            return func(s State) State { s.User = user; return s }
        },
        func(s State) ReaderEither[Env, error, User] {
            // This can access s.Config from the previous step
            return func(env Env) either.Either[error, User] {
                return env.UserService.GetUserForConfig(s.Config)
            }
        },
    ),
)

func BindTo

func BindTo[GS1 ~func(R) O.Option[S1], GT ~func(R) O.Option[T], R, S1, T any](
	setter func(T) S1,
) func(GT) GS1

BindTo initializes a new state [S1] from a value [T]

func Chain

func Chain[GEA ~func(E) O.Option[A], GEB ~func(E) O.Option[B], E, A, B any](f func(A) GEB) func(GEA) GEB

func ChainOptionK

func ChainOptionK[GEA ~func(E) O.Option[A], GEB ~func(E) O.Option[B], E, A, B any](f func(A) O.Option[B]) func(ma GEA) GEB

func Curry0

func Curry0[GEA ~func(R) O.Option[A], R, A any](f func(R) (A, bool)) GEA

func Curry1

func Curry1[GEA ~func(R) O.Option[A], R, T1, A any](f func(R, T1) (A, bool)) func(T1) GEA

func Curry2

func Curry2[GEA ~func(R) O.Option[A], R, T1, T2, A any](f func(R, T1, T2) (A, bool)) func(T1) func(T2) GEA

func Curry3

func Curry3[GEA ~func(R) O.Option[A], R, T1, T2, T3, A any](f func(R, T1, T2, T3) (A, bool)) func(T1) func(T2) func(T3) GEA

func Do

func Do[GS ~func(R) O.Option[S], R, S any](
	empty S,
) GS

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 {
    Config Config
    User   User
}
type Env struct {
    ConfigService ConfigService
    UserService   UserService
}
result := generic.Do[ReaderEither[Env, error, State], Env, error, State](State{})

func Flap

func Flap[GEFAB ~func(E) O.Option[func(A) B], GEB ~func(E) O.Option[B], E, A, B any](a A) func(GEFAB) GEB

func Flatten

func Flatten[GEA ~func(E) O.Option[A], GGA ~func(E) O.Option[GEA], E, A any](mma GGA) GEA

func Fold

func Fold[GEA ~func(E) O.Option[A], GB ~func(E) B, E, A, B any](onNone func() GB, onRight func(A) GB) func(GEA) GB

func From0

func From0[GEA ~func(R) O.Option[A], R, A any](f func(R) (A, bool)) func() GEA

func From1

func From1[GEA ~func(R) O.Option[A], R, T1, A any](f func(R, T1) (A, bool)) func(T1) GEA

func From2

func From2[GEA ~func(R) O.Option[A], R, T1, T2, A any](f func(R, T1, T2) (A, bool)) func(T1, T2) GEA

func From3

func From3[GEA ~func(R) O.Option[A], R, T1, T2, T3, A any](f func(R, T1, T2, T3) (A, bool)) func(T1, T2, T3) GEA

func FromOption

func FromOption[GEA ~func(E) O.Option[A], E, A any](e O.Option[A]) GEA

func FromPredicate

func FromPredicate[GEA ~func(E) O.Option[A], E, A any](pred func(A) bool) func(A) GEA

func FromReader

func FromReader[GA ~func(E) A, GEA ~func(E) O.Option[A], E, A any](r GA) GEA

func GetOrElse

func GetOrElse[GEA ~func(E) O.Option[A], GA ~func(E) A, E, A any](onNone func() GA) func(GEA) GA

func Let

func Let[GS1 ~func(R) O.Option[S1], GS2 ~func(R) O.Option[S2], R, S1, S2, T any](
	key func(T) func(S1) S2,
	f func(S1) T,
) func(GS1) GS2

Let attaches the result of a computation to a context [S1] to produce a context [S2]

func LetTo

func LetTo[GS1 ~func(R) O.Option[S1], GS2 ~func(R) O.Option[S2], R, S1, S2, B any](
	key func(B) func(S1) S2,
	b B,
) func(GS1) GS2

LetTo attaches the a value to a context [S1] to produce a context [S2]

func Local

func Local[GA1 ~func(R1) O.Option[A], GA2 ~func(R2) O.Option[A], R2, R1, E, A any](f func(R2) R1) func(GA1) GA2

Local changes the value of the local context during the execution of the action `ma` (similar to `Contravariant`'s `contramap`).

func MakeReaderOption

func MakeReaderOption[GEA ~func(E) O.Option[A], E, A any](f func(E) O.Option[A]) GEA

func Map

func Map[GEA ~func(E) O.Option[A], GEB ~func(E) O.Option[B], E, A, B any](f func(A) B) func(GEA) GEB

func MonadAp

func MonadAp[GEA ~func(E) O.Option[A], GEB ~func(E) O.Option[B], GEFAB ~func(E) O.Option[func(A) B], E, A, B any](fab GEFAB, fa GEA) GEB

func MonadChain

func MonadChain[GEA ~func(E) O.Option[A], GEB ~func(E) O.Option[B], E, A, B any](ma GEA, f func(A) GEB) GEB

func MonadChainOptionK

func MonadChainOptionK[GEA ~func(E) O.Option[A], GEB ~func(E) O.Option[B], E, A, B any](ma GEA, f func(A) O.Option[B]) GEB

func MonadFlap

func MonadFlap[GEFAB ~func(E) O.Option[func(A) B], GEB ~func(E) O.Option[B], E, A, B any](fab GEFAB, a A) GEB

func MonadMap

func MonadMap[GEA ~func(E) O.Option[A], GEB ~func(E) O.Option[B], E, A, B any](fa GEA, f func(A) B) GEB

func MonadTraverseArray

func MonadTraverseArray[GB ~func(E) O.Option[B], GBS ~func(E) O.Option[BBS], AAS ~[]A, BBS ~[]B, E, A, B any](ma AAS, f func(A) GB) GBS

MonadTraverseArray transforms an array

func Of

func Of[GEA ~func(E) O.Option[A], E, A any](a A) GEA

func SequenceArray

func SequenceArray[GA ~func(E) O.Option[A], GAS ~func(E) O.Option[AAS], AAS ~[]A, GAAS ~[]GA, E, A any](ma GAAS) GAS

SequenceArray converts a homogeneous sequence of either into an either of sequence

func SequenceT1

func SequenceT1[
	GA ~func(E) O.Option[A],
	GTA ~func(E) O.Option[T.Tuple1[A]],
	E, A any](a GA) GTA

func SequenceT2

func SequenceT2[
	GA ~func(E) O.Option[A],
	GB ~func(E) O.Option[B],
	GTAB ~func(E) O.Option[T.Tuple2[A, B]],
	E, A, B any](a GA, b GB) GTAB

func SequenceT3

func SequenceT3[
	GA ~func(E) O.Option[A],
	GB ~func(E) O.Option[B],
	GC ~func(E) O.Option[C],
	GTABC ~func(E) O.Option[T.Tuple3[A, B, C]],
	E, A, B, C any](a GA, b GB, c GC) GTABC

func SequenceT4

func SequenceT4[
	GA ~func(E) O.Option[A],
	GB ~func(E) O.Option[B],
	GC ~func(E) O.Option[C],
	GD ~func(E) O.Option[D],
	GTABCD ~func(E) O.Option[T.Tuple4[A, B, C, D]],
	E, A, B, C, D any](a GA, b GB, c GC, d GD) GTABCD

func Some

func Some[GEA ~func(E) O.Option[A], E, A any](r A) GEA

func SomeReader

func SomeReader[GA ~func(E) A, GEA ~func(E) O.Option[A], E, A any](r GA) GEA

func TraverseArray

func TraverseArray[GB ~func(E) O.Option[B], GBS ~func(E) O.Option[BBS], AAS ~[]A, BBS ~[]B, E, A, B any](f func(A) GB) func(AAS) GBS

TraverseArray transforms an array

func TraverseArrayWithIndex

func TraverseArrayWithIndex[GB ~func(E) O.Option[B], GBS ~func(E) O.Option[BBS], AAS ~[]A, BBS ~[]B, E, A, B any](f func(int, A) GB) func(AAS) GBS

TraverseArrayWithIndex transforms an array

func Uncurry1

func Uncurry1[GEA ~func(R) O.Option[A], R, T1, A any](f func(T1) GEA) func(R, T1) (A, bool)

func Uncurry2

func Uncurry2[GEA ~func(R) O.Option[A], R, T1, T2, A any](f func(T1) func(T2) GEA) func(R, T1, T2) (A, bool)

func Uncurry3

func Uncurry3[GEA ~func(R) O.Option[A], R, T1, T2, T3, A any](f func(T1) func(T2) func(T3) GEA) func(R, T1, T2, T3) (A, bool)

Types

This section is empty.

Jump to

Keyboard shortcuts

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