1
0
mirror of https://github.com/IBM/fp-go.git synced 2025-06-23 00:27:49 +02:00
Files
fp-go/ioeither/ioeither.go
Dr. Carsten Leue 1713de0c3e fix: introduce stateless iterator
Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
2023-07-24 16:43:07 +02:00

233 lines
8.0 KiB
Go

// Copyright (c) 2023 IBM Corp.
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ioeither
import (
ET "github.com/IBM/fp-go/either"
I "github.com/IBM/fp-go/io"
G "github.com/IBM/fp-go/ioeither/generic"
O "github.com/IBM/fp-go/option"
)
// IOEither represents a synchronous computation that may fail
// refer to [https://andywhite.xyz/posts/2021-01-27-rte-foundations/#ioeitherlte-agt] for more details
type IOEither[E, A any] I.IO[ET.Either[E, A]]
func MakeIO[E, A any](f IOEither[E, A]) IOEither[E, A] {
return G.MakeIO(f)
}
func Left[A, E any](l E) IOEither[E, A] {
return G.Left[IOEither[E, A]](l)
}
func Right[E, A any](r A) IOEither[E, A] {
return G.Right[IOEither[E, A]](r)
}
func Of[E, A any](r A) IOEither[E, A] {
return G.Of[IOEither[E, A]](r)
}
func MonadOf[E, A any](r A) IOEither[E, A] {
return G.MonadOf[IOEither[E, A]](r)
}
func LeftIO[A, E any](ml I.IO[E]) IOEither[E, A] {
return G.LeftIO[IOEither[E, A]](ml)
}
func RightIO[E, A any](mr I.IO[A]) IOEither[E, A] {
return G.RightIO[IOEither[E, A]](mr)
}
func FromEither[E, A any](e ET.Either[E, A]) IOEither[E, A] {
return G.FromEither[IOEither[E, A]](e)
}
func FromOption[A, E any](onNone func() E) func(o O.Option[A]) IOEither[E, A] {
return G.FromOption[IOEither[E, A]](onNone)
}
func ChainOptionK[A, B, E any](onNone func() E) func(func(A) O.Option[B]) func(IOEither[E, A]) IOEither[E, B] {
return G.ChainOptionK[IOEither[E, A], IOEither[E, B]](onNone)
}
func MonadChainIOK[E, A, B any](ma IOEither[E, A], f func(A) I.IO[B]) IOEither[E, B] {
return G.MonadChainIOK[IOEither[E, A], IOEither[E, B]](ma, f)
}
func ChainIOK[E, A, B any](f func(A) I.IO[B]) func(IOEither[E, A]) IOEither[E, B] {
return G.ChainIOK[IOEither[E, A], IOEither[E, B]](f)
}
func FromIO[E, A any](mr I.IO[A]) IOEither[E, A] {
return G.FromIO[IOEither[E, A]](mr)
}
func MonadMap[E, A, B any](fa IOEither[E, A], f func(A) B) IOEither[E, B] {
return G.MonadMap[IOEither[E, A], IOEither[E, B]](fa, f)
}
func Map[E, A, B any](f func(A) B) func(IOEither[E, A]) IOEither[E, B] {
return G.Map[IOEither[E, A], IOEither[E, B]](f)
}
func MonadMapTo[E, A, B any](fa IOEither[E, A], b B) IOEither[E, B] {
return G.MonadMapTo[IOEither[E, A], IOEither[E, B]](fa, b)
}
func MapTo[E, A, B any](b B) func(IOEither[E, A]) IOEither[E, B] {
return G.MapTo[IOEither[E, A], IOEither[E, B]](b)
}
func MonadChain[E, A, B any](fa IOEither[E, A], f func(A) IOEither[E, B]) IOEither[E, B] {
return G.MonadChain(fa, f)
}
func Chain[E, A, B any](f func(A) IOEither[E, B]) func(IOEither[E, A]) IOEither[E, B] {
return G.Chain[IOEither[E, A]](f)
}
func MonadChainEitherK[E, A, B any](ma IOEither[E, A], f func(A) ET.Either[E, B]) IOEither[E, B] {
return G.MonadChainEitherK[IOEither[E, A], IOEither[E, B]](ma, f)
}
func ChainEitherK[E, A, B any](f func(A) ET.Either[E, B]) func(IOEither[E, A]) IOEither[E, B] {
return G.ChainEitherK[IOEither[E, A], IOEither[E, B]](f)
}
func MonadAp[B, E, A any](mab IOEither[E, func(A) B], ma IOEither[E, A]) IOEither[E, B] {
return G.MonadAp[IOEither[E, A], IOEither[E, B]](mab, ma)
}
func Ap[B, E, A any](ma IOEither[E, A]) func(IOEither[E, func(A) B]) IOEither[E, B] {
return G.Ap[IOEither[E, A], IOEither[E, B], IOEither[E, func(A) B]](ma)
}
func Flatten[E, A any](mma IOEither[E, IOEither[E, A]]) IOEither[E, A] {
return G.Flatten(mma)
}
func TryCatch[E, A any](f func() (A, error), onThrow func(error) E) IOEither[E, A] {
return G.TryCatch[IOEither[E, A]](f, onThrow)
}
func TryCatchError[A any](f func() (A, error)) IOEither[error, A] {
return G.TryCatchError[IOEither[error, A]](f)
}
func Eitherize0[A any](f func() (A, error)) func() IOEither[error, A] {
return G.Eitherize0[IOEither[error, A]](f)
}
func Eitherize1[T1, A any](f func(t1 T1) (A, error)) func(T1) IOEither[error, A] {
return G.Eitherize1[IOEither[error, A]](f)
}
func Eitherize2[T1, T2, A any](f func(t1 T1, t2 T2) (A, error)) func(T1, T2) IOEither[error, A] {
return G.Eitherize2[IOEither[error, A]](f)
}
func Eitherize3[T1, T2, T3, A any](f func(t1 T1, t2 T2, t3 T3) (A, error)) func(T1, T2, T3) IOEither[error, A] {
return G.Eitherize3[IOEither[error, A]](f)
}
func Eitherize4[T1, T2, T3, T4, A any](f func(t1 T1, t2 T2, t3 T3, t4 T4) (A, error)) func(T1, T2, T3, T4) IOEither[error, A] {
return G.Eitherize4[IOEither[error, A]](f)
}
func Memoize[E, A any](ma IOEither[E, A]) IOEither[E, A] {
return G.Memoize(ma)
}
func MonadMapLeft[E1, E2, A any](fa IOEither[E1, A], f func(E1) E2) IOEither[E2, A] {
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] {
return G.MapLeft[IOEither[E1, A], IOEither[E2, A]](f)
}
func MonadBiMap[E1, E2, A, B any](fa IOEither[E1, A], f func(E1) E2, g func(A) B) IOEither[E2, B] {
return G.MonadBiMap[IOEither[E1, A], IOEither[E2, B]](fa, f, g)
}
// BiMap maps a pair of functions over the two type arguments of the bifunctor.
func BiMap[E1, E2, A, B any](f func(E1) E2, g func(A) B) func(IOEither[E1, A]) IOEither[E2, B] {
return G.BiMap[IOEither[E1, A], IOEither[E2, B]](f, g)
}
// Fold converts an IOEither into an IO
func Fold[E, A, B any](onLeft func(E) I.IO[B], onRight func(A) I.IO[B]) func(IOEither[E, A]) I.IO[B] {
return G.Fold[IOEither[E, A]](onLeft, onRight)
}
// GetOrElse extracts the value or maps the error
func GetOrElse[E, A any](onLeft func(E) I.IO[A]) func(IOEither[E, A]) I.IO[A] {
return G.GetOrElse[IOEither[E, A]](onLeft)
}
// MonadChainTo composes to the second monad ignoring the return value of the first
func MonadChainTo[E, A, B any](fa IOEither[E, A], fb IOEither[E, B]) IOEither[E, B] {
return G.MonadChainTo(fa, fb)
}
// ChainTo composes to the second monad ignoring the return value of the first
func ChainTo[E, A, B any](fb IOEither[E, B]) func(IOEither[E, A]) IOEither[E, B] {
return G.ChainTo[IOEither[E, A]](fb)
}
// MonadChainFirst runs the monad returned by the function but returns the result of the original monad
func MonadChainFirst[E, A, B any](ma IOEither[E, A], f func(A) IOEither[E, B]) IOEither[E, A] {
return G.MonadChainFirst(ma, f)
}
// ChainFirst runs the monad returned by the function but returns the result of the original monad
func ChainFirst[E, A, B any](f func(A) IOEither[E, B]) func(IOEither[E, A]) IOEither[E, A] {
return G.ChainFirst[IOEither[E, A]](f)
}
// MonadChainFirstIOK runs the monad returned by the function but returns the result of the original monad
func MonadChainFirstIOK[E, A, B any](ma IOEither[E, A], f func(A) I.IO[B]) IOEither[E, A] {
return G.MonadChainFirstIOK(ma, f)
}
// ChainFirsIOKt runs the monad returned by the function but returns the result of the original monad
func ChainFirstIOK[E, A, B any](f func(A) I.IO[B]) func(IOEither[E, A]) IOEither[E, A] {
return G.ChainFirstIOK[IOEither[E, A]](f)
}
// WithResource constructs a function that creates a resource, then operates on it and then releases the resource
func WithResource[E, R, A, ANY any](onCreate IOEither[E, R], onRelease func(R) IOEither[E, ANY]) func(func(R) IOEither[E, A]) IOEither[E, A] {
return G.WithResource[IOEither[E, A]](onCreate, onRelease)
}
// Swap changes the order of type parameters
func Swap[E, A any](val IOEither[E, A]) IOEither[A, E] {
return G.Swap[IOEither[E, A], IOEither[A, E]](val)
}
// FromImpure converts a side effect without a return value into a side effect that returns any
func FromImpure[E any](f func()) IOEither[E, any] {
return G.FromImpure[IOEither[E, any]](f)
}
// Defer creates an IO by creating a brand new IO via a generator function, each time
func Defer[E, A any](gen func() IOEither[E, A]) IOEither[E, A] {
return G.Defer[IOEither[E, A]](gen)
}