Skip to content

Commit

Permalink
fix: add conversions for StateReaderIOEither
Browse files Browse the repository at this point in the history
Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
  • Loading branch information
CarstenLeue committed Feb 18, 2024
1 parent 747f477 commit f4f4fb3
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 0 deletions.
31 changes: 31 additions & 0 deletions internal/statet/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,34 @@ func Ap[
)
}
}

func FromF[
HKTSA ~func(S) HKTA,
HKTA,

HKTFA,

S, A any,
](
fmap func(HKTFA, func(A) P.Pair[A, S]) HKTA,
ma HKTFA) HKTSA {

f1 := F.Bind1st(fmap, ma)

return func(s S) HKTA {
return f1(F.Bind2nd(P.MakePair[A, S], s))
}
}

func FromState[
HKTSA ~func(S) HKTA,
ST ~func(S) P.Pair[A, S],
HKTA,

S, A any,
](
fof func(P.Pair[A, S]) HKTA,
sa ST,
) HKTSA {
return F.Flow2(sa, fof)
}
111 changes: 111 additions & 0 deletions statereaderioeither/generic/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,21 @@ package generic

import (
ET "github.com/IBM/fp-go/either"
F "github.com/IBM/fp-go/function"
ST "github.com/IBM/fp-go/internal/statet"
P "github.com/IBM/fp-go/pair"
G "github.com/IBM/fp-go/readerioeither/generic"
)

func Left[
SRIOEA ~func(S) RIOEA,
RIOEA ~func(R) IOEA,
IOEA ~func() ET.Either[E, P.Pair[A, S]],
S, R, E, A any,
](e E) SRIOEA {
return F.Constant1[S](G.Left[RIOEA](e))
}

func Right[
SRIOEA ~func(S) RIOEA,
RIOEA ~func(R) IOEA,
Expand Down Expand Up @@ -143,3 +153,104 @@ func Ap[
fa,
)
}

func FromReaderIOEither[
SRIOEA ~func(S) RIOEA,
RIOEA ~func(R) IOEA,

RIOEA_IN ~func(R) IOEA_IN,

IOEA ~func() ET.Either[E, P.Pair[A, S]],
IOEA_IN ~func() ET.Either[E, A],

S, R, E, A any,
](fa RIOEA_IN) SRIOEA {
return ST.FromF[SRIOEA](
G.MonadMap[RIOEA_IN, RIOEA],
fa,
)
}

func FromReaderEither[
SRIOEA ~func(S) RIOEA,
RIOEA_IN ~func(R) IOEA_IN,
RIOEA ~func(R) IOEA,

REA_IN ~func(R) ET.Either[E, A],

IOEA ~func() ET.Either[E, P.Pair[A, S]],
IOEA_IN ~func() ET.Either[E, A],

S, R, E, A any,
](fa REA_IN) SRIOEA {
return FromReaderIOEither[SRIOEA](G.FromReaderEither[REA_IN, RIOEA_IN](fa))
}

func FromIOEither[
SRIOEA ~func(S) RIOEA,
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],

S, R, E, A any,
](fa IOEA_IN) SRIOEA {
return FromReaderIOEither[SRIOEA](G.FromIOEither[RIOEA_IN](fa))
}

func FromIO[
SRIOEA ~func(S) RIOEA,
RIOEA_IN ~func(R) IOEA_IN,

IO_IN ~func() 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 IO_IN) SRIOEA {
return FromReaderIOEither[SRIOEA](G.FromIO[RIOEA_IN](fa))
}

func FromReader[
SRIOEA ~func(S) RIOEA,
RIOEA_IN ~func(R) IOEA_IN,

R_IN ~func(R) 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 R_IN) SRIOEA {
return FromReaderIOEither[SRIOEA](G.FromReader[R_IN, RIOEA_IN](fa))
}

func FromEither[
SRIOEA ~func(S) RIOEA,

RIOEA ~func(R) IOEA,

IOEA ~func() ET.Either[E, P.Pair[A, S]],

S, R, E, A any,
](ma ET.Either[E, A]) SRIOEA {
return ET.MonadFold(ma, Left[SRIOEA], Right[SRIOEA])
}

func FromState[
SRIOEA ~func(S) RIOEA,
STATE ~func(S) P.Pair[A, S],
RIOEA ~func(R) IOEA,

IOEA ~func() ET.Either[E, P.Pair[A, S]],

S, R, E, A any,
](fa STATE) SRIOEA {
return ST.FromState[SRIOEA](G.Of[RIOEA], fa)
}
39 changes: 39 additions & 0 deletions statereaderioeither/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,20 @@
package statereaderioeither

import (
ET "github.com/IBM/fp-go/either"
IO "github.com/IBM/fp-go/io"
IOE "github.com/IBM/fp-go/ioeither"
RD "github.com/IBM/fp-go/reader"
RE "github.com/IBM/fp-go/readereither"
RIOE "github.com/IBM/fp-go/readerioeither"
ST "github.com/IBM/fp-go/state"
G "github.com/IBM/fp-go/statereaderioeither/generic"
)

func Left[S, R, A, E any](e E) StateReaderIOEither[S, R, E, A] {
return G.Left[StateReaderIOEither[S, R, E, A]](e)
}

func Right[S, R, E, A any](a A) StateReaderIOEither[S, R, E, A] {
return G.Right[StateReaderIOEither[S, R, E, A]](a)
}
Expand Down Expand Up @@ -50,3 +61,31 @@ func MonadAp[S, R, E, A, B any](fab StateReaderIOEither[S, R, E, func(A) B], fa
func Ap[S, R, E, A, B any](fa StateReaderIOEither[S, R, E, A]) func(StateReaderIOEither[S, R, E, func(A) B]) StateReaderIOEither[S, R, E, B] {
return G.Ap[StateReaderIOEither[S, R, E, A], StateReaderIOEither[S, R, E, B], StateReaderIOEither[S, R, E, func(A) B]](fa)
}

func FromReaderIOEither[S, R, E, A any](fa RIOE.ReaderIOEither[R, E, A]) StateReaderIOEither[S, R, E, A] {
return G.FromReaderIOEither[StateReaderIOEither[S, R, E, A]](fa)
}

func FromReaderEither[S, R, E, A any](fa RE.ReaderEither[R, E, A]) StateReaderIOEither[S, R, E, A] {
return G.FromReaderEither[StateReaderIOEither[S, R, E, A], RIOE.ReaderIOEither[R, E, A]](fa)
}

func FromIOEither[S, R, E, A any](fa IOE.IOEither[E, A]) StateReaderIOEither[S, R, E, A] {
return G.FromIOEither[StateReaderIOEither[S, R, E, A], RIOE.ReaderIOEither[R, E, A]](fa)
}

func FromState[S, R, E, A any](sa ST.State[S, A]) StateReaderIOEither[S, R, E, A] {
return G.FromState[StateReaderIOEither[S, R, E, A]](sa)
}

func FromIO[S, R, E, A any](fa IO.IO[A]) StateReaderIOEither[S, R, E, A] {
return G.FromIO[StateReaderIOEither[S, R, E, A], RIOE.ReaderIOEither[R, E, A]](fa)
}

func FromReader[S, R, E, A any](fa RD.Reader[R, A]) StateReaderIOEither[S, R, E, A] {
return G.FromReader[StateReaderIOEither[S, R, E, A], RIOE.ReaderIOEither[R, E, A]](fa)
}

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)
}

0 comments on commit f4f4fb3

Please sign in to comment.