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

fix: automate eitherize methods

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
This commit is contained in:
Dr. Carsten Leue
2023-07-18 15:24:00 +02:00
parent 2efe4f64c5
commit eb4975e86d
60 changed files with 2106 additions and 430 deletions

View File

@@ -1,18 +1,10 @@
package readerioeither
import (
"context"
CIOE "github.com/ibm/fp-go/context/ioeither"
IOE "github.com/ibm/fp-go/ioeither"
G "github.com/ibm/fp-go/context/readerioeither/generic"
)
// withContext wraps an existing ReaderIOEither and performs a context check for cancellation before delegating
// WithContext wraps an existing ReaderIOEither and performs a context check for cancellation before delegating
func WithContext[A any](ma ReaderIOEither[A]) ReaderIOEither[A] {
return func(ctx context.Context) IOE.IOEither[error, A] {
if err := context.Cause(ctx); err != nil {
return IOE.Left[A](err)
}
return CIOE.WithContext(ctx, ma(ctx))
}
return G.WithContext(ma)
}

View File

@@ -0,0 +1,3 @@
package readerioeither
//go:generate go run ../.. contextreaderioeither --count 10 --filename gen.go

View File

@@ -1,30 +0,0 @@
package readerioeither
import (
"context"
RE "github.com/ibm/fp-go/readerioeither/generic"
)
// these functions curry a golang function with the context as the firsr parameter into a either reader with the context as the last parameter
// this goes back to the advice in https://pkg.go.dev/context to put the context as a first parameter as a convention
func Eitherize0[A any](f func(context.Context) (A, error)) ReaderIOEither[A] {
return RE.Eitherize0[ReaderIOEither[A]](f)
}
func Eitherize1[T1, A any](f func(context.Context, T1) (A, error)) func(T1) ReaderIOEither[A] {
return RE.Eitherize1[ReaderIOEither[A]](f)
}
func Eitherize2[T1, T2, A any](f func(context.Context, T1, T2) (A, error)) func(T1, T2) ReaderIOEither[A] {
return RE.Eitherize2[ReaderIOEither[A]](f)
}
func Eitherize3[T1, T2, T3, A any](f func(context.Context, T1, T2, T3) (A, error)) func(T1, T2, T3) ReaderIOEither[A] {
return RE.Eitherize3[ReaderIOEither[A]](f)
}
func Eitherize4[T1, T2, T3, T4, A any](f func(context.Context, T1, T2, T3, T4) (A, error)) func(T1, T2, T3, T4) ReaderIOEither[A] {
return RE.Eitherize4[ReaderIOEither[A]](f)
}

View File

@@ -3,9 +3,9 @@ package readerioeither
import (
"context"
G "github.com/ibm/fp-go/context/readerioeither/generic"
ET "github.com/ibm/fp-go/either"
EQ "github.com/ibm/fp-go/eq"
G "github.com/ibm/fp-go/readerioeither/generic"
)
// Eq implements the equals predicate for values contained in the IOEither monad

View File

@@ -1,30 +0,0 @@
package readerioeither
import (
"context"
RE "github.com/ibm/fp-go/readerioeither/generic"
)
// these functions curry a golang function with the context as the firsr parameter into a either reader with the context as the last parameter
// this goes back to the advice in https://pkg.go.dev/context to put the context as a first parameter as a convention
func From0[A any](f func(context.Context) func() (A, error)) ReaderIOEither[A] {
return RE.From0[ReaderIOEither[A]](f)
}
func From1[T1, A any](f func(context.Context, T1) func() (A, error)) func(T1) ReaderIOEither[A] {
return RE.From1[ReaderIOEither[A]](f)
}
func From2[T1, T2, A any](f func(context.Context, T1, T2) func() (A, error)) func(T1, T2) ReaderIOEither[A] {
return RE.From2[ReaderIOEither[A]](f)
}
func From3[T1, T2, T3, A any](f func(context.Context, T1, T2, T3) func() (A, error)) func(T1, T2, T3) ReaderIOEither[A] {
return RE.From3[ReaderIOEither[A]](f)
}
func From4[T1, T2, T3, T4, A any](f func(context.Context, T1, T2, T3, T4) func() (A, error)) func(T1, T2, T3, T4) ReaderIOEither[A] {
return RE.From4[ReaderIOEither[A]](f)
}

View File

@@ -0,0 +1,77 @@
// Code generated by go generate; DO NOT EDIT.
// This file was generated by robots at
// 2023-07-18 15:21:14.8906482 +0200 CEST m=+0.127356001
package readerioeither
import (
"context"
G "github.com/ibm/fp-go/context/readerioeither/generic"
)
// 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)
}
// 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)
}
// 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)
}
// 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)
}
// 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)
}
// 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)
}
// 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)
}
// 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)
}
// 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)
}
// 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)
}
// 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)
}

View File

@@ -0,0 +1,19 @@
package generic
import (
"context"
CIOE "github.com/ibm/fp-go/context/ioeither/generic"
E "github.com/ibm/fp-go/either"
IOE "github.com/ibm/fp-go/ioeither/generic"
)
// withContext wraps an existing ReaderIOEither and performs a context check for cancellation before delegating
func WithContext[GRA ~func(context.Context) GIOA, GIOA ~func() E.Either[error, A], A any](ma GRA) GRA {
return func(ctx context.Context) GIOA {
if err := context.Cause(ctx); err != nil {
return IOE.Left[GIOA](err)
}
return CIOE.WithContext(ctx, ma(ctx))
}
}

View File

@@ -0,0 +1,15 @@
package generic
import (
"context"
E "github.com/ibm/fp-go/either"
ET "github.com/ibm/fp-go/either"
EQ "github.com/ibm/fp-go/eq"
G "github.com/ibm/fp-go/readerioeither/generic"
)
// Eq implements the equals predicate for values contained in the IOEither monad
func Eq[GRA ~func(context.Context) GIOA, GIOA ~func() E.Either[error, A], A any](eq EQ.Eq[ET.Either[error, A]]) func(context.Context) EQ.Eq[GRA] {
return G.Eq[GRA](eq)
}

View File

@@ -0,0 +1,78 @@
// Code generated by go generate; DO NOT EDIT.
// This file was generated by robots at
// 2023-07-18 15:21:14.8906482 +0200 CEST m=+0.127356001
package generic
import (
"context"
E "github.com/ibm/fp-go/either"
RE "github.com/ibm/fp-go/readerioeither/generic"
)
// 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)
}
// 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)
}
// 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)
}
// 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)
}
// 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)
}
// 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)
}
// 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)
}
// 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)
}
// 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)
}
// 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)
}
// 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)
}

View File

@@ -0,0 +1,464 @@
package generic
import (
"context"
"time"
E "github.com/ibm/fp-go/either"
ER "github.com/ibm/fp-go/errors"
F "github.com/ibm/fp-go/function"
IO "github.com/ibm/fp-go/io/generic"
IOE "github.com/ibm/fp-go/ioeither/generic"
O "github.com/ibm/fp-go/option"
RIE "github.com/ibm/fp-go/readerioeither/generic"
)
func FromEither[
GRA ~func(context.Context) GIOA,
GIOA ~func() E.Either[error, A],
A any](e E.Either[error, A]) GRA {
return RIE.FromEither[GRA](e)
}
func RightReader[
GRA ~func(context.Context) GIOA,
GR ~func(context.Context) A,
GIOA ~func() E.Either[error, A],
A any](r GR) GRA {
return RIE.RightReader[GR, GRA](r)
}
func LeftReader[
GRA ~func(context.Context) GIOA,
GR ~func(context.Context) error,
GIOA ~func() E.Either[error, A],
A any](l GR) GRA {
return RIE.LeftReader[GR, GRA](l)
}
func Left[
GRA ~func(context.Context) GIOA,
GIOA ~func() E.Either[error, A],
A any](l error) GRA {
return RIE.Left[GRA](l)
}
func Right[
GRA ~func(context.Context) GIOA,
GIOA ~func() E.Either[error, A],
A any](r A) GRA {
return RIE.Right[GRA](r)
}
func FromReader[
GRA ~func(context.Context) GIOA,
GR ~func(context.Context) A,
GIOA ~func() E.Either[error, A],
A any](r GR) GRA {
return RIE.FromReader[GR, GRA](r)
}
func MonadMap[
GRA ~func(context.Context) GIOA,
GRB ~func(context.Context) GIOB,
GIOA ~func() E.Either[error, A],
GIOB ~func() E.Either[error, B],
A, B any](fa GRA, f func(A) B) GRB {
return RIE.MonadMap[GRA, GRB](fa, f)
}
func Map[
GRA ~func(context.Context) GIOA,
GRB ~func(context.Context) GIOB,
GIOA ~func() E.Either[error, A],
GIOB ~func() E.Either[error, B],
A, B any](f func(A) B) func(GRA) GRB {
return RIE.Map[GRA, GRB](f)
}
func MonadChain[
GRA ~func(context.Context) GIOA,
GRB ~func(context.Context) GIOB,
GIOA ~func() E.Either[error, A],
GIOB ~func() E.Either[error, B],
A, B any](ma GRA, f func(A) GRB) GRB {
return RIE.MonadChain(ma, f)
}
func Chain[
GRA ~func(context.Context) GIOA,
GRB ~func(context.Context) GIOB,
GIOA ~func() E.Either[error, A],
GIOB ~func() E.Either[error, B],
A, B any](f func(A) GRB) func(GRA) GRB {
return RIE.Chain[GRA](f)
}
func MonadChainFirst[
GRA ~func(context.Context) GIOA,
GRB ~func(context.Context) GIOB,
GIOA ~func() E.Either[error, A],
GIOB ~func() E.Either[error, B],
A, B any](ma GRA, f func(A) GRB) GRA {
return RIE.MonadChainFirst(ma, f)
}
func ChainFirst[
GRA ~func(context.Context) GIOA,
GRB ~func(context.Context) GIOB,
GIOA ~func() E.Either[error, A],
GIOB ~func() E.Either[error, B],
A, B any](f func(A) GRB) func(GRA) GRA {
return RIE.ChainFirst[GRA](f)
}
func Of[
GRA ~func(context.Context) GIOA,
GIOA ~func() E.Either[error, A],
A any](a A) GRA {
return RIE.Of[GRA](a)
}
// withCancelCauseFunc wraps an IOEither such that in case of an error the cancel function is invoked
func withCancelCauseFunc[
GIOA ~func() E.Either[error, A],
A any](cancel context.CancelCauseFunc, ma GIOA) GIOA {
return F.Pipe3(
ma,
IOE.Swap[GIOA, func() E.Either[A, error]],
IOE.ChainFirstIOK[func() E.Either[A, error], func() any](func(err error) func() any {
return IO.MakeIO[func() any](func() any {
cancel(err)
return nil
})
}),
IOE.Swap[func() E.Either[A, error], GIOA],
)
}
// MonadAp implements the `Ap` function for a reader with context. It creates a sub-context that will
// be canceled if any of the input operations errors out or
func MonadAp[
GRB ~func(context.Context) GIOB,
GRA ~func(context.Context) GIOA,
GRAB ~func(context.Context) GIOAB,
GIOA ~func() E.Either[error, A],
GIOB ~func() E.Either[error, B],
GIOAB ~func() E.Either[error, func(A) B],
A, B any](fab GRAB, fa GRA) GRB {
// context sensitive input
cfab := WithContext(fab)
cfa := WithContext(fa)
return func(ctx context.Context) GIOB {
// quick check for cancellation
if err := context.Cause(ctx); err != nil {
return IOE.Left[GIOB](err)
}
return func() E.Either[error, B] {
// quick check for cancellation
if err := context.Cause(ctx); err != nil {
return E.Left[B](err)
}
// create sub-contexts for fa and fab, so they can cancel one other
ctxSub, cancelSub := context.WithCancelCause(ctx)
defer cancelSub(nil) // cancel has to be called in all paths
fabIOE := withCancelCauseFunc(cancelSub, cfab(ctxSub))
faIOE := withCancelCauseFunc(cancelSub, cfa(ctxSub))
return IOE.MonadAp[GIOA, GIOB, GIOAB](fabIOE, faIOE)()
}
}
}
func Ap[
GRB ~func(context.Context) GIOB,
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],
A, B any](fa GRA) func(GRAB) GRB {
return F.Bind2nd(MonadAp[GRB, GRA, GRAB], fa)
}
func FromPredicate[
GRA ~func(context.Context) GIOA,
GIOA ~func() E.Either[error, A],
A any](pred func(A) bool, onFalse func(A) error) func(A) GRA {
return RIE.FromPredicate[GRA](pred, onFalse)
}
func Fold[
GRB ~func(context.Context) GIOB,
GRA ~func(context.Context) GIOA,
GIOA ~func() E.Either[error, A],
GIOB ~func() B,
A, B any](onLeft func(error) GRB, onRight func(A) GRB) func(GRA) GRB {
return RIE.Fold[GRB, GRA](onLeft, onRight)
}
func GetOrElse[
GRB ~func(context.Context) GIOB,
GRA ~func(context.Context) GIOA,
GIOA ~func() E.Either[error, A],
GIOB ~func() A,
A any](onLeft func(error) GRB) func(GRA) GRB {
return RIE.GetOrElse[GRB, GRA](onLeft)
}
func OrElse[
GRA ~func(context.Context) GIOA,
GIOA ~func() E.Either[error, A],
A any](onLeft func(error) GRA) func(GRA) GRA {
return RIE.OrElse[GRA](onLeft)
}
func OrLeft[
GRA ~func(context.Context) GIOA,
GRB ~func(context.Context) GIOB,
GIOA ~func() E.Either[error, A],
GIOB ~func() error,
A any](onLeft func(error) GRB) func(GRA) GRA {
return RIE.OrLeft[GRA, GRB, GRA](onLeft)
}
func Ask[
GRA ~func(context.Context) GIOA,
GIOA ~func() E.Either[error, context.Context],
]() GRA {
return RIE.Ask[GRA]()
}
func Asks[
GRA ~func(context.Context) GIOA,
GRB ~func(context.Context) A,
GIOA ~func() E.Either[error, A],
A any](r GRB) GRA {
return RIE.Asks[GRB, GRA](r)
}
func MonadChainEitherK[
GRA ~func(context.Context) GIOA,
GRB ~func(context.Context) GIOB,
GIOA ~func() E.Either[error, A],
GIOB ~func() E.Either[error, B],
A, B any](ma GRA, f func(A) E.Either[error, B]) GRB {
return RIE.MonadChainEitherK[GRA, GRB](ma, f)
}
func ChainEitherK[
GRA ~func(context.Context) GIOA,
GRB ~func(context.Context) GIOB,
GIOA ~func() E.Either[error, A],
GIOB ~func() E.Either[error, B],
A, B any](f func(A) E.Either[error, B]) func(ma GRA) GRB {
return RIE.ChainEitherK[GRA, GRB](f)
}
func MonadChainFirstEitherK[
GRA ~func(context.Context) GIOA,
GIOA ~func() E.Either[error, A],
A, B any](ma GRA, f func(A) E.Either[error, B]) GRA {
return RIE.MonadChainFirstEitherK[GRA](ma, f)
}
func ChainFirstEitherK[
GRA ~func(context.Context) GIOA,
GIOA ~func() E.Either[error, A],
A, B any](f func(A) E.Either[error, B]) func(ma GRA) GRA {
return RIE.ChainFirstEitherK[GRA](f)
}
func ChainOptionK[
GRA ~func(context.Context) GIOA,
GRB ~func(context.Context) GIOB,
GIOB ~func() E.Either[error, B],
GIOA ~func() E.Either[error, A],
A, B any](onNone func() error) func(func(A) O.Option[B]) func(GRA) GRB {
return RIE.ChainOptionK[GRA, GRB](onNone)
}
func FromIOEither[
GRA ~func(context.Context) GIOA,
GIOA ~func() E.Either[error, A],
A any](t GIOA) GRA {
return RIE.FromIOEither[GRA](t)
}
func FromIO[
GRA ~func(context.Context) GIOA,
GIOA ~func() E.Either[error, A],
GIOB ~func() A,
A any](t GIOB) GRA {
return RIE.FromIO[GRA](t)
}
// Never returns a 'ReaderIOEither' that never returns, except if its context gets canceled
func Never[
GRA ~func(context.Context) GIOA,
GIOA ~func() E.Either[error, A],
A any]() GRA {
return func(ctx context.Context) GIOA {
return IOE.MakeIO(func() E.Either[error, A] {
<-ctx.Done()
return E.Left[A](context.Cause(ctx))
})
}
}
func MonadChainIOK[
GRB ~func(context.Context) GIOB,
GRA ~func(context.Context) GIOA,
GIOA ~func() E.Either[error, A],
GIOB ~func() E.Either[error, B],
GIO ~func() B,
A, B any](ma GRA, f func(A) GIO) GRB {
return RIE.MonadChainIOK[GRA, GRB](ma, f)
}
func ChainIOK[
GRB ~func(context.Context) GIOB,
GRA ~func(context.Context) GIOA,
GIOA ~func() E.Either[error, A],
GIOB ~func() E.Either[error, B],
GIO ~func() B,
A, B any](f func(A) GIO) func(ma GRA) GRB {
return RIE.ChainIOK[GRA, GRB](f)
}
func MonadChainFirstIOK[
GRA ~func(context.Context) GIOA,
GIOA ~func() E.Either[error, A],
GIO ~func() B,
A, B any](ma GRA, f func(A) GIO) GRA {
return RIE.MonadChainFirstIOK[GRA](ma, f)
}
func ChainFirstIOK[
GRA ~func(context.Context) GIOA,
GIOA ~func() E.Either[error, A],
GIO ~func() B,
A, B any](f func(A) GIO) func(ma GRA) GRA {
return RIE.ChainFirstIOK[GRA](f)
}
func ChainIOEitherK[
GRA ~func(context.Context) GIOA,
GRB ~func(context.Context) GIOB,
GIOA ~func() E.Either[error, A],
GIOB ~func() E.Either[error, B],
A, B any](f func(A) GIOB) func(ma GRA) GRB {
return RIE.ChainIOEitherK[GRA, GRB](f)
}
// Delay creates an operation that passes in the value after some delay
func Delay[
GRA ~func(context.Context) GIOA,
GIOA ~func() E.Either[error, A],
A any](delay time.Duration) func(ma GRA) GRA {
return func(ma GRA) GRA {
return func(ctx context.Context) GIOA {
return IOE.MakeIO(func() E.Either[error, A] {
// manage the timeout
timeoutCtx, cancelTimeout := context.WithTimeout(ctx, delay)
defer cancelTimeout()
// whatever comes first
select {
case <-timeoutCtx.Done():
return ma(ctx)()
case <-ctx.Done():
return E.Left[A](context.Cause(ctx))
}
})
}
}
}
// Timer will return the current time after an initial delay
func Timer[
GRA ~func(context.Context) GIOA,
GIOA ~func() E.Either[error, time.Time],
](delay time.Duration) GRA {
return F.Pipe2(
IO.Now[func() time.Time](),
FromIO[GRA, GIOA, func() time.Time],
Delay[GRA](delay),
)
}
// Defer creates an IO by creating a brand new IO via a generator function, each time
func Defer[
GRA ~func(context.Context) GIOA,
GIOA ~func() E.Either[error, A],
A any](gen func() GRA) GRA {
return RIE.Defer[GRA](gen)
}
// TryCatch wraps a reader returning a tuple as an error into ReaderIOEither
func TryCatch[
GRA ~func(context.Context) GIOA,
GIOA ~func() E.Either[error, A],
A any](f func(context.Context) func() (A, error)) GRA {
return RIE.TryCatch[GRA](f, ER.IdentityError)
}

View File

@@ -0,0 +1,25 @@
package generic
import (
"context"
E "github.com/ibm/fp-go/either"
F "github.com/ibm/fp-go/function"
RIE "github.com/ibm/fp-go/readerioeither/generic"
)
// WithResource constructs a function that creates a resource, then operates on it and then releases the resource
func WithResource[
GRA ~func(context.Context) GIOA,
GRR ~func(context.Context) GIOR,
GRANY ~func(context.Context) GIOANY,
GIOR ~func() E.Either[error, R],
GIOA ~func() E.Either[error, A],
GIOANY ~func() E.Either[error, ANY],
R, A, ANY any](onCreate GRR, onRelease func(R) GRANY) func(func(R) GRA) GRA {
// wraps the callback functions with a context check
return F.Flow2(
F.Bind2nd(F.Flow2[func(R) GRA, func(GRA) GRA, R, GRA, GRA], WithContext[GRA]),
RIE.WithResource[GRA](WithContext(onCreate), onRelease),
)
}

View File

@@ -0,0 +1,85 @@
package generic
import (
"context"
E "github.com/ibm/fp-go/either"
RE "github.com/ibm/fp-go/readerioeither/generic"
T "github.com/ibm/fp-go/tuple"
)
// SequenceT converts n inputs of higher kinded types into a higher kinded types of n strongly typed values, represented as a tuple
func SequenceT1[
GRT ~func(context.Context) GIOT,
GRA ~func(context.Context) GIOA,
GIOA ~func() E.Either[error, A],
GIOT ~func() E.Either[error, T.Tuple1[A]],
A any](a GRA) GRT {
return RE.SequenceT1[
GRA,
GRT,
](a)
}
func SequenceT2[
GRT ~func(context.Context) GIOT,
GRA ~func(context.Context) GIOA,
GRB ~func(context.Context) GIOB,
GIOA ~func() E.Either[error, A],
GIOB ~func() E.Either[error, B],
GIOT ~func() E.Either[error, T.Tuple2[A, B]],
A, B any](a GRA, b GRB) GRT {
return RE.SequenceT2[
GRA,
GRB,
GRT,
](a, b)
}
func SequenceT3[
GRT ~func(context.Context) GIOT,
GRA ~func(context.Context) GIOA,
GRB ~func(context.Context) GIOB,
GRC ~func(context.Context) GIOC,
GIOA ~func() E.Either[error, A],
GIOB ~func() E.Either[error, B],
GIOC ~func() E.Either[error, C],
GIOT ~func() E.Either[error, T.Tuple3[A, B, C]],
A, B, C any](a GRA, b GRB, c GRC) GRT {
return RE.SequenceT3[
GRA,
GRB,
GRC,
GRT,
](a, b, c)
}
func SequenceT4[
GRT ~func(context.Context) GIOT,
GRA ~func(context.Context) GIOA,
GRB ~func(context.Context) GIOB,
GRC ~func(context.Context) GIOC,
GRD ~func(context.Context) GIOD,
GIOA ~func() E.Either[error, A],
GIOB ~func() E.Either[error, B],
GIOC ~func() E.Either[error, C],
GIOD ~func() E.Either[error, D],
GIOT ~func() E.Either[error, T.Tuple4[A, B, C, D]],
A, B, C, D any](a GRA, b GRB, c GRC, d GRD) GRT {
return RE.SequenceT4[
GRA,
GRB,
GRC,
GRD,
GRT,
](a, b, c, d)
}

View File

@@ -0,0 +1,57 @@
package generic
import (
"context"
E "github.com/ibm/fp-go/either"
RE "github.com/ibm/fp-go/readerioeither/generic"
)
// 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)
}

View File

@@ -71,21 +71,6 @@ func ReadFullResponse(client Client) func(Requester) RIOE.ReaderIOEither[H.FullR
}
}
// func test(resp *http.Response) IOE.IOEither[error, H.FullResponse] {
// x := F.Pipe3(
// resp,
// T.Replicate2[*http.Response],
// T.Map2(
// IOE.Of[error, *http.Response],
// F.Flow3(
// H.GetBody,
// IOE.Of[error, io.ReadCloser],
// IOEF.ReadAll[io.ReadCloser],
// ),
// ),
// )
// }
// ReadAll sends a request and reads the response as bytes
func ReadAll(client Client) func(Requester) RIOE.ReaderIOEither[[]byte] {
return F.Flow2(

View File

@@ -6,238 +6,172 @@ import (
R "github.com/ibm/fp-go/context/reader"
RIO "github.com/ibm/fp-go/context/readerio"
G "github.com/ibm/fp-go/context/readerioeither/generic"
ET "github.com/ibm/fp-go/either"
ER "github.com/ibm/fp-go/errors"
F "github.com/ibm/fp-go/function"
IO "github.com/ibm/fp-go/io"
IOE "github.com/ibm/fp-go/ioeither"
O "github.com/ibm/fp-go/option"
RIE "github.com/ibm/fp-go/readerioeither/generic"
)
func FromEither[A any](e ET.Either[error, A]) ReaderIOEither[A] {
return RIE.FromEither[ReaderIOEither[A]](e)
return G.FromEither[ReaderIOEither[A]](e)
}
func RightReader[A any](r R.Reader[A]) ReaderIOEither[A] {
return RIE.RightReader[R.Reader[A], ReaderIOEither[A]](r)
return G.RightReader[ReaderIOEither[A]](r)
}
func LeftReader[A any](l R.Reader[error]) ReaderIOEither[A] {
return RIE.LeftReader[R.Reader[error], ReaderIOEither[A]](l)
return G.LeftReader[ReaderIOEither[A]](l)
}
func Left[A any](l error) ReaderIOEither[A] {
return RIE.Left[ReaderIOEither[A]](l)
return G.Left[ReaderIOEither[A]](l)
}
func Right[A any](r A) ReaderIOEither[A] {
return RIE.Right[ReaderIOEither[A]](r)
return G.Right[ReaderIOEither[A]](r)
}
func FromReader[A any](r R.Reader[A]) ReaderIOEither[A] {
return RIE.FromReader[R.Reader[A], ReaderIOEither[A]](r)
return G.FromReader[ReaderIOEither[A]](r)
}
func MonadMap[A, B any](fa ReaderIOEither[A], f func(A) B) ReaderIOEither[B] {
return RIE.MonadMap[ReaderIOEither[A], ReaderIOEither[B]](fa, f)
return G.MonadMap[ReaderIOEither[A], ReaderIOEither[B]](fa, f)
}
func Map[A, B any](f func(A) B) func(ReaderIOEither[A]) ReaderIOEither[B] {
return RIE.Map[ReaderIOEither[A], ReaderIOEither[B]](f)
return G.Map[ReaderIOEither[A], ReaderIOEither[B]](f)
}
func MonadChain[A, B any](ma ReaderIOEither[A], f func(A) ReaderIOEither[B]) ReaderIOEither[B] {
return RIE.MonadChain(ma, f)
return G.MonadChain(ma, f)
}
func Chain[A, B any](f func(A) ReaderIOEither[B]) func(ReaderIOEither[A]) ReaderIOEither[B] {
return RIE.Chain[ReaderIOEither[A]](f)
return G.Chain[ReaderIOEither[A]](f)
}
func MonadChainFirst[A, B any](ma ReaderIOEither[A], f func(A) ReaderIOEither[B]) ReaderIOEither[A] {
return RIE.MonadChainFirst(ma, f)
return G.MonadChainFirst(ma, f)
}
func ChainFirst[A, B any](f func(A) ReaderIOEither[B]) func(ReaderIOEither[A]) ReaderIOEither[A] {
return RIE.ChainFirst[ReaderIOEither[A]](f)
return G.ChainFirst[ReaderIOEither[A]](f)
}
func Of[A any](a A) ReaderIOEither[A] {
return RIE.Of[ReaderIOEither[A]](a)
}
// withCancelCauseFunc wraps an IOEither such that in case of an error the cancel function is invoked
func withCancelCauseFunc[A any](cancel context.CancelCauseFunc, ma IOE.IOEither[error, A]) IOE.IOEither[error, A] {
return F.Pipe3(
ma,
IOE.Swap[error, A],
IOE.ChainFirstIOK[A, error, any](func(err error) IO.IO[any] {
return IO.MakeIO(func() any {
cancel(err)
return nil
})
}),
IOE.Swap[A, error],
)
return G.Of[ReaderIOEither[A]](a)
}
// MonadAp implements the `Ap` function for a reader with context. It creates a sub-context that will
// be canceled if any of the input operations errors out or
func MonadAp[A, B any](fab ReaderIOEither[func(A) B], fa ReaderIOEither[A]) ReaderIOEither[B] {
// context sensitive input
cfab := WithContext(fab)
cfa := WithContext(fa)
return func(ctx context.Context) IOE.IOEither[error, B] {
// quick check for cancellation
if err := context.Cause(ctx); err != nil {
return IOE.Left[B](err)
}
return func() ET.Either[error, B] {
// quick check for cancellation
if err := context.Cause(ctx); err != nil {
return ET.Left[B](err)
}
// create sub-contexts for fa and fab, so they can cancel one other
ctxSub, cancelSub := context.WithCancelCause(ctx)
defer cancelSub(nil) // cancel has to be called in all paths
fabIOE := withCancelCauseFunc(cancelSub, cfab(ctxSub))
faIOE := withCancelCauseFunc(cancelSub, cfa(ctxSub))
return IOE.MonadAp(fabIOE, faIOE)()
}
}
func MonadAp[B, A any](fab ReaderIOEither[func(A) B], fa ReaderIOEither[A]) ReaderIOEither[B] {
return G.MonadAp[ReaderIOEither[B]](fab, fa)
}
func Ap[A, B any](fa ReaderIOEither[A]) func(ReaderIOEither[func(A) B]) ReaderIOEither[B] {
return F.Bind2nd(MonadAp[A, B], fa)
func Ap[B, A any](fa ReaderIOEither[A]) func(ReaderIOEither[func(A) B]) ReaderIOEither[B] {
return G.Ap[ReaderIOEither[B], ReaderIOEither[func(A) B]](fa)
}
func FromPredicate[A any](pred func(A) bool, onFalse func(A) error) func(A) ReaderIOEither[A] {
return RIE.FromPredicate[ReaderIOEither[A]](pred, onFalse)
return G.FromPredicate[ReaderIOEither[A]](pred, onFalse)
}
func Fold[A, B any](onLeft func(error) RIO.ReaderIO[B], onRight func(A) RIO.ReaderIO[B]) func(ReaderIOEither[A]) RIO.ReaderIO[B] {
return RIE.Fold[RIO.ReaderIO[B], ReaderIOEither[A]](onLeft, onRight)
return G.Fold[RIO.ReaderIO[B], ReaderIOEither[A]](onLeft, onRight)
}
func GetOrElse[A any](onLeft func(error) RIO.ReaderIO[A]) func(ReaderIOEither[A]) RIO.ReaderIO[A] {
return RIE.GetOrElse[RIO.ReaderIO[A], ReaderIOEither[A]](onLeft)
return G.GetOrElse[RIO.ReaderIO[A], ReaderIOEither[A]](onLeft)
}
func OrElse[A any](onLeft func(error) ReaderIOEither[A]) func(ReaderIOEither[A]) ReaderIOEither[A] {
return RIE.OrElse[ReaderIOEither[A]](onLeft)
return G.OrElse[ReaderIOEither[A]](onLeft)
}
func OrLeft[A any](onLeft func(error) RIO.ReaderIO[error]) func(ReaderIOEither[A]) ReaderIOEither[A] {
return RIE.OrLeft[ReaderIOEither[A], RIO.ReaderIO[error], ReaderIOEither[A]](onLeft)
return G.OrLeft[ReaderIOEither[A], RIO.ReaderIO[error]](onLeft)
}
func Ask() ReaderIOEither[context.Context] {
return RIE.Ask[ReaderIOEither[context.Context]]()
return G.Ask[ReaderIOEither[context.Context]]()
}
func Asks[A any](r R.Reader[A]) ReaderIOEither[A] {
return RIE.Asks[R.Reader[A], ReaderIOEither[A]](r)
return G.Asks[ReaderIOEither[A]](r)
}
func MonadChainEitherK[A, B any](ma ReaderIOEither[A], f func(A) ET.Either[error, B]) ReaderIOEither[B] {
return RIE.MonadChainEitherK[ReaderIOEither[A], ReaderIOEither[B]](ma, f)
return G.MonadChainEitherK[ReaderIOEither[A], ReaderIOEither[B]](ma, f)
}
func ChainEitherK[A, B any](f func(A) ET.Either[error, B]) func(ma ReaderIOEither[A]) ReaderIOEither[B] {
return RIE.ChainEitherK[ReaderIOEither[A], ReaderIOEither[B]](f)
return G.ChainEitherK[ReaderIOEither[A], ReaderIOEither[B]](f)
}
func MonadChainFirstEitherK[A, B any](ma ReaderIOEither[A], f func(A) ET.Either[error, B]) ReaderIOEither[A] {
return RIE.MonadChainFirstEitherK[ReaderIOEither[A]](ma, f)
return G.MonadChainFirstEitherK[ReaderIOEither[A]](ma, f)
}
func ChainFirstEitherK[A, B any](f func(A) ET.Either[error, B]) func(ma ReaderIOEither[A]) ReaderIOEither[A] {
return RIE.ChainFirstEitherK[ReaderIOEither[A]](f)
return G.ChainFirstEitherK[ReaderIOEither[A]](f)
}
func ChainOptionK[A, B any](onNone func() error) func(func(A) O.Option[B]) func(ReaderIOEither[A]) ReaderIOEither[B] {
return RIE.ChainOptionK[ReaderIOEither[A], ReaderIOEither[B]](onNone)
return G.ChainOptionK[ReaderIOEither[A], ReaderIOEither[B]](onNone)
}
func FromIOEither[A any](t IOE.IOEither[error, A]) ReaderIOEither[A] {
return RIE.FromIOEither[ReaderIOEither[A]](t)
return G.FromIOEither[ReaderIOEither[A]](t)
}
func FromIO[A any](t IO.IO[A]) ReaderIOEither[A] {
return RIE.FromIO[ReaderIOEither[A]](t)
return G.FromIO[ReaderIOEither[A]](t)
}
// Never returns a 'ReaderIOEither' that never returns, except if its context gets canceled
func Never[A any]() ReaderIOEither[A] {
return func(ctx context.Context) IOE.IOEither[error, A] {
return IOE.MakeIO(func() ET.Either[error, A] {
<-ctx.Done()
return ET.Left[A](context.Cause(ctx))
})
}
return G.Never[ReaderIOEither[A]]()
}
func MonadChainIOK[A, B any](ma ReaderIOEither[A], f func(A) IO.IO[B]) ReaderIOEither[B] {
return RIE.MonadChainIOK[ReaderIOEither[A], ReaderIOEither[B]](ma, f)
return G.MonadChainIOK[ReaderIOEither[B], ReaderIOEither[A]](ma, f)
}
func ChainIOK[A, B any](f func(A) IO.IO[B]) func(ma ReaderIOEither[A]) ReaderIOEither[B] {
return RIE.ChainIOK[ReaderIOEither[A], ReaderIOEither[B]](f)
return G.ChainIOK[ReaderIOEither[B], ReaderIOEither[A]](f)
}
func MonadChainFirstIOK[A, B any](ma ReaderIOEither[A], f func(A) IO.IO[B]) ReaderIOEither[A] {
return RIE.MonadChainFirstIOK[ReaderIOEither[A]](ma, f)
return G.MonadChainFirstIOK[ReaderIOEither[A]](ma, f)
}
func ChainFirstIOK[A, B any](f func(A) IO.IO[B]) func(ma ReaderIOEither[A]) ReaderIOEither[A] {
return RIE.ChainFirstIOK[ReaderIOEither[A]](f)
return G.ChainFirstIOK[ReaderIOEither[A]](f)
}
func ChainIOEitherK[A, B any](f func(A) IOE.IOEither[error, B]) func(ma ReaderIOEither[A]) ReaderIOEither[B] {
return RIE.ChainIOEitherK[ReaderIOEither[A], ReaderIOEither[B]](f)
return G.ChainIOEitherK[ReaderIOEither[A], ReaderIOEither[B]](f)
}
// Delay creates an operation that passes in the value after some delay
func Delay[A any](delay time.Duration) func(ma ReaderIOEither[A]) ReaderIOEither[A] {
return func(ma ReaderIOEither[A]) ReaderIOEither[A] {
return func(ctx context.Context) IOE.IOEither[error, A] {
return IOE.MakeIO(func() ET.Either[error, A] {
// manage the timeout
timeoutCtx, cancelTimeout := context.WithTimeout(ctx, delay)
defer cancelTimeout()
// whatever comes first
select {
case <-timeoutCtx.Done():
return ma(ctx)()
case <-ctx.Done():
return ET.Left[A](context.Cause(ctx))
}
})
}
}
return G.Delay[ReaderIOEither[A]](delay)
}
// Timer will return the current time after an initial delay
func Timer(delay time.Duration) ReaderIOEither[time.Time] {
return F.Pipe2(
IO.Now,
FromIO[time.Time],
Delay[time.Time](delay),
)
return G.Timer[ReaderIOEither[time.Time]](delay)
}
// Defer creates an IO by creating a brand new IO via a generator function, each time
func Defer[A any](gen func() ReaderIOEither[A]) ReaderIOEither[A] {
return RIE.Defer[ReaderIOEither[A]](gen)
return G.Defer[ReaderIOEither[A]](gen)
}
// TryCatch wraps a reader returning a tuple as an error into ReaderIOEither
func TryCatch[A any](f func(context.Context) func() (A, error)) ReaderIOEither[A] {
return RIE.TryCatch[ReaderIOEither[A]](f, ER.IdentityError)
return G.TryCatch[ReaderIOEither[A]](f)
}

View File

@@ -1,15 +1,11 @@
package readerioeither
import (
F "github.com/ibm/fp-go/function"
RIE "github.com/ibm/fp-go/readerioeither/generic"
G "github.com/ibm/fp-go/context/readerioeither/generic"
)
// WithResource constructs a function that creates a resource, then operates on it and then releases the resource
func WithResource[R, A any](onCreate ReaderIOEither[R], onRelease func(R) ReaderIOEither[any]) func(func(R) ReaderIOEither[A]) ReaderIOEither[A] {
func WithResource[R, A, ANY any](onCreate ReaderIOEither[R], onRelease func(R) ReaderIOEither[ANY]) func(func(R) ReaderIOEither[A]) ReaderIOEither[A] {
// wraps the callback functions with a context check
return F.Flow2(
F.Bind2nd(F.Flow2[func(R) ReaderIOEither[A], func(ReaderIOEither[A]) ReaderIOEither[A], R, ReaderIOEither[A], ReaderIOEither[A]], WithContext[A]),
RIE.WithResource[ReaderIOEither[A]](WithContext(onCreate), onRelease),
)
return G.WithResource[ReaderIOEither[A]](onCreate, onRelease)
}

View File

@@ -1,42 +1,24 @@
package readerioeither
import (
RE "github.com/ibm/fp-go/readerioeither/generic"
G "github.com/ibm/fp-go/context/readerioeither/generic"
T "github.com/ibm/fp-go/tuple"
)
// SequenceT converts n inputs of higher kinded types into a higher kinded types of n strongly typed values, represented as a tuple
func SequenceT1[A any](a ReaderIOEither[A]) ReaderIOEither[T.Tuple1[A]] {
return RE.SequenceT1[
ReaderIOEither[A],
ReaderIOEither[T.Tuple1[A]],
](a)
return G.SequenceT1[ReaderIOEither[T.Tuple1[A]]](a)
}
func SequenceT2[A, B any](a ReaderIOEither[A], b ReaderIOEither[B]) ReaderIOEither[T.Tuple2[A, B]] {
return RE.SequenceT2[
ReaderIOEither[A],
ReaderIOEither[B],
ReaderIOEither[T.Tuple2[A, B]],
](a, b)
return G.SequenceT2[ReaderIOEither[T.Tuple2[A, B]]](a, b)
}
func SequenceT3[A, B, C any](a ReaderIOEither[A], b ReaderIOEither[B], c ReaderIOEither[C]) ReaderIOEither[T.Tuple3[A, B, C]] {
return RE.SequenceT3[
ReaderIOEither[A],
ReaderIOEither[B],
ReaderIOEither[C],
ReaderIOEither[T.Tuple3[A, B, C]],
](a, b, c)
return G.SequenceT3[ReaderIOEither[T.Tuple3[A, B, C]]](a, b, c)
}
func SequenceT4[A, B, C, D any](a ReaderIOEither[A], b ReaderIOEither[B], c ReaderIOEither[C], d ReaderIOEither[D]) ReaderIOEither[T.Tuple4[A, B, C, D]] {
return RE.SequenceT4[
ReaderIOEither[A],
ReaderIOEither[B],
ReaderIOEither[C],
ReaderIOEither[D],
ReaderIOEither[T.Tuple4[A, B, C, D]],
](a, b, c, d)
return G.SequenceT4[ReaderIOEither[T.Tuple4[A, B, C, D]]](a, b, c, d)
}

View File

@@ -1,26 +1,25 @@
package readerioeither
import (
IOE "github.com/ibm/fp-go/ioeither"
RE "github.com/ibm/fp-go/readerioeither/generic"
G "github.com/ibm/fp-go/context/readerioeither/generic"
)
// TraverseArray transforms an array
func TraverseArray[A, B any](f func(A) ReaderIOEither[B]) func([]A) ReaderIOEither[[]B] {
return RE.TraverseArray[ReaderIOEither[B], ReaderIOEither[[]B], IOE.IOEither[error, B], IOE.IOEither[error, []B], []A](f)
return G.TraverseArray[[]A, ReaderIOEither[[]B]](f)
}
// SequenceArray converts a homogeneous sequence of either into an either of sequence
func SequenceArray[A any](ma []ReaderIOEither[A]) ReaderIOEither[[]A] {
return RE.SequenceArray[ReaderIOEither[A], ReaderIOEither[[]A]](ma)
return G.SequenceArray[[]A, []ReaderIOEither[A], ReaderIOEither[[]A]](ma)
}
// TraverseRecord transforms a record
func TraverseRecord[K comparable, A, B any](f func(A) ReaderIOEither[B]) func(map[K]A) ReaderIOEither[map[K]B] {
return RE.TraverseRecord[ReaderIOEither[B], ReaderIOEither[map[K]B], IOE.IOEither[error, B], IOE.IOEither[error, map[K]B], map[K]A](f)
return G.TraverseRecord[K, map[K]A, ReaderIOEither[map[K]B]](f)
}
// SequenceRecord converts a homogeneous sequence of either into an either of sequence
func SequenceRecord[K comparable, A any](ma map[K]ReaderIOEither[A]) ReaderIOEither[map[K]A] {
return RE.SequenceRecord[ReaderIOEither[A], ReaderIOEither[map[K]A]](ma)
return G.SequenceRecord[K, map[K]A, map[K]ReaderIOEither[A], ReaderIOEither[map[K]A]](ma)
}