1
0
mirror of https://github.com/IBM/fp-go.git synced 2025-08-10 22:31:32 +02:00

fix: implementation of traversal for ReaderIOEither

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
This commit is contained in:
Dr. Carsten Leue
2023-07-19 17:05:50 +02:00
parent 19bdaad7ae
commit b87cfcf941
16 changed files with 4578 additions and 4363 deletions

View File

@@ -13,65 +13,65 @@ import (
// Eitherize0 converts a function with 0 parameters returning a tuple into a function with 0 parameters returning a [ReaderIOEither[R]]
// The inverse function is [Uneitherize0]
func Eitherize0[F ~func(context.Context) (R, error), R any](f F) func() ReaderIOEither[R] {
return G.Eitherize0[ReaderIOEither[R]](f)
return G.Eitherize0[ReaderIOEither[R]](f)
}
// Eitherize1 converts a function with 1 parameters returning a tuple into a function with 1 parameters returning a [ReaderIOEither[R]]
// The inverse function is [Uneitherize1]
func Eitherize1[F ~func(context.Context, T0) (R, error), T0, R any](f F) func(T0) ReaderIOEither[R] {
return G.Eitherize1[ReaderIOEither[R]](f)
return G.Eitherize1[ReaderIOEither[R]](f)
}
// Eitherize2 converts a function with 2 parameters returning a tuple into a function with 2 parameters returning a [ReaderIOEither[R]]
// The inverse function is [Uneitherize2]
func Eitherize2[F ~func(context.Context, T0, T1) (R, error), T0, T1, R any](f F) func(T0, T1) ReaderIOEither[R] {
return G.Eitherize2[ReaderIOEither[R]](f)
return G.Eitherize2[ReaderIOEither[R]](f)
}
// Eitherize3 converts a function with 3 parameters returning a tuple into a function with 3 parameters returning a [ReaderIOEither[R]]
// The inverse function is [Uneitherize3]
func Eitherize3[F ~func(context.Context, T0, T1, T2) (R, error), T0, T1, T2, R any](f F) func(T0, T1, T2) ReaderIOEither[R] {
return G.Eitherize3[ReaderIOEither[R]](f)
return G.Eitherize3[ReaderIOEither[R]](f)
}
// Eitherize4 converts a function with 4 parameters returning a tuple into a function with 4 parameters returning a [ReaderIOEither[R]]
// The inverse function is [Uneitherize4]
func Eitherize4[F ~func(context.Context, T0, T1, T2, T3) (R, error), T0, T1, T2, T3, R any](f F) func(T0, T1, T2, T3) ReaderIOEither[R] {
return G.Eitherize4[ReaderIOEither[R]](f)
return G.Eitherize4[ReaderIOEither[R]](f)
}
// Eitherize5 converts a function with 5 parameters returning a tuple into a function with 5 parameters returning a [ReaderIOEither[R]]
// The inverse function is [Uneitherize5]
func Eitherize5[F ~func(context.Context, T0, T1, T2, T3, T4) (R, error), T0, T1, T2, T3, T4, R any](f F) func(T0, T1, T2, T3, T4) ReaderIOEither[R] {
return G.Eitherize5[ReaderIOEither[R]](f)
return G.Eitherize5[ReaderIOEither[R]](f)
}
// Eitherize6 converts a function with 6 parameters returning a tuple into a function with 6 parameters returning a [ReaderIOEither[R]]
// The inverse function is [Uneitherize6]
func Eitherize6[F ~func(context.Context, T0, T1, T2, T3, T4, T5) (R, error), T0, T1, T2, T3, T4, T5, R any](f F) func(T0, T1, T2, T3, T4, T5) ReaderIOEither[R] {
return G.Eitherize6[ReaderIOEither[R]](f)
return G.Eitherize6[ReaderIOEither[R]](f)
}
// Eitherize7 converts a function with 7 parameters returning a tuple into a function with 7 parameters returning a [ReaderIOEither[R]]
// The inverse function is [Uneitherize7]
func Eitherize7[F ~func(context.Context, T0, T1, T2, T3, T4, T5, T6) (R, error), T0, T1, T2, T3, T4, T5, T6, R any](f F) func(T0, T1, T2, T3, T4, T5, T6) ReaderIOEither[R] {
return G.Eitherize7[ReaderIOEither[R]](f)
return G.Eitherize7[ReaderIOEither[R]](f)
}
// Eitherize8 converts a function with 8 parameters returning a tuple into a function with 8 parameters returning a [ReaderIOEither[R]]
// The inverse function is [Uneitherize8]
func Eitherize8[F ~func(context.Context, T0, T1, T2, T3, T4, T5, T6, T7) (R, error), T0, T1, T2, T3, T4, T5, T6, T7, R any](f F) func(T0, T1, T2, T3, T4, T5, T6, T7) ReaderIOEither[R] {
return G.Eitherize8[ReaderIOEither[R]](f)
return G.Eitherize8[ReaderIOEither[R]](f)
}
// Eitherize9 converts a function with 9 parameters returning a tuple into a function with 9 parameters returning a [ReaderIOEither[R]]
// The inverse function is [Uneitherize9]
func Eitherize9[F ~func(context.Context, T0, T1, T2, T3, T4, T5, T6, T7, T8) (R, error), T0, T1, T2, T3, T4, T5, T6, T7, T8, R any](f F) func(T0, T1, T2, T3, T4, T5, T6, T7, T8) ReaderIOEither[R] {
return G.Eitherize9[ReaderIOEither[R]](f)
return G.Eitherize9[ReaderIOEither[R]](f)
}
// Eitherize10 converts a function with 10 parameters returning a tuple into a function with 10 parameters returning a [ReaderIOEither[R]]
// The inverse function is [Uneitherize10]
func Eitherize10[F ~func(context.Context, T0, T1, T2, T3, T4, T5, T6, T7, T8, T9) (R, error), T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, R any](f F) func(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9) ReaderIOEither[R] {
return G.Eitherize10[ReaderIOEither[R]](f)
return G.Eitherize10[ReaderIOEither[R]](f)
}

View File

@@ -14,65 +14,65 @@ import (
// Eitherize0 converts a function with 0 parameters returning a tuple into a function with 0 parameters returning a [GRA]
// The inverse function is [Uneitherize0]
func Eitherize0[GRA ~func(context.Context) GIOA, F ~func(context.Context) (R, error), GIOA ~func() E.Either[error, R], R any](f F) func() GRA {
return RE.Eitherize0[GRA](f)
return RE.Eitherize0[GRA](f)
}
// Eitherize1 converts a function with 1 parameters returning a tuple into a function with 1 parameters returning a [GRA]
// The inverse function is [Uneitherize1]
func Eitherize1[GRA ~func(context.Context) GIOA, F ~func(context.Context, T0) (R, error), GIOA ~func() E.Either[error, R], T0, R any](f F) func(T0) GRA {
return RE.Eitherize1[GRA](f)
return RE.Eitherize1[GRA](f)
}
// Eitherize2 converts a function with 2 parameters returning a tuple into a function with 2 parameters returning a [GRA]
// The inverse function is [Uneitherize2]
func Eitherize2[GRA ~func(context.Context) GIOA, F ~func(context.Context, T0, T1) (R, error), GIOA ~func() E.Either[error, R], T0, T1, R any](f F) func(T0, T1) GRA {
return RE.Eitherize2[GRA](f)
return RE.Eitherize2[GRA](f)
}
// Eitherize3 converts a function with 3 parameters returning a tuple into a function with 3 parameters returning a [GRA]
// The inverse function is [Uneitherize3]
func Eitherize3[GRA ~func(context.Context) GIOA, F ~func(context.Context, T0, T1, T2) (R, error), GIOA ~func() E.Either[error, R], T0, T1, T2, R any](f F) func(T0, T1, T2) GRA {
return RE.Eitherize3[GRA](f)
return RE.Eitherize3[GRA](f)
}
// Eitherize4 converts a function with 4 parameters returning a tuple into a function with 4 parameters returning a [GRA]
// The inverse function is [Uneitherize4]
func Eitherize4[GRA ~func(context.Context) GIOA, F ~func(context.Context, T0, T1, T2, T3) (R, error), GIOA ~func() E.Either[error, R], T0, T1, T2, T3, R any](f F) func(T0, T1, T2, T3) GRA {
return RE.Eitherize4[GRA](f)
return RE.Eitherize4[GRA](f)
}
// Eitherize5 converts a function with 5 parameters returning a tuple into a function with 5 parameters returning a [GRA]
// The inverse function is [Uneitherize5]
func Eitherize5[GRA ~func(context.Context) GIOA, F ~func(context.Context, T0, T1, T2, T3, T4) (R, error), GIOA ~func() E.Either[error, R], T0, T1, T2, T3, T4, R any](f F) func(T0, T1, T2, T3, T4) GRA {
return RE.Eitherize5[GRA](f)
return RE.Eitherize5[GRA](f)
}
// Eitherize6 converts a function with 6 parameters returning a tuple into a function with 6 parameters returning a [GRA]
// The inverse function is [Uneitherize6]
func Eitherize6[GRA ~func(context.Context) GIOA, F ~func(context.Context, T0, T1, T2, T3, T4, T5) (R, error), GIOA ~func() E.Either[error, R], T0, T1, T2, T3, T4, T5, R any](f F) func(T0, T1, T2, T3, T4, T5) GRA {
return RE.Eitherize6[GRA](f)
return RE.Eitherize6[GRA](f)
}
// Eitherize7 converts a function with 7 parameters returning a tuple into a function with 7 parameters returning a [GRA]
// The inverse function is [Uneitherize7]
func Eitherize7[GRA ~func(context.Context) GIOA, F ~func(context.Context, T0, T1, T2, T3, T4, T5, T6) (R, error), GIOA ~func() E.Either[error, R], T0, T1, T2, T3, T4, T5, T6, R any](f F) func(T0, T1, T2, T3, T4, T5, T6) GRA {
return RE.Eitherize7[GRA](f)
return RE.Eitherize7[GRA](f)
}
// Eitherize8 converts a function with 8 parameters returning a tuple into a function with 8 parameters returning a [GRA]
// The inverse function is [Uneitherize8]
func Eitherize8[GRA ~func(context.Context) GIOA, F ~func(context.Context, T0, T1, T2, T3, T4, T5, T6, T7) (R, error), GIOA ~func() E.Either[error, R], T0, T1, T2, T3, T4, T5, T6, T7, R any](f F) func(T0, T1, T2, T3, T4, T5, T6, T7) GRA {
return RE.Eitherize8[GRA](f)
return RE.Eitherize8[GRA](f)
}
// Eitherize9 converts a function with 9 parameters returning a tuple into a function with 9 parameters returning a [GRA]
// The inverse function is [Uneitherize9]
func Eitherize9[GRA ~func(context.Context) GIOA, F ~func(context.Context, T0, T1, T2, T3, T4, T5, T6, T7, T8) (R, error), GIOA ~func() E.Either[error, R], T0, T1, T2, T3, T4, T5, T6, T7, T8, R any](f F) func(T0, T1, T2, T3, T4, T5, T6, T7, T8) GRA {
return RE.Eitherize9[GRA](f)
return RE.Eitherize9[GRA](f)
}
// Eitherize10 converts a function with 10 parameters returning a tuple into a function with 10 parameters returning a [GRA]
// The inverse function is [Uneitherize10]
func Eitherize10[GRA ~func(context.Context) GIOA, F ~func(context.Context, T0, T1, T2, T3, T4, T5, T6, T7, T8, T9) (R, error), GIOA ~func() E.Either[error, R], T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, R any](f F) func(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9) GRA {
return RE.Eitherize10[GRA](f)
return RE.Eitherize10[GRA](f)
}

View File

@@ -194,9 +194,9 @@ func Ap[
GRAB ~func(context.Context) GIOAB,
GRA ~func(context.Context) GIOA,
GIOA ~func() E.Either[error, A],
GIOB ~func() E.Either[error, B],
GIOAB ~func() E.Either[error, func(A) B],
GIOA ~func() E.Either[error, A],
A, B any](fa GRA) func(GRAB) GRB {
return F.Bind2nd(MonadAp[GRB, GRA, GRAB], fa)

View File

@@ -4,9 +4,30 @@ import (
"context"
E "github.com/IBM/fp-go/either"
RE "github.com/IBM/fp-go/readerioeither/generic"
F "github.com/IBM/fp-go/function"
RA "github.com/IBM/fp-go/internal/array"
RR "github.com/IBM/fp-go/internal/record"
)
// MonadTraverseArray transforms an array
func MonadTraverseArray[
AS ~[]A,
GRBS ~func(context.Context) GIOBS,
GRB ~func(context.Context) GIOB,
GIOBS ~func() E.Either[error, BS],
GIOB ~func() E.Either[error, B],
BS ~[]B,
A, B any](as AS, f func(A) GRB) GRBS {
return RA.MonadTraverse[AS](
Of[GRBS, GIOBS, BS],
Map[GRBS, func(context.Context) func() E.Either[error, func(B) BS], GIOBS, func() E.Either[error, func(B) BS], BS, func(B) BS],
Ap[GRBS, func(context.Context) func() E.Either[error, func(B) BS], GRB],
as, f,
)
}
// TraverseArray transforms an array
func TraverseArray[
AS ~[]A,
@@ -16,7 +37,14 @@ func TraverseArray[
GIOB ~func() E.Either[error, B],
BS ~[]B,
A, B any](f func(A) GRB) func(AS) GRBS {
return RE.TraverseArray[GRB, GRBS, GIOB, GIOBS, AS](f)
return RA.Traverse[AS](
Of[GRBS, GIOBS, BS],
Map[GRBS, func(context.Context) func() E.Either[error, func(B) BS], GIOBS, func() E.Either[error, func(B) BS], BS, func(B) BS],
Ap[GRBS, func(context.Context) func() E.Either[error, func(B) BS], GRB],
f,
)
}
// SequenceArray converts a homogeneous sequence of either into an either of sequence
@@ -28,7 +56,28 @@ func SequenceArray[
GIOAS ~func() E.Either[error, AS],
GIOA ~func() E.Either[error, A],
A any](ma GAS) GRAS {
return RE.SequenceArray[GRA, GRAS](ma)
return MonadTraverseArray[GAS, GRAS](ma, F.Identity[GRA])
}
// MonadTraverseRecord transforms a record
func MonadTraverseRecord[K comparable,
AS ~map[K]A,
GRBS ~func(context.Context) GIOBS,
GRB ~func(context.Context) GIOB,
GIOBS ~func() E.Either[error, BS],
GIOB ~func() E.Either[error, B],
BS ~map[K]B,
A, B any](ma AS, f func(A) GRB) GRBS {
return RR.MonadTraverse[AS](
Of[GRBS, GIOBS, BS],
Map[GRBS, func(context.Context) func() E.Either[error, func(B) BS], GIOBS, func() E.Either[error, func(B) BS], BS, func(B) BS],
Ap[GRBS, func(context.Context) func() E.Either[error, func(B) BS], GRB],
ma, f,
)
}
// TraverseRecord transforms a record
@@ -41,7 +90,14 @@ func TraverseRecord[K comparable,
BS ~map[K]B,
A, B any](f func(A) GRB) func(AS) GRBS {
return RE.TraverseRecord[GRB, GRBS, GIOB, GIOBS, AS](f)
return RR.Traverse[AS](
Of[GRBS, GIOBS, BS],
Map[GRBS, func(context.Context) func() E.Either[error, func(B) BS], GIOBS, func() E.Either[error, func(B) BS], BS, func(B) BS],
Ap[GRBS, func(context.Context) func() E.Either[error, func(B) BS], GRB],
f,
)
}
// SequenceRecord converts a homogeneous sequence of either into an either of sequence
@@ -53,5 +109,6 @@ func SequenceRecord[K comparable,
GIOAS ~func() E.Either[error, AS],
GIOA ~func() E.Either[error, A],
A any](ma GAS) GRAS {
return RE.SequenceRecord[GRA, GRAS](ma)
return MonadTraverseRecord[K, GAS, GRAS](ma, F.Identity[GRA])
}

View File

@@ -4,7 +4,7 @@ import (
G "github.com/IBM/fp-go/context/readerioeither/generic"
)
// TraverseArray transforms an array
// TraverseArray uses transforms an array [[]A] into [[]ReaderIOEither[B]] and then resolves that into a [ReaderIOEither[[]B]]
func TraverseArray[A, B any](f func(A) ReaderIOEither[B]) func([]A) ReaderIOEither[[]B] {
return G.TraverseArray[[]A, ReaderIOEither[[]B]](f)
}
@@ -14,7 +14,7 @@ func SequenceArray[A any](ma []ReaderIOEither[A]) ReaderIOEither[[]A] {
return G.SequenceArray[[]A, []ReaderIOEither[A], ReaderIOEither[[]A]](ma)
}
// TraverseRecord transforms a record
// TraverseRecord uses transforms a record [map[K]A] into [map[K]ReaderIOEither[B]] and then resolves that into a [ReaderIOEither[map[K]B]]
func TraverseRecord[K comparable, A, B any](f func(A) ReaderIOEither[B]) func(map[K]A) ReaderIOEither[map[K]B] {
return G.TraverseRecord[K, map[K]A, ReaderIOEither[map[K]B]](f)
}