mirror of
https://github.com/IBM/fp-go.git
synced 2025-08-10 22:31:32 +02:00
fix: add support for context sensitive readers
Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
This commit is contained in:
15
context/readereither/array.go
Normal file
15
context/readereither/array.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package readereither
|
||||
|
||||
import (
|
||||
RE "github.com/ibm/fp-go/readereither/generic"
|
||||
)
|
||||
|
||||
// TraverseArray transforms an array
|
||||
func TraverseArray[A, B any](f func(A) ReaderEither[B]) func([]A) ReaderEither[[]B] {
|
||||
return RE.TraverseArray[ReaderEither[B], ReaderEither[[]B], []A](f)
|
||||
}
|
||||
|
||||
// SequenceArray converts a homogeneous sequence of either into an either of sequence
|
||||
func SequenceArray[A any](ma []ReaderEither[A]) ReaderEither[[]A] {
|
||||
return RE.SequenceArray[ReaderEither[A], ReaderEither[[]A]](ma)
|
||||
}
|
17
context/readereither/cancel.go
Normal file
17
context/readereither/cancel.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package readereither
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
E "github.com/ibm/fp-go/either"
|
||||
)
|
||||
|
||||
// withContext wraps an existing ReaderEither and performs a context check for cancellation before deletating
|
||||
func WithContext[A any](ma ReaderEither[A]) ReaderEither[A] {
|
||||
return func(ctx context.Context) E.Either[error, A] {
|
||||
if err := context.Cause(ctx); err != nil {
|
||||
return E.Left[A](err)
|
||||
}
|
||||
return ma(ctx)
|
||||
}
|
||||
}
|
38
context/readereither/curry.go
Normal file
38
context/readereither/curry.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package readereither
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
RE "github.com/ibm/fp-go/readereither/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 Curry0[A any](f func(context.Context) (A, error)) ReaderEither[A] {
|
||||
return RE.Curry0[ReaderEither[A]](f)
|
||||
}
|
||||
|
||||
func Curry1[T1, A any](f func(context.Context, T1) (A, error)) func(T1) ReaderEither[A] {
|
||||
return RE.Curry1[ReaderEither[A]](f)
|
||||
}
|
||||
|
||||
func Curry2[T1, T2, A any](f func(context.Context, T1, T2) (A, error)) func(T1) func(T2) ReaderEither[A] {
|
||||
return RE.Curry2[ReaderEither[A]](f)
|
||||
}
|
||||
|
||||
func Curry3[T1, T2, T3, A any](f func(context.Context, T1, T2, T3) (A, error)) func(T1) func(T2) func(T3) ReaderEither[A] {
|
||||
return RE.Curry3[ReaderEither[A]](f)
|
||||
}
|
||||
|
||||
func Uncurry1[T1, A any](f func(T1) ReaderEither[A]) func(context.Context, T1) (A, error) {
|
||||
return RE.Uncurry1(f)
|
||||
}
|
||||
|
||||
func Uncurry2[T1, T2, A any](f func(T1) func(T2) ReaderEither[A]) func(context.Context, T1, T2) (A, error) {
|
||||
return RE.Uncurry2(f)
|
||||
}
|
||||
|
||||
func Uncurry3[T1, T2, T3, A any](f func(T1) func(T2) func(T3) ReaderEither[A]) func(context.Context, T1, T2, T3) (A, error) {
|
||||
return RE.Uncurry3(f)
|
||||
}
|
26
context/readereither/exec/exec.go
Normal file
26
context/readereither/exec/exec.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package exec
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
RE "github.com/ibm/fp-go/context/readereither"
|
||||
E "github.com/ibm/fp-go/either"
|
||||
"github.com/ibm/fp-go/exec"
|
||||
F "github.com/ibm/fp-go/function"
|
||||
GE "github.com/ibm/fp-go/internal/exec"
|
||||
)
|
||||
|
||||
var (
|
||||
// Command executes a command
|
||||
// use this version if the command does not produce any side effect, i.e. if the output is uniquely determined by by the input
|
||||
// typically you'd rather use the ReaderIOEither version of the command
|
||||
Command = F.Curry3(command)
|
||||
)
|
||||
|
||||
func command(name string, args []string, in []byte) RE.ReaderEither[exec.CommandOutput] {
|
||||
return func(ctx context.Context) E.Either[error, exec.CommandOutput] {
|
||||
return E.TryCatchError(func() (exec.CommandOutput, error) {
|
||||
return GE.Exec(ctx, name, args, in)
|
||||
})
|
||||
}
|
||||
}
|
26
context/readereither/from.go
Normal file
26
context/readereither/from.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package readereither
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
RE "github.com/ibm/fp-go/readereither/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) (A, error)) ReaderEither[A] {
|
||||
return RE.From0[ReaderEither[A]](f)
|
||||
}
|
||||
|
||||
func From1[T1, A any](f func(context.Context, T1) (A, error)) func(T1) ReaderEither[A] {
|
||||
return RE.From1[ReaderEither[A]](f)
|
||||
}
|
||||
|
||||
func From2[T1, T2, A any](f func(context.Context, T1, T2) (A, error)) func(T1, T2) ReaderEither[A] {
|
||||
return RE.From2[ReaderEither[A]](f)
|
||||
}
|
||||
|
||||
func From3[T1, T2, T3, A any](f func(context.Context, T1, T2, T3) (A, error)) func(T1, T2, T3) ReaderEither[A] {
|
||||
return RE.From3[ReaderEither[A]](f)
|
||||
}
|
106
context/readereither/reader.go
Normal file
106
context/readereither/reader.go
Normal file
@@ -0,0 +1,106 @@
|
||||
package readereither
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
R "github.com/ibm/fp-go/context/reader"
|
||||
ET "github.com/ibm/fp-go/either"
|
||||
O "github.com/ibm/fp-go/option"
|
||||
RE "github.com/ibm/fp-go/readereither/generic"
|
||||
)
|
||||
|
||||
func MakeReaderEither[A any](f func(context.Context) ET.Either[error, A]) ReaderEither[A] {
|
||||
return RE.MakeReaderEither[ReaderEither[A]](f)
|
||||
}
|
||||
|
||||
func FromEither[A any](e ET.Either[error, A]) ReaderEither[A] {
|
||||
return RE.FromEither[ReaderEither[A]](e)
|
||||
}
|
||||
|
||||
func RightReader[A any](r R.Reader[A]) ReaderEither[A] {
|
||||
return RE.RightReader[R.Reader[A], ReaderEither[A]](r)
|
||||
}
|
||||
|
||||
func LeftReader[A any](l R.Reader[error]) ReaderEither[A] {
|
||||
return RE.LeftReader[R.Reader[error], ReaderEither[A]](l)
|
||||
}
|
||||
|
||||
func Left[A any](l error) ReaderEither[A] {
|
||||
return RE.Left[ReaderEither[A]](l)
|
||||
}
|
||||
|
||||
func Right[A any](r A) ReaderEither[A] {
|
||||
return RE.Right[ReaderEither[A]](r)
|
||||
}
|
||||
|
||||
func FromReader[A any](r R.Reader[A]) ReaderEither[A] {
|
||||
return RE.FromReader[R.Reader[A], ReaderEither[A]](r)
|
||||
}
|
||||
|
||||
func MonadMap[A, B any](fa ReaderEither[A], f func(A) B) ReaderEither[B] {
|
||||
return RE.MonadMap[ReaderEither[A], ReaderEither[B]](fa, f)
|
||||
}
|
||||
|
||||
func Map[A, B any](f func(A) B) func(ReaderEither[A]) ReaderEither[B] {
|
||||
return RE.Map[ReaderEither[A], ReaderEither[B]](f)
|
||||
}
|
||||
|
||||
func MonadChain[A, B any](ma ReaderEither[A], f func(A) ReaderEither[B]) ReaderEither[B] {
|
||||
return RE.MonadChain(ma, f)
|
||||
}
|
||||
|
||||
func Chain[A, B any](f func(A) ReaderEither[B]) func(ReaderEither[A]) ReaderEither[B] {
|
||||
return RE.Chain[ReaderEither[A]](f)
|
||||
}
|
||||
|
||||
func Of[A any](a A) ReaderEither[A] {
|
||||
return RE.Of[ReaderEither[A]](a)
|
||||
}
|
||||
|
||||
func MonadAp[A, B any](fab ReaderEither[func(A) B], fa ReaderEither[A]) ReaderEither[B] {
|
||||
return RE.MonadAp[ReaderEither[A], ReaderEither[B]](fab, fa)
|
||||
}
|
||||
|
||||
func Ap[A, B any](fa ReaderEither[A]) func(ReaderEither[func(A) B]) ReaderEither[B] {
|
||||
return RE.Ap[ReaderEither[A], ReaderEither[B], ReaderEither[func(A) B]](fa)
|
||||
}
|
||||
|
||||
func FromPredicate[A any](pred func(A) bool, onFalse func(A) error) func(A) ReaderEither[A] {
|
||||
return RE.FromPredicate[ReaderEither[A]](pred, onFalse)
|
||||
}
|
||||
|
||||
func Fold[A, B any](onLeft func(error) R.Reader[B], onRight func(A) R.Reader[B]) func(ReaderEither[A]) R.Reader[B] {
|
||||
return RE.Fold[ReaderEither[A]](onLeft, onRight)
|
||||
}
|
||||
|
||||
func GetOrElse[A any](onLeft func(error) R.Reader[A]) func(ReaderEither[A]) R.Reader[A] {
|
||||
return RE.GetOrElse[ReaderEither[A]](onLeft)
|
||||
}
|
||||
|
||||
func OrElse[A any](onLeft func(error) ReaderEither[A]) func(ReaderEither[A]) ReaderEither[A] {
|
||||
return RE.OrElse[ReaderEither[A]](onLeft)
|
||||
}
|
||||
|
||||
func OrLeft[A any](onLeft func(error) R.Reader[error]) func(ReaderEither[A]) ReaderEither[A] {
|
||||
return RE.OrLeft[ReaderEither[A], ReaderEither[A]](onLeft)
|
||||
}
|
||||
|
||||
func Ask() ReaderEither[context.Context] {
|
||||
return RE.Ask[ReaderEither[context.Context]]()
|
||||
}
|
||||
|
||||
func Asks[A any](r R.Reader[A]) ReaderEither[A] {
|
||||
return RE.Asks[R.Reader[A], ReaderEither[A]](r)
|
||||
}
|
||||
|
||||
func MonadChainEitherK[A, B any](ma ReaderEither[A], f func(A) ET.Either[error, B]) ReaderEither[B] {
|
||||
return RE.MonadChainEitherK[ReaderEither[A], ReaderEither[B]](ma, f)
|
||||
}
|
||||
|
||||
func ChainEitherK[A, B any](f func(A) ET.Either[error, B]) func(ma ReaderEither[A]) ReaderEither[B] {
|
||||
return RE.ChainEitherK[ReaderEither[A], ReaderEither[B]](f)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
42
context/readereither/sequence.go
Normal file
42
context/readereither/sequence.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package readereither
|
||||
|
||||
import (
|
||||
RE "github.com/ibm/fp-go/readereither/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 ReaderEither[A]) ReaderEither[T.Tuple1[A]] {
|
||||
return RE.SequenceT1[
|
||||
ReaderEither[A],
|
||||
ReaderEither[T.Tuple1[A]],
|
||||
](a)
|
||||
}
|
||||
|
||||
func SequenceT2[A, B any](a ReaderEither[A], b ReaderEither[B]) ReaderEither[T.Tuple2[A, B]] {
|
||||
return RE.SequenceT2[
|
||||
ReaderEither[A],
|
||||
ReaderEither[B],
|
||||
ReaderEither[T.Tuple2[A, B]],
|
||||
](a, b)
|
||||
}
|
||||
|
||||
func SequenceT3[A, B, C any](a ReaderEither[A], b ReaderEither[B], c ReaderEither[C]) ReaderEither[T.Tuple3[A, B, C]] {
|
||||
return RE.SequenceT3[
|
||||
ReaderEither[A],
|
||||
ReaderEither[B],
|
||||
ReaderEither[C],
|
||||
ReaderEither[T.Tuple3[A, B, C]],
|
||||
](a, b, c)
|
||||
}
|
||||
|
||||
func SequenceT4[A, B, C, D any](a ReaderEither[A], b ReaderEither[B], c ReaderEither[C], d ReaderEither[D]) ReaderEither[T.Tuple4[A, B, C, D]] {
|
||||
return RE.SequenceT4[
|
||||
ReaderEither[A],
|
||||
ReaderEither[B],
|
||||
ReaderEither[C],
|
||||
ReaderEither[D],
|
||||
ReaderEither[T.Tuple4[A, B, C, D]],
|
||||
](a, b, c, d)
|
||||
}
|
11
context/readereither/type.go
Normal file
11
context/readereither/type.go
Normal file
@@ -0,0 +1,11 @@
|
||||
// Package readereither implements a specialization of the Reader monad assuming a golang context as the context of the monad and a standard golang error
|
||||
package readereither
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
RE "github.com/ibm/fp-go/readereither"
|
||||
)
|
||||
|
||||
// ReaderEither is a specialization of the Reader monad for the typical golang scenario
|
||||
type ReaderEither[A any] RE.ReaderEither[context.Context, error, A]
|
Reference in New Issue
Block a user