2023-07-18 15:24:00 +02:00
|
|
|
package generic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2023-07-18 15:57:54 +02:00
|
|
|
E "github.com/IBM/fp-go/either"
|
|
|
|
RE "github.com/IBM/fp-go/readerioeither/generic"
|
2023-07-18 15:24:00 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// TraverseArray transforms an array
|
|
|
|
func TraverseArray[
|
|
|
|
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](f func(A) GRB) func(AS) GRBS {
|
|
|
|
return RE.TraverseArray[GRB, GRBS, GIOB, GIOBS, AS](f)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SequenceArray converts a homogeneous sequence of either into an either of sequence
|
|
|
|
func SequenceArray[
|
|
|
|
AS ~[]A,
|
|
|
|
GAS ~[]GRA,
|
|
|
|
GRAS ~func(context.Context) GIOAS,
|
|
|
|
GRA ~func(context.Context) GIOA,
|
|
|
|
GIOAS ~func() E.Either[error, AS],
|
|
|
|
GIOA ~func() E.Either[error, A],
|
|
|
|
A any](ma GAS) GRAS {
|
|
|
|
return RE.SequenceArray[GRA, GRAS](ma)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TraverseRecord transforms a record
|
|
|
|
func TraverseRecord[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](f func(A) GRB) func(AS) GRBS {
|
|
|
|
return RE.TraverseRecord[GRB, GRBS, GIOB, GIOBS, AS](f)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SequenceRecord converts a homogeneous sequence of either into an either of sequence
|
|
|
|
func SequenceRecord[K comparable,
|
|
|
|
AS ~map[K]A,
|
|
|
|
GAS ~map[K]GRA,
|
|
|
|
GRAS ~func(context.Context) GIOAS,
|
|
|
|
GRA ~func(context.Context) GIOA,
|
|
|
|
GIOAS ~func() E.Either[error, AS],
|
|
|
|
GIOA ~func() E.Either[error, A],
|
|
|
|
A any](ma GAS) GRAS {
|
|
|
|
return RE.SequenceRecord[GRA, GRAS](ma)
|
|
|
|
}
|