Skip to content

Commit

Permalink
fix: add Flap to more monads
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 Oct 12, 2023
1 parent b434f1f commit f129297
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 0 deletions.
8 changes: 8 additions & 0 deletions context/reader/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,11 @@ func Ask() Reader[context.Context] {
func Asks[A any](r Reader[A]) Reader[A] {
return R.Asks(r)
}

func MonadFlap[B, A any](fab Reader[func(A) B], a A) Reader[B] {
return R.MonadFlap[Reader[func(A) B], Reader[B]](fab, a)
}

func Flap[B, A any](a A) func(Reader[func(A) B]) Reader[B] {
return R.Flap[Reader[func(A) B], Reader[B]](a)
}
8 changes: 8 additions & 0 deletions context/readereither/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,11 @@ func ChainEitherK[A, B any](f func(A) ET.Either[error, B]) func(ma ReaderEither[
func ChainOptionK[A, B any](onNone func() error) func(func(A) O.Option[B]) func(ReaderEither[A]) ReaderEither[B] {
return RE.ChainOptionK[ReaderEither[A], ReaderEither[B]](onNone)
}

func MonadFlap[B, A any](fab ReaderEither[func(A) B], a A) ReaderEither[B] {
return RE.MonadFlap[ReaderEither[func(A) B], ReaderEither[B]](fab, a)
}

func Flap[B, A any](a A) func(ReaderEither[func(A) B]) ReaderEither[B] {
return RE.Flap[ReaderEither[func(A) B], ReaderEither[B]](a)
}
8 changes: 8 additions & 0 deletions context/readerio/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,11 @@ func Memoize[A any](rdr ReaderIO[A]) ReaderIO[A] {
func Flatten[A any](mma ReaderIO[ReaderIO[A]]) ReaderIO[A] {
return R.Flatten[ReaderIO[A]](mma)
}

func MonadFlap[B, A any](fab ReaderIO[func(A) B], a A) ReaderIO[B] {
return R.MonadFlap[ReaderIO[func(A) B], ReaderIO[B]](fab, a)
}

func Flap[B, A any](a A) func(ReaderIO[func(A) B]) ReaderIO[B] {
return R.Flap[ReaderIO[func(A) B], ReaderIO[B]](a)
}
9 changes: 9 additions & 0 deletions reader/generic/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package generic
import (
F "github.com/IBM/fp-go/function"
I "github.com/IBM/fp-go/identity/generic"
FC "github.com/IBM/fp-go/internal/functor"
T "github.com/IBM/fp-go/tuple"
)

Expand Down Expand Up @@ -120,3 +121,11 @@ func Local[GA1 ~func(R1) A, GA2 ~func(R2) A, R2, R1, A any](f func(R2) R1) func(
func Read[GA ~func(E) A, E, A any](e E) func(GA) A {
return I.Ap[GA](e)
}

func MonadFlap[GAB ~func(R) func(A) B, GB ~func(R) B, R, A, B any](fab GAB, a A) GB {
return FC.MonadFlap(MonadMap[GAB, GB], fab, a)
}

func Flap[GAB ~func(R) func(A) B, GB ~func(R) B, R, A, B any](a A) func(GAB) GB {
return FC.Flap(MonadMap[GAB, GB], a)
}
8 changes: 8 additions & 0 deletions reader/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,11 @@ func Local[R2, R1, A any](f func(R2) R1) func(Reader[R1, A]) Reader[R2, A] {
func Read[E, A any](e E) func(Reader[E, A]) A {
return G.Read[Reader[E, A]](e)
}

func MonadFlap[R, A, B any](fab Reader[R, func(A) B], a A) Reader[R, B] {
return G.MonadFlap[Reader[R, func(A) B], Reader[R, B]](fab, a)
}

func Flap[R, A, B any](a A) func(Reader[R, func(A) B]) Reader[R, B] {
return G.Flap[Reader[R, func(A) B], Reader[R, B]](a)
}
9 changes: 9 additions & 0 deletions readereither/generic/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/IBM/fp-go/internal/eithert"
FE "github.com/IBM/fp-go/internal/fromeither"
FR "github.com/IBM/fp-go/internal/fromreader"
FC "github.com/IBM/fp-go/internal/functor"
"github.com/IBM/fp-go/internal/readert"
O "github.com/IBM/fp-go/option"
R "github.com/IBM/fp-go/reader/generic"
Expand Down Expand Up @@ -155,3 +156,11 @@ func Local[GA1 ~func(R1) ET.Either[E, A], GA2 ~func(R2) ET.Either[E, A], R2, R1,
func Read[GA ~func(E) ET.Either[E1, A], E, E1, A any](e E) func(GA) ET.Either[E1, A] {
return R.Read[GA](e)
}

func MonadFlap[GEFAB ~func(E) ET.Either[L, func(A) B], GEB ~func(E) ET.Either[L, B], L, E, A, B any](fab GEFAB, a A) GEB {
return FC.MonadFlap(MonadMap[GEFAB, GEB], fab, a)
}

func Flap[GEFAB ~func(E) ET.Either[L, func(A) B], GEB ~func(E) ET.Either[L, B], L, E, A, B any](a A) func(GEFAB) GEB {
return FC.Flap(MonadMap[GEFAB, GEB], a)
}
8 changes: 8 additions & 0 deletions readereither/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,11 @@ func Local[E, A, R2, R1 any](f func(R2) R1) func(ReaderEither[R1, E, A]) ReaderE
func Read[E1, A, E any](e E) func(ReaderEither[E, E1, A]) ET.Either[E1, A] {
return G.Read[ReaderEither[E, E1, A]](e)
}

func MonadFlap[L, E, A, B any](fab ReaderEither[L, E, func(A) B], a A) ReaderEither[L, E, B] {
return G.MonadFlap[ReaderEither[L, E, func(A) B], ReaderEither[L, E, B]](fab, a)
}

func Flap[L, E, B, A any](a A) func(ReaderEither[L, E, func(A) B]) ReaderEither[L, E, B] {
return G.Flap[ReaderEither[L, E, func(A) B], ReaderEither[L, E, B]](a)
}
9 changes: 9 additions & 0 deletions readerio/generic/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
F "github.com/IBM/fp-go/function"
FIO "github.com/IBM/fp-go/internal/fromio"
FR "github.com/IBM/fp-go/internal/fromreader"
FC "github.com/IBM/fp-go/internal/functor"
"github.com/IBM/fp-go/internal/readert"
IO "github.com/IBM/fp-go/io/generic"
R "github.com/IBM/fp-go/reader/generic"
Expand Down Expand Up @@ -155,3 +156,11 @@ func Memoize[GEA ~func(E) GA, GA ~func() A, E, A any](rdr GEA) GEA {
func Flatten[GEA ~func(R) GIOA, GGEA ~func(R) GIOEA, GIOA ~func() A, GIOEA ~func() GEA, R, A any](mma GGEA) GEA {
return MonadChain(mma, F.Identity[GEA])
}

func MonadFlap[GEFAB ~func(E) GIOFAB, GEB ~func(E) GIOB, GIOFAB ~func() func(A) B, GIOB ~func() B, E, A, B any](fab GEFAB, a A) GEB {
return FC.MonadFlap(MonadMap[GEFAB, GEB], fab, a)
}

func Flap[GEFAB ~func(E) GIOFAB, GEB ~func(E) GIOB, GIOFAB ~func() func(A) B, GIOB ~func() B, E, A, B any](a A) func(GEFAB) GEB {
return FC.Flap(MonadMap[GEFAB, GEB], a)
}
8 changes: 8 additions & 0 deletions readerio/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,11 @@ func Memoize[E, A any](rdr ReaderIO[E, A]) ReaderIO[E, A] {
func Flatten[E, A any](mma ReaderIO[E, ReaderIO[E, A]]) ReaderIO[E, A] {
return G.Flatten[ReaderIO[E, A], ReaderIO[E, ReaderIO[E, A]]](mma)
}

func MonadFlap[E, A, B any](fab ReaderIO[E, func(A) B], a A) ReaderIO[E, B] {
return G.MonadFlap[ReaderIO[E, func(A) B], ReaderIO[E, B]](fab, a)
}

func Flap[E, A, B any](a A) func(ReaderIO[E, func(A) B]) ReaderIO[E, B] {
return G.Flap[ReaderIO[E, func(A) B], ReaderIO[E, B]](a)
}
9 changes: 9 additions & 0 deletions record/generic/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"sort"

F "github.com/IBM/fp-go/function"
FC "github.com/IBM/fp-go/internal/functor"
G "github.com/IBM/fp-go/internal/record"
Mg "github.com/IBM/fp-go/magma"
Mo "github.com/IBM/fp-go/monoid"
Expand Down Expand Up @@ -514,3 +515,11 @@ func FoldMapOrdWithIndex[AS ~map[K]A, K comparable, A, B any](o ord.Ord[K]) func
}
}
}

func MonadFlap[GFAB ~map[K]func(A) B, GB ~map[K]B, K comparable, A, B any](fab GFAB, a A) GB {
return FC.MonadFlap(MonadMap[GFAB, GB], fab, a)
}

func Flap[GFAB ~map[K]func(A) B, GB ~map[K]B, K comparable, A, B any](a A) func(GFAB) GB {
return FC.Flap(MonadMap[GFAB, GB], a)
}
8 changes: 8 additions & 0 deletions record/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,11 @@ func KeysOrd[V any, K comparable](o ord.Ord[K]) func(r map[K]V) []K {
func ValuesOrd[V any, K comparable](o ord.Ord[K]) func(r map[K]V) []V {
return G.ValuesOrd[map[K]V, []V, K, V](o)
}

func MonadFlap[B any, K comparable, A any](fab map[K]func(A) B, a A) map[K]B {
return G.MonadFlap[map[K]func(A) B, map[K]B](fab, a)
}

func Flap[B any, K comparable, A any](a A) func(map[K]func(A) B) map[K]B {
return G.Flap[map[K]func(A) B, map[K]B](a)
}

0 comments on commit f129297

Please sign in to comment.