From a14feff1d6833e5a398b2f94d54e695e7a737892 Mon Sep 17 00:00:00 2001 From: "Dr. Carsten Leue" Date: Mon, 15 Jan 2024 12:04:55 +0100 Subject: [PATCH] fix: add missing MapLeft Signed-off-by: Dr. Carsten Leue --- either/either.go | 9 +++++---- ioeither/ioeither.go | 2 +- readereither/generic/reader.go | 9 +++++++++ readereither/reader.go | 9 +++++++++ readerioeither/generic/reader.go | 9 +++++++++ readerioeither/reader.go | 9 +++++++++ 6 files changed, 42 insertions(+), 5 deletions(-) diff --git a/either/either.go b/either/either.go index cfe542e..512b33a 100644 --- a/either/either.go +++ b/either/either.go @@ -67,16 +67,17 @@ func MapTo[E, A, B any](b B) func(Either[E, A]) Either[E, B] { return F.Bind2nd(MonadMapTo[E, A, B], b) } -func MonadMapLeft[E, A, B any](fa Either[E, A], f func(E) B) Either[B, A] { - return MonadFold(fa, F.Flow2(f, Left[A, B]), Right[B, A]) +func MonadMapLeft[E1, A, E2 any](fa Either[E1, A], f func(E1) E2) Either[E2, A] { + return MonadFold(fa, F.Flow2(f, Left[A, E2]), Right[E2, A]) } func Map[E, A, B any](f func(a A) B) func(fa Either[E, A]) Either[E, B] { return Chain(F.Flow2(f, Right[E, B])) } -func MapLeft[E, A, B any](f func(E) B) func(fa Either[E, A]) Either[B, A] { - return F.Bind2nd(MonadMapLeft[E, A, B], f) +// MapLeft applies a mapping function to the error channel +func MapLeft[A, E1, E2 any](f func(E1) E2) func(fa Either[E1, A]) Either[E2, A] { + return F.Bind2nd(MonadMapLeft[E1, A, E2], f) } func MonadChain[E, A, B any](fa Either[E, A], f func(a A) Either[E, B]) Either[E, B] { diff --git a/ioeither/ioeither.go b/ioeither/ioeither.go index c8e24ce..d32d73e 100644 --- a/ioeither/ioeither.go +++ b/ioeither/ioeither.go @@ -175,7 +175,7 @@ func MonadMapLeft[E1, E2, A any](fa IOEither[E1, A], f func(E1) E2) IOEither[E2, return G.MonadMapLeft[IOEither[E1, A], IOEither[E2, A]](fa, f) } -func MapLeft[E1, E2, A any](f func(E1) E2) func(IOEither[E1, A]) IOEither[E2, A] { +func MapLeft[A, E1, E2 any](f func(E1) E2) func(IOEither[E1, A]) IOEither[E2, A] { return G.MapLeft[IOEither[E1, A], IOEither[E2, A]](f) } diff --git a/readereither/generic/reader.go b/readereither/generic/reader.go index 949317f..9bba54a 100644 --- a/readereither/generic/reader.go +++ b/readereither/generic/reader.go @@ -164,3 +164,12 @@ func MonadFlap[GEFAB ~func(E) ET.Either[L, func(A) B], GEB ~func(E) ET.Either[L, 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) } + +func MonadMapLeft[GA1 ~func(C) ET.Either[E1, A], GA2 ~func(C) ET.Either[E2, A], C, E1, E2, A any](fa GA1, f func(E1) E2) GA2 { + return eithert.MonadMapLeft(R.MonadMap[GA1, GA2, C, ET.Either[E1, A], ET.Either[E2, A]], fa, f) +} + +// MapLeft applies a mapping function to the error channel +func MapLeft[GA1 ~func(C) ET.Either[E1, A], GA2 ~func(C) ET.Either[E2, A], C, E1, E2, A any](f func(E1) E2) func(GA1) GA2 { + return F.Bind2nd(MonadMapLeft[GA1, GA2, C, E1, E2, A], f) +} diff --git a/readereither/reader.go b/readereither/reader.go index 0c2e56c..2c2cd0c 100644 --- a/readereither/reader.go +++ b/readereither/reader.go @@ -151,3 +151,12 @@ func MonadFlap[L, E, A, B any](fab ReaderEither[L, E, func(A) B], a A) ReaderEit 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) } + +func MonadMapLeft[C, E1, E2, A any](fa ReaderEither[C, E1, A], f func(E1) E2) ReaderEither[C, E2, A] { + return G.MonadMapLeft[ReaderEither[C, E1, A], ReaderEither[C, E2, A]](fa, f) +} + +// MapLeft applies a mapping function to the error channel +func MapLeft[C, E1, E2, A any](f func(E1) E2) func(ReaderEither[C, E1, A]) ReaderEither[C, E2, A] { + return G.MapLeft[ReaderEither[C, E1, A], ReaderEither[C, E2, A]](f) +} diff --git a/readerioeither/generic/reader.go b/readerioeither/generic/reader.go index 22dddc8..2b0b88e 100644 --- a/readerioeither/generic/reader.go +++ b/readerioeither/generic/reader.go @@ -440,3 +440,12 @@ func MonadFlap[GREAB ~func(R) GEAB, GREB ~func(R) GEB, GEAB ~func() ET.Either[E, func Flap[GREAB ~func(R) GEAB, GREB ~func(R) GEB, GEAB ~func() ET.Either[E, func(A) B], GEB ~func() ET.Either[E, B], R, E, B, A any](a A) func(GREAB) GREB { return FC.Flap(MonadMap[GREAB, GREB], a) } + +func MonadMapLeft[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](fa GREA1, f func(E1) E2) GREA2 { + return eithert.MonadMapLeft(G.MonadMap[GREA1, GREA2], fa, f) +} + +// MapLeft applies a mapping function to the error channel +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) +} diff --git a/readerioeither/reader.go b/readerioeither/reader.go index 4c6b26c..29074b1 100644 --- a/readerioeither/reader.go +++ b/readerioeither/reader.go @@ -280,3 +280,12 @@ func MonadFlap[R, E, B, A any](fab ReaderIOEither[R, E, func(A) B], a A) ReaderI func Flap[R, E, B, A any](a A) func(ReaderIOEither[R, E, func(A) B]) ReaderIOEither[R, E, B] { return G.Flap[ReaderIOEither[R, E, func(A) B], ReaderIOEither[R, E, B]](a) } + +func MonadMapLeft[R, E1, E2, A any](fa ReaderIOEither[R, E1, A], f func(E1) E2) ReaderIOEither[R, E2, A] { + return G.MonadMapLeft[ReaderIOEither[R, E1, A], ReaderIOEither[R, E2, A]](fa, f) +} + +// MapLeft applies a mapping function to the error channel +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) +}