diff --git a/readerioeither/generic/reader.go b/readerioeither/generic/reader.go index 1bedda9..bcff0b5 100644 --- a/readerioeither/generic/reader.go +++ b/readerioeither/generic/reader.go @@ -449,3 +449,15 @@ func MonadMapLeft[GREA1 ~func(R) GEA1, GREA2 ~func(R) GEA2, GEA1 ~func() ET.Eith func MapLeft[GREA1 ~func(R) GEA1, GREA2 ~func(R) GEA2, GEA1 ~func() ET.Either[E1, A], GEA2 ~func() ET.Either[E2, A], R, E1, E2, A any](f func(E1) E2) func(GREA1) GREA2 { return F.Bind2nd(MonadMapLeft[GREA1, GREA2], f) } + +// Local changes the value of the local context during the execution of the action `ma` (similar to `Contravariant`'s +// `contramap`). +func Local[ + GEA1 ~func(R1) GIOA, + GEA2 ~func(R2) GIOA, + + GIOA ~func() ET.Either[E, A], + R1, R2, E, A any, +](f func(R2) R1) func(GEA1) GEA2 { + return RD.Local[GEA1, GEA2](f) +} diff --git a/readerioeither/reader.go b/readerioeither/reader.go index 29074b1..c128521 100644 --- a/readerioeither/reader.go +++ b/readerioeither/reader.go @@ -289,3 +289,9 @@ func MonadMapLeft[R, E1, E2, A any](fa ReaderIOEither[R, E1, A], f func(E1) E2) func MapLeft[R, A, E1, E2 any](f func(E1) E2) func(ReaderIOEither[R, E1, A]) ReaderIOEither[R, E2, A] { return G.MapLeft[ReaderIOEither[R, E1, A], ReaderIOEither[R, E2, A]](f) } + +// Local changes the value of the local context during the execution of the action `ma` (similar to `Contravariant`'s +// `contramap`). +func Local[R1, R2, E, A any](f func(R2) R1) func(ReaderIOEither[R1, E, A]) ReaderIOEither[R2, E, A] { + return G.Local[ReaderIOEither[R1, E, A], ReaderIOEither[R2, E, A]](f) +} diff --git a/statereaderioeither/generic/state.go b/statereaderioeither/generic/state.go index 7af5b8d..7249743 100644 --- a/statereaderioeither/generic/state.go +++ b/statereaderioeither/generic/state.go @@ -154,12 +154,14 @@ func Ap[ ) } +// Conversions + func FromReaderIOEither[ SRIOEA ~func(S) RIOEA, - RIOEA ~func(R) IOEA, - RIOEA_IN ~func(R) IOEA_IN, + RIOEA ~func(R) IOEA, + IOEA ~func() ET.Either[E, P.Pair[A, S]], IOEA_IN ~func() ET.Either[E, A], @@ -189,10 +191,10 @@ func FromReaderEither[ func FromIOEither[ SRIOEA ~func(S) RIOEA, RIOEA_IN ~func(R) IOEA_IN, + IOEA_IN ~func() ET.Either[E, A], RIOEA ~func(R) IOEA, IOEA ~func() ET.Either[E, P.Pair[A, S]], - IOEA_IN ~func() ET.Either[E, A], S, R, E, A any, ](fa IOEA_IN) SRIOEA { @@ -254,3 +256,170 @@ func FromState[ ](fa STATE) SRIOEA { return ST.FromState[SRIOEA](G.Of[RIOEA], fa) } + +// Combinators + +func Local[ + SR1IOEA ~func(S) R1IOEA, + SR2IOEA ~func(S) R2IOEA, + R1IOEA ~func(R1) IOEA, + R2IOEA ~func(R2) IOEA, + IOEA ~func() ET.Either[E, P.Pair[A, S]], + S, R1, R2, E, A any, +](f func(R2) R1) func(SR1IOEA) SR2IOEA { + return func(ma SR1IOEA) SR2IOEA { + return F.Flow2(ma, G.Local[R1IOEA, R2IOEA](f)) + } +} + +func Asks[ + SRIOEA ~func(S) RIOEA, + RIOEA ~func(R) IOEA, + IOEA ~func() ET.Either[E, P.Pair[A, S]], + S, R, E, A any, +](f func(R) SRIOEA) SRIOEA { + return func(s S) RIOEA { + return func(r R) IOEA { + return f(r)(s)(r) + } + } +} + +func FromIOEitherK[ + SRIOEB ~func(S) RIOEB, + RIOEB_IN ~func(R) IOEB_IN, + IOEB_IN ~func() ET.Either[E, B], + RIOEB ~func(R) IOEB, + IOEB ~func() ET.Either[E, P.Pair[B, S]], + S, R, E, A, B any, +](f func(A) IOEB_IN) func(A) SRIOEB { + return F.Flow2( + f, + FromIOEither[SRIOEB, RIOEB_IN], + ) +} + +func FromEitherK[ + SRIOEB ~func(S) RIOEB, + RIOEB ~func(R) IOEB, + IOEB ~func() ET.Either[E, P.Pair[B, S]], + S, R, E, A, B any, +](f func(A) ET.Either[E, B]) func(A) SRIOEB { + return F.Flow2( + f, + FromEither[SRIOEB], + ) +} + +func FromIOK[ + SRIOEB ~func(S) RIOEB, + RIOEB_IN ~func(R) IOEB_IN, + + IOB_IN ~func() B, + + RIOEB ~func(R) IOEB, + + IOEB ~func() ET.Either[E, P.Pair[B, S]], + IOEB_IN ~func() ET.Either[E, B], + + S, R, E, A, B any, +](f func(A) IOB_IN) func(A) SRIOEB { + return F.Flow2( + f, + FromIO[SRIOEB, RIOEB_IN, IOB_IN], + ) +} + +func FromReaderIOEitherK[ + SRIOEB ~func(S) RIOEB, + RIOEB_IN ~func(R) IOEB_IN, + IOEB_IN ~func() ET.Either[E, B], + RIOEB ~func(R) IOEB, + IOEB ~func() ET.Either[E, P.Pair[B, S]], + S, R, E, A, B any, +](f func(A) RIOEB_IN) func(A) SRIOEB { + return F.Flow2( + f, + FromReaderIOEither[SRIOEB, RIOEB_IN], + ) +} + +func MonadChainReaderIOEitherK[ + SRIOEA ~func(S) RIOEA, + SRIOEB ~func(S) RIOEB, + RIOEB_IN ~func(R) IOEB_IN, + RIOEA ~func(R) IOEA, + RIOEB ~func(R) IOEB, + IOEA ~func() ET.Either[E, P.Pair[A, S]], + IOEB ~func() ET.Either[E, P.Pair[B, S]], + IOEB_IN ~func() ET.Either[E, B], + S, R, E, A, B any, +](ma SRIOEA, f func(A) RIOEB_IN) SRIOEB { + return MonadChain(ma, FromReaderIOEitherK[SRIOEB, RIOEB_IN](f)) +} + +func ChainReaderIOEitherK[ + SRIOEA ~func(S) RIOEA, + SRIOEB ~func(S) RIOEB, + RIOEB_IN ~func(R) IOEB_IN, + RIOEA ~func(R) IOEA, + RIOEB ~func(R) IOEB, + IOEA ~func() ET.Either[E, P.Pair[A, S]], + IOEB ~func() ET.Either[E, P.Pair[B, S]], + IOEB_IN ~func() ET.Either[E, B], + S, R, E, A, B any, +](f func(A) RIOEB_IN) func(SRIOEA) SRIOEB { + return Chain[SRIOEA](FromReaderIOEitherK[SRIOEB, RIOEB_IN](f)) +} + +func MonadChainIOEitherK[ + SRIOEA ~func(S) RIOEA, + SRIOEB ~func(S) RIOEB, + RIOEB_IN ~func(R) IOEB_IN, + IOEB_IN ~func() ET.Either[E, B], + RIOEA ~func(R) IOEA, + RIOEB ~func(R) IOEB, + IOEA ~func() ET.Either[E, P.Pair[A, S]], + IOEB ~func() ET.Either[E, P.Pair[B, S]], + S, R, E, A, B any, +](ma SRIOEA, f func(A) IOEB_IN) SRIOEB { + return MonadChain(ma, FromIOEitherK[SRIOEB, RIOEB_IN](f)) +} + +func ChainIOEitherK[ + SRIOEA ~func(S) RIOEA, + SRIOEB ~func(S) RIOEB, + RIOEB_IN ~func(R) IOEB_IN, + IOEB_IN ~func() ET.Either[E, B], + RIOEA ~func(R) IOEA, + RIOEB ~func(R) IOEB, + IOEA ~func() ET.Either[E, P.Pair[A, S]], + IOEB ~func() ET.Either[E, P.Pair[B, S]], + S, R, E, A, B any, +](f func(A) IOEB_IN) func(SRIOEA) SRIOEB { + return Chain[SRIOEA](FromIOEitherK[SRIOEB, RIOEB_IN](f)) +} + +func MonadChainEitherK[ + SRIOEA ~func(S) RIOEA, + SRIOEB ~func(S) RIOEB, + RIOEA ~func(R) IOEA, + RIOEB ~func(R) IOEB, + IOEA ~func() ET.Either[E, P.Pair[A, S]], + IOEB ~func() ET.Either[E, P.Pair[B, S]], + S, R, E, A, B any, +](ma SRIOEA, f func(A) ET.Either[E, B]) SRIOEB { + return MonadChain(ma, FromEitherK[SRIOEB](f)) +} + +func ChainEitherK[ + SRIOEA ~func(S) RIOEA, + SRIOEB ~func(S) RIOEB, + RIOEA ~func(R) IOEA, + RIOEB ~func(R) IOEB, + IOEA ~func() ET.Either[E, P.Pair[A, S]], + IOEB ~func() ET.Either[E, P.Pair[B, S]], + S, R, E, A, B any, +](f func(A) ET.Either[E, B]) func(SRIOEA) SRIOEB { + return Chain[SRIOEA](FromEitherK[SRIOEB](f)) +} diff --git a/statereaderioeither/state.go b/statereaderioeither/state.go index e60e126..315e13b 100644 --- a/statereaderioeither/state.go +++ b/statereaderioeither/state.go @@ -89,3 +89,57 @@ func FromReader[S, R, E, A any](fa RD.Reader[R, A]) StateReaderIOEither[S, R, E, func FromEither[S, R, E, A any](ma ET.Either[E, A]) StateReaderIOEither[S, R, E, A] { return G.FromEither[StateReaderIOEither[S, R, E, A]](ma) } + +// Combinators + +func Local[S, R1, R2, E, A, B any](f func(R2) R1) func(StateReaderIOEither[S, R1, E, A]) StateReaderIOEither[S, R2, E, A] { + return G.Local[StateReaderIOEither[S, R1, E, A], StateReaderIOEither[S, R2, E, A]](f) +} + +func Asks[ + S, R, E, A any, +](f func(R) StateReaderIOEither[S, R, E, A]) StateReaderIOEither[S, R, E, A] { + return G.Asks[StateReaderIOEither[S, R, E, A]](f) +} + +func FromEitherK[S, R, E, A, B any](f func(A) ET.Either[E, B]) func(A) StateReaderIOEither[S, R, E, B] { + return G.FromEitherK[StateReaderIOEither[S, R, E, B]](f) +} + +func FromIOK[S, R, E, A, B any](f func(A) IO.IO[B]) func(A) StateReaderIOEither[S, R, E, B] { + return G.FromIOK[StateReaderIOEither[S, R, E, B], RIOE.ReaderIOEither[R, E, B]](f) +} + +func FromIOEitherK[ + S, R, E, A, B any, +](f func(A) IOE.IOEither[E, B]) func(A) StateReaderIOEither[S, R, E, B] { + return G.FromIOEitherK[StateReaderIOEither[S, R, E, B], RIOE.ReaderIOEither[R, E, B]](f) +} + +func FromReaderIOEitherK[S, R, E, A, B any](f func(A) RIOE.ReaderIOEither[R, E, B]) func(A) StateReaderIOEither[S, R, E, B] { + return G.FromReaderIOEitherK[StateReaderIOEither[S, R, E, B], RIOE.ReaderIOEither[R, E, B]](f) +} + +func MonadChainReaderIOEitherK[S, R, E, A, B any](ma StateReaderIOEither[S, R, E, A], f func(A) RIOE.ReaderIOEither[R, E, B]) StateReaderIOEither[S, R, E, B] { + return G.MonadChainReaderIOEitherK[StateReaderIOEither[S, R, E, A], StateReaderIOEither[S, R, E, B]](ma, f) +} + +func ChainReaderIOEitherK[S, R, E, A, B any](f func(A) RIOE.ReaderIOEither[R, E, B]) func(StateReaderIOEither[S, R, E, A]) StateReaderIOEither[S, R, E, B] { + return G.ChainReaderIOEitherK[StateReaderIOEither[S, R, E, A], StateReaderIOEither[S, R, E, B]](f) +} + +func MonadChainIOEitherK[S, R, E, A, B any](ma StateReaderIOEither[S, R, E, A], f func(A) IOE.IOEither[E, B]) StateReaderIOEither[S, R, E, B] { + return G.MonadChainIOEitherK[StateReaderIOEither[S, R, E, A], StateReaderIOEither[S, R, E, B], RIOE.ReaderIOEither[R, E, B]](ma, f) +} + +func ChainIOEitherK[S, R, E, A, B any](f func(A) IOE.IOEither[E, B]) func(StateReaderIOEither[S, R, E, A]) StateReaderIOEither[S, R, E, B] { + return G.ChainIOEitherK[StateReaderIOEither[S, R, E, A], StateReaderIOEither[S, R, E, B], RIOE.ReaderIOEither[R, E, B]](f) +} + +func MonadChainEitherK[S, R, E, A, B any](ma StateReaderIOEither[S, R, E, A], f func(A) ET.Either[E, B]) StateReaderIOEither[S, R, E, B] { + return G.MonadChainEitherK[StateReaderIOEither[S, R, E, A], StateReaderIOEither[S, R, E, B]](ma, f) +} + +func ChainEitherK[S, R, E, A, B any](f func(A) ET.Either[E, B]) func(StateReaderIOEither[S, R, E, A]) StateReaderIOEither[S, R, E, B] { + return G.ChainEitherK[StateReaderIOEither[S, R, E, A], StateReaderIOEither[S, R, E, B]](f) +}