Documentation
¶
Index ¶
- func Fold[T, U, R any](f Foldable[T, U], successFunc func(U) R, failureFunc func(T) R) R
- type Either
- func (e Either[L, R]) ForEach(leftCb func(L), rightCb func(R))
- func (e Either[L, R]) IsLeft() bool
- func (e Either[L, R]) IsRight() bool
- func (e Either[L, R]) Left() (L, bool)
- func (e Either[L, R]) LeftOrElse(fallback L) L
- func (e Either[L, R]) LeftOrEmpty() L
- func (e Either[L, R]) MapLeft(mapper func(L) Either[L, R]) Either[L, R]
- func (e Either[L, R]) MapRight(mapper func(R) Either[L, R]) Either[L, R]
- func (e Either[L, R]) Match(onLeft func(L) Either[L, R], onRight func(R) Either[L, R]) Either[L, R]
- func (e Either[L, R]) MustLeft() L
- func (e Either[L, R]) MustRight() R
- func (e Either[L, R]) Right() (R, bool)
- func (e Either[L, R]) RightOrElse(fallback R) R
- func (e Either[L, R]) RightOrEmpty() R
- func (e Either[L, R]) Swap() Either[R, L]
- func (e Either[L, R]) Unpack() (L, R)
- type Either3
- func (e Either3[T1, T2, T3]) Arg1() (T1, bool)
- func (e Either3[T1, T2, T3]) Arg1OrElse(fallback T1) T1
- func (e Either3[T1, T2, T3]) Arg1OrEmpty() T1
- func (e Either3[T1, T2, T3]) Arg2() (T2, bool)
- func (e Either3[T1, T2, T3]) Arg2OrElse(fallback T2) T2
- func (e Either3[T1, T2, T3]) Arg2OrEmpty() T2
- func (e Either3[T1, T2, T3]) Arg3() (T3, bool)
- func (e Either3[T1, T2, T3]) Arg3OrElse(fallback T3) T3
- func (e Either3[T1, T2, T3]) Arg3OrEmpty() T3
- func (e Either3[T1, T2, T3]) ForEach(arg1Cb func(T1), arg2Cb func(T2), arg3Cb func(T3))
- func (e Either3[T1, T2, T3]) IsArg1() bool
- func (e Either3[T1, T2, T3]) IsArg2() bool
- func (e Either3[T1, T2, T3]) IsArg3() bool
- func (e Either3[T1, T2, T3]) MapArg1(mapper func(T1) Either3[T1, T2, T3]) Either3[T1, T2, T3]
- func (e Either3[T1, T2, T3]) MapArg2(mapper func(T2) Either3[T1, T2, T3]) Either3[T1, T2, T3]
- func (e Either3[T1, T2, T3]) MapArg3(mapper func(T3) Either3[T1, T2, T3]) Either3[T1, T2, T3]
- func (e Either3[T1, T2, T3]) Match(onArg1 func(T1) Either3[T1, T2, T3], onArg2 func(T2) Either3[T1, T2, T3], ...) Either3[T1, T2, T3]
- func (e Either3[T1, T2, T3]) MustArg1() T1
- func (e Either3[T1, T2, T3]) MustArg2() T2
- func (e Either3[T1, T2, T3]) MustArg3() T3
- func (e Either3[T1, T2, T3]) Unpack() (T1, T2, T3)
- type Either4
- func NewEither4Arg1[T1 any, T2 any, T3 any, T4 any](value T1) Either4[T1, T2, T3, T4]
- func NewEither4Arg2[T1 any, T2 any, T3 any, T4 any](value T2) Either4[T1, T2, T3, T4]
- func NewEither4Arg3[T1 any, T2 any, T3 any, T4 any](value T3) Either4[T1, T2, T3, T4]
- func NewEither4Arg4[T1 any, T2 any, T3 any, T4 any](value T4) Either4[T1, T2, T3, T4]
- func (e Either4[T1, T2, T3, T4]) Arg1() (T1, bool)
- func (e Either4[T1, T2, T3, T4]) Arg1OrElse(fallback T1) T1
- func (e Either4[T1, T2, T3, T4]) Arg1OrEmpty() T1
- func (e Either4[T1, T2, T3, T4]) Arg2() (T2, bool)
- func (e Either4[T1, T2, T3, T4]) Arg2OrElse(fallback T2) T2
- func (e Either4[T1, T2, T3, T4]) Arg2OrEmpty() T2
- func (e Either4[T1, T2, T3, T4]) Arg3() (T3, bool)
- func (e Either4[T1, T2, T3, T4]) Arg3OrElse(fallback T3) T3
- func (e Either4[T1, T2, T3, T4]) Arg3OrEmpty() T3
- func (e Either4[T1, T2, T3, T4]) Arg4() (T4, bool)
- func (e Either4[T1, T2, T3, T4]) Arg4OrElse(fallback T4) T4
- func (e Either4[T1, T2, T3, T4]) Arg4OrEmpty() T4
- func (e Either4[T1, T2, T3, T4]) ForEach(arg1Cb func(T1), arg2Cb func(T2), arg3Cb func(T3), arg4Cb func(T4))
- func (e Either4[T1, T2, T3, T4]) IsArg1() bool
- func (e Either4[T1, T2, T3, T4]) IsArg2() bool
- func (e Either4[T1, T2, T3, T4]) IsArg3() bool
- func (e Either4[T1, T2, T3, T4]) IsArg4() bool
- func (e Either4[T1, T2, T3, T4]) MapArg1(mapper func(T1) Either4[T1, T2, T3, T4]) Either4[T1, T2, T3, T4]
- func (e Either4[T1, T2, T3, T4]) MapArg2(mapper func(T2) Either4[T1, T2, T3, T4]) Either4[T1, T2, T3, T4]
- func (e Either4[T1, T2, T3, T4]) MapArg3(mapper func(T3) Either4[T1, T2, T3, T4]) Either4[T1, T2, T3, T4]
- func (e Either4[T1, T2, T3, T4]) MapArg4(mapper func(T4) Either4[T1, T2, T3, T4]) Either4[T1, T2, T3, T4]
- func (e Either4[T1, T2, T3, T4]) Match(onArg1 func(T1) Either4[T1, T2, T3, T4], ...) Either4[T1, T2, T3, T4]
- func (e Either4[T1, T2, T3, T4]) MustArg1() T1
- func (e Either4[T1, T2, T3, T4]) MustArg2() T2
- func (e Either4[T1, T2, T3, T4]) MustArg3() T3
- func (e Either4[T1, T2, T3, T4]) MustArg4() T4
- func (e Either4[T1, T2, T3, T4]) Unpack() (T1, T2, T3, T4)
- type Either5
- func NewEither5Arg1[T1 any, T2 any, T3 any, T4 any, T5 any](value T1) Either5[T1, T2, T3, T4, T5]
- func NewEither5Arg2[T1 any, T2 any, T3 any, T4 any, T5 any](value T2) Either5[T1, T2, T3, T4, T5]
- func NewEither5Arg3[T1 any, T2 any, T3 any, T4 any, T5 any](value T3) Either5[T1, T2, T3, T4, T5]
- func NewEither5Arg4[T1 any, T2 any, T3 any, T4 any, T5 any](value T4) Either5[T1, T2, T3, T4, T5]
- func NewEither5Arg5[T1 any, T2 any, T3 any, T4 any, T5 any](value T5) Either5[T1, T2, T3, T4, T5]
- func (e Either5[T1, T2, T3, T4, T5]) Arg1() (T1, bool)
- func (e Either5[T1, T2, T3, T4, T5]) Arg1OrElse(fallback T1) T1
- func (e Either5[T1, T2, T3, T4, T5]) Arg1OrEmpty() T1
- func (e Either5[T1, T2, T3, T4, T5]) Arg2() (T2, bool)
- func (e Either5[T1, T2, T3, T4, T5]) Arg2OrElse(fallback T2) T2
- func (e Either5[T1, T2, T3, T4, T5]) Arg2OrEmpty() T2
- func (e Either5[T1, T2, T3, T4, T5]) Arg3() (T3, bool)
- func (e Either5[T1, T2, T3, T4, T5]) Arg3OrElse(fallback T3) T3
- func (e Either5[T1, T2, T3, T4, T5]) Arg3OrEmpty() T3
- func (e Either5[T1, T2, T3, T4, T5]) Arg4() (T4, bool)
- func (e Either5[T1, T2, T3, T4, T5]) Arg4OrElse(fallback T4) T4
- func (e Either5[T1, T2, T3, T4, T5]) Arg4OrEmpty() T4
- func (e Either5[T1, T2, T3, T4, T5]) Arg5() (T5, bool)
- func (e Either5[T1, T2, T3, T4, T5]) Arg5OrElse(fallback T5) T5
- func (e Either5[T1, T2, T3, T4, T5]) Arg5OrEmpty() T5
- func (e Either5[T1, T2, T3, T4, T5]) ForEach(arg1Cb func(T1), arg2Cb func(T2), arg3Cb func(T3), arg4Cb func(T4), ...)
- func (e Either5[T1, T2, T3, T4, T5]) IsArg1() bool
- func (e Either5[T1, T2, T3, T4, T5]) IsArg2() bool
- func (e Either5[T1, T2, T3, T4, T5]) IsArg3() bool
- func (e Either5[T1, T2, T3, T4, T5]) IsArg4() bool
- func (e Either5[T1, T2, T3, T4, T5]) IsArg5() bool
- func (e Either5[T1, T2, T3, T4, T5]) MapArg1(mapper func(T1) Either5[T1, T2, T3, T4, T5]) Either5[T1, T2, T3, T4, T5]
- func (e Either5[T1, T2, T3, T4, T5]) MapArg2(mapper func(T2) Either5[T1, T2, T3, T4, T5]) Either5[T1, T2, T3, T4, T5]
- func (e Either5[T1, T2, T3, T4, T5]) MapArg3(mapper func(T3) Either5[T1, T2, T3, T4, T5]) Either5[T1, T2, T3, T4, T5]
- func (e Either5[T1, T2, T3, T4, T5]) MapArg4(mapper func(T4) Either5[T1, T2, T3, T4, T5]) Either5[T1, T2, T3, T4, T5]
- func (e Either5[T1, T2, T3, T4, T5]) MapArg5(mapper func(T5) Either5[T1, T2, T3, T4, T5]) Either5[T1, T2, T3, T4, T5]
- func (e Either5[T1, T2, T3, T4, T5]) Match(onArg1 func(T1) Either5[T1, T2, T3, T4, T5], ...) Either5[T1, T2, T3, T4, T5]
- func (e Either5[T1, T2, T3, T4, T5]) MustArg1() T1
- func (e Either5[T1, T2, T3, T4, T5]) MustArg2() T2
- func (e Either5[T1, T2, T3, T4, T5]) MustArg3() T3
- func (e Either5[T1, T2, T3, T4, T5]) MustArg4() T4
- func (e Either5[T1, T2, T3, T4, T5]) MustArg5() T5
- func (e Either5[T1, T2, T3, T4, T5]) Unpack() (T1, T2, T3, T4, T5)
- type Foldable
- type Future
- func (f *Future[T]) Cancel()
- func (f *Future[T]) Catch(cb func(error) (T, error)) *Future[T]
- func (f *Future[T]) Collect() (T, error)
- func (f *Future[T]) Either() Either[error, T]
- func (f *Future[T]) Finally(cb func(T, error) (T, error)) *Future[T]
- func (f *Future[T]) Result() Result[T]
- func (f *Future[T]) Then(cb func(T) (T, error)) *Future[T]
- type IO
- type IO1
- type IO2
- type IO3
- type IO4
- type IO5
- type IOEither
- type IOEither1
- type IOEither2
- type IOEither3
- type IOEither4
- type IOEither5
- type Option
- func (o Option[T]) Equal(other Option[T]) bool
- func (o Option[T]) FlatMap(mapper func(value T) Option[T]) Option[T]
- func (o Option[T]) ForEach(onValue func(value T))
- func (o Option[T]) Get() (T, bool)
- func (o *Option[T]) GobDecode(data []byte) error
- func (o Option[T]) GobEncode() ([]byte, error)
- func (o Option[T]) IsAbsent() bool
- func (o Option[T]) IsNone() bool
- func (o Option[T]) IsPresent() bool
- func (o Option[T]) IsSome() bool
- func (o Option[T]) IsZero() bool
- func (o Option[T]) Map(mapper func(value T) (T, bool)) Option[T]
- func (o Option[T]) MapNone(mapper func() (T, bool)) Option[T]
- func (o Option[T]) MapValue(mapper func(value T) T) Option[T]
- func (o Option[T]) MarshalBinary() ([]byte, error)
- func (o Option[T]) MarshalJSON() ([]byte, error)
- func (o Option[T]) MarshalText() ([]byte, error)
- func (o Option[T]) Match(onValue func(value T) (T, bool), onNone func() (T, bool)) Option[T]
- func (o Option[T]) MustGet() T
- func (o Option[T]) OrElse(fallback T) T
- func (o Option[T]) OrEmpty() T
- func (o *Option[T]) Scan(src any) error
- func (o Option[T]) Size() int
- func (o Option[T]) ToPointer() *T
- func (o *Option[T]) UnmarshalBinary(data []byte) error
- func (o *Option[T]) UnmarshalJSON(b []byte) error
- func (o *Option[T]) UnmarshalText(data []byte) error
- func (o Option[T]) Value() (driver.Value, error)
- type Result
- func (r Result[T]) Error() error
- func (r Result[T]) FlatMap(mapper func(value T) Result[T]) Result[T]
- func (r Result[T]) ForEach(mapper func(value T))
- func (r Result[T]) Get() (T, error)
- func (r Result[T]) IsError() bool
- func (r Result[T]) IsOk() bool
- func (r Result[T]) Map(mapper func(value T) (T, error)) Result[T]
- func (r Result[T]) MapErr(mapper func(error) (T, error)) Result[T]
- func (r Result[T]) MapValue(mapper func(value T) T) Result[T]
- func (o Result[T]) MarshalJSON() ([]byte, error)
- func (r Result[T]) Match(onSuccess func(value T) (T, error), onError func(err error) (T, error)) Result[T]
- func (r Result[T]) MustGet() T
- func (r Result[T]) OrElse(fallback T) T
- func (r Result[T]) OrEmpty() T
- func (r Result[T]) ToEither() Either[error, T]
- func (o *Result[T]) UnmarshalJSON(data []byte) error
- type State
- type Task
- type Task1
- type Task2
- type Task3
- type Task4
- type Task5
- type TaskEither
- func (t TaskEither[R]) Match(onLeft func(error) Either[error, R], onRight func(R) Either[error, R]) Either[error, R]
- func (t TaskEither[R]) OrElse(fallback R) R
- func (t TaskEither[R]) ToEither() Either[error, R]
- func (t TaskEither[R]) ToTask(fallback R) Task[R]
- func (t TaskEither[R]) TryCatch(onLeft func(error) Either[error, R], onRight func(R) Either[error, R]) Either[error, R]
Examples ¶
- Do
- Do (Panic)
- Either.IsLeft (Left)
- Either.IsLeft (Right)
- Either.IsRight (Left)
- Either.IsRight (Right)
- Either.Left (Left)
- Either.Left (Right)
- Either.LeftOrElse (Left)
- Either.LeftOrElse (Right)
- Either.LeftOrEmpty (Left)
- Either.LeftOrEmpty (Right)
- Either.MapLeft (Left)
- Either.MapLeft (Right)
- Either.MapRight (Left)
- Either.MapRight (Right)
- Either.Match (Left)
- Either.Match (Right)
- Either.MustLeft (Left)
- Either.MustRight (Right)
- Either.Right (Left)
- Either.Right (Right)
- Either.RightOrElse (Left)
- Either.RightOrElse (Right)
- Either.RightOrEmpty (Left)
- Either.RightOrEmpty (Right)
- Either.Swap (Left)
- Either.Swap (Right)
- Either.Unpack (Left)
- Either.Unpack (Right)
- Either5.Arg1
- Either5.Arg1OrElse
- Either5.Arg1OrEmpty
- Either5.IsArg1
- Either5.MapArg1
- Either5.Match
- Either5.MustArg1
- Either5.Unpack
- EmptyableToOption
- Err
- Errf
- Fold (Either)
- Fold (Option)
- Fold (Result)
- Future.Cancel (Reject)
- Future.Cancel (Resolve)
- Future.Catch (Reject)
- Future.Catch (Resolve)
- Future.Collect (Reject)
- Future.Collect (Resolve)
- Future.Finally (Reject)
- Future.Finally (Resolve)
- Future.Result (Reject)
- Future.Result (Resolve)
- Future.Then (Reject)
- Future.Then (Resolve)
- IO
- IOEither1
- Left
- NewEither5Arg1
- NewFuture (Reject)
- NewFuture (Resolve)
- None
- Ok
- Option (None)
- Option (Some)
- Option.Equal
- Option.FlatMap (None)
- Option.FlatMap (Some)
- Option.Get (None)
- Option.Get (Some)
- Option.IsAbsent (None)
- Option.IsAbsent (Some)
- Option.IsPresent (None)
- Option.IsPresent (Some)
- Option.Map (None)
- Option.Map (Some)
- Option.MapNone (None)
- Option.MapNone (Some)
- Option.MarshalJSON (None)
- Option.MarshalJSON (Some)
- Option.Match (None)
- Option.Match (Some)
- Option.MustGet (Some)
- Option.OrElse (None)
- Option.OrElse (Some)
- Option.OrEmpty (None)
- Option.OrEmpty (Some)
- Option.Size (None)
- Option.Size (Some)
- Option.ToPointer (None)
- Option.ToPointer (Some)
- Option.UnmarshalJSON (None)
- Option.UnmarshalJSON (Some)
- Result (Err)
- Result (Ok)
- Result.Error (Err)
- Result.Error (Ok)
- Result.FlatMap (Err)
- Result.FlatMap (Ok)
- Result.Get (Err)
- Result.Get (Ok)
- Result.IsError (Err)
- Result.IsError (Ok)
- Result.IsOk (Err)
- Result.IsOk (Ok)
- Result.Map (Err)
- Result.Map (Ok)
- Result.MapErr (Err)
- Result.MapErr (Ok)
- Result.MapValue (Err)
- Result.MapValue (Ok)
- Result.Match (Err)
- Result.Match (Ok)
- Result.MustGet (Ok)
- Result.OrElse (Err)
- Result.OrElse (Ok)
- Result.OrEmpty (Err)
- Result.OrEmpty (Ok)
- Result.ToEither (Err)
- Result.ToEither (Ok)
- Right
- Some
- Task
- Try (Err)
- Try (Ok)
- TupleToOption
- TupleToResult
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Fold ¶ added in v1.12.0
Fold applies one of the two functions based on the state of the Foldable type, and it returns the result of applying either successFunc or failureFunc.
- T: the type of the failure value (e.g., an error type) - U: the type of the success value - R: the type of the return value from the folding functions
successFunc is applied when the Foldable is in the success state (i.e., isLeft() is false). failureFunc is applied when the Foldable is in the failure state (i.e., isLeft() is true).
Example (Either) ¶
either1 := Either[error, int]{isLeft: false, right: 42}
either2 := Either[error, int]{isLeft: true, left: errors.New("either error")}
successFunc := func(val int) string {
return fmt.Sprintf("Success with value %d", val)
}
failureFunc := func(err error) string {
return fmt.Sprintf("Failure with error %s", err)
}
fmt.Println(Fold[error, int, string](either1, successFunc, failureFunc))
fmt.Println(Fold[error, int, string](either2, successFunc, failureFunc))
Output: Success with value 42 Failure with error either error
Example (Option) ¶
option1 := Option[int]{isPresent: true, value: 42}
option2 := Option[int]{isPresent: false}
successFunc := func(val int) string {
return fmt.Sprintf("Success with value %d", val)
}
failureFunc := func(err error) string {
return fmt.Sprintf("Failure with error %s", err)
}
fmt.Println(Fold[error, int, string](option1, successFunc, failureFunc))
fmt.Println(Fold[error, int, string](option2, successFunc, failureFunc))
Output: Success with value 42 Failure with error no such element
Example (Result) ¶
res1 := Result[int]{value: 42, isErr: false, err: nil}
res2 := Result[int]{value: 0, isErr: true, err: errors.New("error")}
successFunc := func(val int) string {
return fmt.Sprintf("Success with value %d", val)
}
failureFunc := func(err error) string {
return fmt.Sprintf("Failure with error %s", err)
}
fmt.Println(Fold[error, int, string](res1, successFunc, failureFunc))
fmt.Println(Fold[error, int, string](res2, successFunc, failureFunc))
Output: Success with value 42 Failure with error error
Types ¶
type Either ¶
Either respresents a value of 2 possible types. An instance of Either is an instance of either A or B.
func Left ¶
Left builds the left side of the Either struct, as opposed to the Right side.
Example ¶
left := Left[string, int]("hello")
result1 := left.LeftOrElse("world")
result2 := left.RightOrElse(1234)
fmt.Println(result1, result2)
Output: hello 1234
func Right ¶
Right builds the right side of the Either struct, as opposed to the Left side.
Example ¶
right := Right[string, int](42)
result1 := right.LeftOrElse("world")
result2 := right.RightOrElse(1234)
fmt.Println(result1, result2)
Output: world 42
func (Either[L, R]) ForEach ¶
func (e Either[L, R]) ForEach(leftCb func(L), rightCb func(R))
ForEach executes the given side-effecting function, depending of value is Left or Right.
func (Either[L, R]) IsLeft ¶
IsLeft returns true if Either is an instance of Left.
Example (Left) ¶
left := Left[string, int]("hello")
result := left.IsLeft()
fmt.Println(result)
Output: true
Example (Right) ¶
right := Right[string, int](42) result := right.IsLeft() fmt.Println(result)
Output: false
func (Either[L, R]) IsRight ¶
IsRight returns true if Either is an instance of Right.
Example (Left) ¶
left := Left[string, int]("hello")
result := left.IsRight()
fmt.Println(result)
Output: false
Example (Right) ¶
right := Right[string, int](42) result := right.IsRight() fmt.Println(result)
Output: true
func (Either[L, R]) Left ¶
Left returns left value of a Either struct.
Example (Left) ¶
left := Left[string, int]("hello")
result, ok := left.Left()
fmt.Println(result)
fmt.Println(ok)
Output: hello true
Example (Right) ¶
right := Right[string, int](42) result, ok := right.Left() fmt.Println(result) fmt.Println(ok)
Output: false
func (Either[L, R]) LeftOrElse ¶
func (e Either[L, R]) LeftOrElse(fallback L) L
LeftOrElse returns left value of a Either struct or fallback.
Example (Left) ¶
left := Left[string, int]("hello")
result := left.LeftOrElse("world")
fmt.Println(result)
Output: hello
Example (Right) ¶
right := Right[string, int](42)
result := right.LeftOrElse("world")
fmt.Println(result)
Output: world
func (Either[L, R]) LeftOrEmpty ¶
func (e Either[L, R]) LeftOrEmpty() L
LeftOrEmpty returns left value of a Either struct or empty value.
Example (Left) ¶
left := Left[string, int]("hello")
result := left.LeftOrEmpty()
fmt.Println(result)
Output: hello
Example (Right) ¶
right := Right[string, int](42) result := right.LeftOrEmpty() fmt.Println(result)
func (Either[L, R]) MapLeft ¶
MapLeft executes the given function, if Either is of type Left, and returns result.
Example (Left) ¶
left := Left[string, int]("hello")
result := left.MapLeft(
func(s string) Either[string, int] {
return Right[string, int](1234)
},
)
result1, ok1 := result.Left()
result2, ok2 := result.Right()
fmt.Println(result1)
fmt.Println(ok1)
fmt.Println(result2)
fmt.Println(ok2)
Output: false 1234 true
Example (Right) ¶
right := Right[string, int](42)
result := right.MapLeft(
func(s string) Either[string, int] {
return Left[string, int]("world")
},
)
result1, ok1 := result.Left()
result2, ok2 := result.Right()
fmt.Println(result1)
fmt.Println(ok1)
fmt.Println(result2)
fmt.Println(ok2)
Output: false 42 true
func (Either[L, R]) MapRight ¶
MapRight executes the given function, if Either is of type Right, and returns result.
Example (Left) ¶
left := Left[string, int]("hello")
result := left.MapRight(
func(i int) Either[string, int] {
return Right[string, int](1234)
},
)
result1, ok1 := result.Left()
result2, ok2 := result.Right()
fmt.Println(result1)
fmt.Println(ok1)
fmt.Println(result2)
fmt.Println(ok2)
Output: hello true 0 false
Example (Right) ¶
right := Right[string, int](42)
result := right.MapRight(
func(i int) Either[string, int] {
return Right[string, int](1234)
},
)
result1, ok1 := result.Left()
result2, ok2 := result.Right()
fmt.Println(result1)
fmt.Println(ok1)
fmt.Println(result2)
fmt.Println(ok2)
Output: false 1234 true
func (Either[L, R]) Match ¶
Match executes the given function, depending of value is Left or Right, and returns result.
Example (Left) ¶
left := Left[string, int]("hello")
result := left.Match(
func(s string) Either[string, int] {
return Right[string, int](1234)
},
func(i int) Either[string, int] {
return Right[string, int](i * 42)
},
)
result1, ok1 := result.Left()
result2, ok2 := result.Right()
fmt.Println(result1)
fmt.Println(ok1)
fmt.Println(result2)
fmt.Println(ok2)
Output: false 1234 true
Example (Right) ¶
right := Right[string, int](42)
result := right.Match(
func(s string) Either[string, int] {
return Left[string, int]("world")
},
func(i int) Either[string, int] {
return Left[string, int]("foobar")
},
)
result1, ok1 := result.Left()
result2, ok2 := result.Right()
fmt.Println(result1)
fmt.Println(ok1)
fmt.Println(result2)
fmt.Println(ok2)
Output: foobar true 0 false
func (Either[L, R]) MustLeft ¶
func (e Either[L, R]) MustLeft() L
MustLeft returns left value of a Either struct or panics.
Example (Left) ¶
left := Left[string, int]("hello")
result := left.MustLeft()
fmt.Println(result)
Output: hello
func (Either[L, R]) MustRight ¶
func (e Either[L, R]) MustRight() R
MustRight returns right value of a Either struct or panics.
Example (Right) ¶
right := Right[string, int](42) result := right.MustRight() fmt.Println(result)
Output: 42
func (Either[L, R]) Right ¶
Right returns right value of a Either struct.
Example (Left) ¶
left := Left[string, int]("hello")
result, ok := left.Right()
fmt.Println(result)
fmt.Println(ok)
Output: 0 false
Example (Right) ¶
right := Right[string, int](42) result, ok := right.Right() fmt.Println(result) fmt.Println(ok)
Output: 42 true
func (Either[L, R]) RightOrElse ¶
func (e Either[L, R]) RightOrElse(fallback R) R
RightOrElse returns right value of a Either struct or fallback.
Example (Left) ¶
left := Left[string, int]("hello")
result := left.RightOrElse(1234)
fmt.Println(result)
Output: 1234
Example (Right) ¶
right := Right[string, int](42) result := right.RightOrElse(1234) fmt.Println(result)
Output: 42
func (Either[L, R]) RightOrEmpty ¶
func (e Either[L, R]) RightOrEmpty() R
RightOrEmpty returns right value of a Either struct or empty value.
Example (Left) ¶
left := Left[string, int]("hello")
result := left.RightOrEmpty()
fmt.Println(result)
Output: 0
Example (Right) ¶
right := Right[string, int](42) result := right.RightOrEmpty() fmt.Println(result)
Output: 42
func (Either[L, R]) Swap ¶
Swap returns the left value in Right and vice versa.
Example (Left) ¶
left := Left[string, int]("hello")
right := left.Swap()
result1, ok1 := right.Left()
result2, ok2 := right.Right()
fmt.Println(result1)
fmt.Println(ok1)
fmt.Println(result2)
fmt.Println(ok2)
Output: 0 false hello true
Example (Right) ¶
right := Right[string, int](42) left := right.Swap() result1, ok1 := left.Left() result2, ok2 := left.Right() fmt.Println(result1) fmt.Println(ok1) fmt.Println(result2) fmt.Println(ok2) // 42 // true // // false
func (Either[L, R]) Unpack ¶ added in v1.6.0
func (e Either[L, R]) Unpack() (L, R)
Unpack returns all values
Example (Left) ¶
either := Left[string, int]("42")
left, right := either.Unpack()
fmt.Println(left, right)
Output: 42 0
Example (Right) ¶
either := Right[string, int](42) left, right := either.Unpack() fmt.Println(left, right)
Output: 42
type Either3 ¶ added in v1.2.0
Either3 represents a value of 3 possible types. An instance of Either3 is an instance of either T1, T2 or T3.
func NewEither3Arg1 ¶ added in v1.2.0
NewEither3Arg1 builds the first argument of the Either3 struct.
func NewEither3Arg2 ¶ added in v1.2.0
NewEither3Arg2 builds the second argument of the Either3 struct.
func NewEither3Arg3 ¶ added in v1.2.0
NewEither3Arg3 builds the third argument of the Either3 struct.
func (Either3[T1, T2, T3]) Arg1 ¶ added in v1.2.0
Arg1 returns the first argument of a Either3 struct.
func (Either3[T1, T2, T3]) Arg1OrElse ¶ added in v1.2.0
func (e Either3[T1, T2, T3]) Arg1OrElse(fallback T1) T1
Arg1OrElse returns the first argument of a Either3 struct or fallback.
func (Either3[T1, T2, T3]) Arg1OrEmpty ¶ added in v1.2.0
func (e Either3[T1, T2, T3]) Arg1OrEmpty() T1
Arg1OrEmpty returns the first argument of a Either3 struct or empty value.
func (Either3[T1, T2, T3]) Arg2 ¶ added in v1.2.0
Arg2 returns the second argument of a Either3 struct.
func (Either3[T1, T2, T3]) Arg2OrElse ¶ added in v1.2.0
func (e Either3[T1, T2, T3]) Arg2OrElse(fallback T2) T2
Arg2OrElse returns the second argument of a Either3 struct or fallback.
func (Either3[T1, T2, T3]) Arg2OrEmpty ¶ added in v1.2.0
func (e Either3[T1, T2, T3]) Arg2OrEmpty() T2
Arg2OrEmpty returns the second argument of a Either3 struct or empty value.
func (Either3[T1, T2, T3]) Arg3 ¶ added in v1.2.0
Arg3 returns the third argument of a Either3 struct.
func (Either3[T1, T2, T3]) Arg3OrElse ¶ added in v1.2.0
func (e Either3[T1, T2, T3]) Arg3OrElse(fallback T3) T3
Arg3OrElse returns the third argument of a Either3 struct or fallback.
func (Either3[T1, T2, T3]) Arg3OrEmpty ¶ added in v1.2.0
func (e Either3[T1, T2, T3]) Arg3OrEmpty() T3
Arg3OrEmpty returns the third argument of a Either3 struct or empty value.
func (Either3[T1, T2, T3]) ForEach ¶ added in v1.2.0
func (e Either3[T1, T2, T3]) ForEach(arg1Cb func(T1), arg2Cb func(T2), arg3Cb func(T3))
ForEach executes the given side-effecting function, depending of the argument set.
func (Either3[T1, T2, T3]) IsArg1 ¶ added in v1.2.0
IsArg1 returns true if Either3 uses the first argument.
func (Either3[T1, T2, T3]) IsArg2 ¶ added in v1.2.0
IsArg2 returns true if Either3 uses the second argument.
func (Either3[T1, T2, T3]) IsArg3 ¶ added in v1.2.0
IsArg3 returns true if Either3 uses the third argument.
func (Either3[T1, T2, T3]) MapArg1 ¶ added in v1.2.0
MapArg1 executes the given function, if Either3 use the first argument, and returns result.
func (Either3[T1, T2, T3]) MapArg2 ¶ added in v1.2.0
MapArg2 executes the given function, if Either3 use the second argument, and returns result.
func (Either3[T1, T2, T3]) MapArg3 ¶ added in v1.2.0
MapArg3 executes the given function, if Either3 use the third argument, and returns result.
func (Either3[T1, T2, T3]) Match ¶ added in v1.2.0
func (e Either3[T1, T2, T3]) Match( onArg1 func(T1) Either3[T1, T2, T3], onArg2 func(T2) Either3[T1, T2, T3], onArg3 func(T3) Either3[T1, T2, T3]) Either3[T1, T2, T3]
Match executes the given function, depending of the argument set, and returns result.
func (Either3[T1, T2, T3]) MustArg1 ¶ added in v1.2.0
func (e Either3[T1, T2, T3]) MustArg1() T1
MustArg1 returns the first argument of a Either3 struct or panics.
func (Either3[T1, T2, T3]) MustArg2 ¶ added in v1.2.0
func (e Either3[T1, T2, T3]) MustArg2() T2
MustArg2 returns the second argument of a Either3 struct or panics.
type Either4 ¶ added in v1.2.0
Either4 respresents a value of 4 possible types. An instance of Either4 is an instance of either T1, T2, T3 or T4.
func NewEither4Arg1 ¶ added in v1.2.0
NewEither4Arg1 builds the first argument of the Either4 struct.
func NewEither4Arg2 ¶ added in v1.2.0
NewEither4Arg2 builds the second argument of the Either4 struct.
func NewEither4Arg3 ¶ added in v1.2.0
NewEither4Arg3 builds the third argument of the Either4 struct.
func NewEither4Arg4 ¶ added in v1.2.0
NewEither4Arg4 builds the fourth argument of the Either4 struct.
func (Either4[T1, T2, T3, T4]) Arg1 ¶ added in v1.2.0
Arg1 returns the first argument of a Either4 struct.
func (Either4[T1, T2, T3, T4]) Arg1OrElse ¶ added in v1.2.0
func (e Either4[T1, T2, T3, T4]) Arg1OrElse(fallback T1) T1
Arg1OrElse returns the first argument of a Either4 struct or fallback.
func (Either4[T1, T2, T3, T4]) Arg1OrEmpty ¶ added in v1.2.0
func (e Either4[T1, T2, T3, T4]) Arg1OrEmpty() T1
Arg1OrEmpty returns the first argument of a Either4 struct or empty value.
func (Either4[T1, T2, T3, T4]) Arg2 ¶ added in v1.2.0
Arg2 returns the second argument of a Either4 struct.
func (Either4[T1, T2, T3, T4]) Arg2OrElse ¶ added in v1.2.0
func (e Either4[T1, T2, T3, T4]) Arg2OrElse(fallback T2) T2
Arg2OrElse returns the second argument of a Either4 struct or fallback.
func (Either4[T1, T2, T3, T4]) Arg2OrEmpty ¶ added in v1.2.0
func (e Either4[T1, T2, T3, T4]) Arg2OrEmpty() T2
Arg2OrEmpty returns the second argument of a Either4 struct or empty value.
func (Either4[T1, T2, T3, T4]) Arg3 ¶ added in v1.2.0
Arg3 returns the third argument of a Either4 struct.
func (Either4[T1, T2, T3, T4]) Arg3OrElse ¶ added in v1.2.0
func (e Either4[T1, T2, T3, T4]) Arg3OrElse(fallback T3) T3
Arg3OrElse returns the third argument of a Either4 struct or fallback.
func (Either4[T1, T2, T3, T4]) Arg3OrEmpty ¶ added in v1.2.0
func (e Either4[T1, T2, T3, T4]) Arg3OrEmpty() T3
Arg3OrEmpty returns the third argument of a Either4 struct or empty value.
func (Either4[T1, T2, T3, T4]) Arg4 ¶ added in v1.2.0
Arg4 returns the fourth argument of a Either4 struct.
func (Either4[T1, T2, T3, T4]) Arg4OrElse ¶ added in v1.2.0
func (e Either4[T1, T2, T3, T4]) Arg4OrElse(fallback T4) T4
Arg4OrElse returns the fourth argument of a Either4 struct or fallback.
func (Either4[T1, T2, T3, T4]) Arg4OrEmpty ¶ added in v1.2.0
func (e Either4[T1, T2, T3, T4]) Arg4OrEmpty() T4
Arg4OrEmpty returns the fourth argument of a Either4 struct or empty value.
func (Either4[T1, T2, T3, T4]) ForEach ¶ added in v1.2.0
func (e Either4[T1, T2, T3, T4]) ForEach(arg1Cb func(T1), arg2Cb func(T2), arg3Cb func(T3), arg4Cb func(T4))
ForEach executes the given side-effecting function, depending of the argument set.
func (Either4[T1, T2, T3, T4]) IsArg1 ¶ added in v1.2.0
IsArg1 returns true if Either4 uses the first argument.
func (Either4[T1, T2, T3, T4]) IsArg2 ¶ added in v1.2.0
IsArg2 returns true if Either4 uses the second argument.
func (Either4[T1, T2, T3, T4]) IsArg3 ¶ added in v1.2.0
IsArg3 returns true if Either4 uses the third argument.
func (Either4[T1, T2, T3, T4]) IsArg4 ¶ added in v1.2.0
IsArg4 returns true if Either4 uses the fourth argument.
func (Either4[T1, T2, T3, T4]) MapArg1 ¶ added in v1.2.0
func (e Either4[T1, T2, T3, T4]) MapArg1(mapper func(T1) Either4[T1, T2, T3, T4]) Either4[T1, T2, T3, T4]
MapArg1 executes the given function, if Either4 use the first argument, and returns result.
func (Either4[T1, T2, T3, T4]) MapArg2 ¶ added in v1.2.0
func (e Either4[T1, T2, T3, T4]) MapArg2(mapper func(T2) Either4[T1, T2, T3, T4]) Either4[T1, T2, T3, T4]
MapArg2 executes the given function, if Either4 use the second argument, and returns result.
func (Either4[T1, T2, T3, T4]) MapArg3 ¶ added in v1.2.0
func (e Either4[T1, T2, T3, T4]) MapArg3(mapper func(T3) Either4[T1, T2, T3, T4]) Either4[T1, T2, T3, T4]
MapArg3 executes the given function, if Either4 use the third argument, and returns result.
func (Either4[T1, T2, T3, T4]) MapArg4 ¶ added in v1.2.0
func (e Either4[T1, T2, T3, T4]) MapArg4(mapper func(T4) Either4[T1, T2, T3, T4]) Either4[T1, T2, T3, T4]
MapArg4 executes the given function, if Either4 use the fourth argument, and returns result.
func (Either4[T1, T2, T3, T4]) Match ¶ added in v1.2.0
func (e Either4[T1, T2, T3, T4]) Match( onArg1 func(T1) Either4[T1, T2, T3, T4], onArg2 func(T2) Either4[T1, T2, T3, T4], onArg3 func(T3) Either4[T1, T2, T3, T4], onArg4 func(T4) Either4[T1, T2, T3, T4]) Either4[T1, T2, T3, T4]
Match executes the given function, depending of the argument set, and returns result.
func (Either4[T1, T2, T3, T4]) MustArg1 ¶ added in v1.2.0
func (e Either4[T1, T2, T3, T4]) MustArg1() T1
MustArg1 returns the first argument of a Either4 struct or panics.
func (Either4[T1, T2, T3, T4]) MustArg2 ¶ added in v1.2.0
func (e Either4[T1, T2, T3, T4]) MustArg2() T2
MustArg2 returns the second argument of a Either4 struct or panics.
func (Either4[T1, T2, T3, T4]) MustArg3 ¶ added in v1.2.0
func (e Either4[T1, T2, T3, T4]) MustArg3() T3
MustArg3 returns the third argument of a Either4 struct or panics.
type Either5 ¶ added in v1.2.0
type Either5[T1 any, T2 any, T3 any, T4 any, T5 any] struct { // contains filtered or unexported fields }
Either5 respresents a value of 5 possible types. An instance of Either5 is an instance of either T1, T2, T3, T4, or T5.
func NewEither5Arg1 ¶ added in v1.2.0
NewEither5Arg1 builds the first argument of the Either5 struct.
Example ¶
either5Arg1 := NewEither5Arg1[int, bool, float64, string, byte](42)
result1 := either5Arg1.Arg1OrElse(21)
result2 := either5Arg1.Arg4OrElse("Bye")
fmt.Println(result1, result2)
Output: 42 Bye
func NewEither5Arg2 ¶ added in v1.2.0
NewEither5Arg2 builds the second argument of the Either5 struct.
func NewEither5Arg3 ¶ added in v1.2.0
NewEither5Arg3 builds the third argument of the Either5 struct.
func NewEither5Arg4 ¶ added in v1.2.0
NewEither5Arg4 builds the fourth argument of the Either5 struct.
func NewEither5Arg5 ¶ added in v1.2.0
NewEither5Arg5 builds the fith argument of the Either5 struct.
func (Either5[T1, T2, T3, T4, T5]) Arg1 ¶ added in v1.2.0
Arg1 returns the first argument of a Either5 struct.
Example ¶
either5Arg1 := NewEither5Arg1[int, bool, float64, string, byte](42) result1, ok1 := either5Arg1.Arg1() result2, ok2 := either5Arg1.Arg3() fmt.Println(result1, ok1) fmt.Println(result2, ok2)
Output: 42 true 0 false
func (Either5[T1, T2, T3, T4, T5]) Arg1OrElse ¶ added in v1.2.0
func (e Either5[T1, T2, T3, T4, T5]) Arg1OrElse(fallback T1) T1
Arg1OrElse returns the first argument of a Either5 struct or fallback.
Example ¶
either5Arg1 := NewEither5Arg1[int, bool, float64, string, byte](42)
result1 := either5Arg1.Arg1OrElse(21)
result2 := either5Arg1.Arg4OrElse("Bye")
fmt.Println(result1, result2)
Output: 42 Bye
func (Either5[T1, T2, T3, T4, T5]) Arg1OrEmpty ¶ added in v1.2.0
func (e Either5[T1, T2, T3, T4, T5]) Arg1OrEmpty() T1
Arg1OrEmpty returns the first argument of a Either5 struct or empty value.
Example ¶
either5Arg1 := NewEither5Arg1[int, bool, float64, string, byte](42) result1 := either5Arg1.Arg1OrEmpty() result2 := either5Arg1.Arg2OrEmpty() fmt.Println(result1, result2)
Output: 42 false
func (Either5[T1, T2, T3, T4, T5]) Arg2 ¶ added in v1.2.0
Arg2 returns the second argument of a Either5 struct.
func (Either5[T1, T2, T3, T4, T5]) Arg2OrElse ¶ added in v1.2.0
func (e Either5[T1, T2, T3, T4, T5]) Arg2OrElse(fallback T2) T2
Arg2OrElse returns the second argument of a Either5 struct or fallback.
func (Either5[T1, T2, T3, T4, T5]) Arg2OrEmpty ¶ added in v1.2.0
func (e Either5[T1, T2, T3, T4, T5]) Arg2OrEmpty() T2
Arg2OrEmpty returns the second argument of a Either5 struct or empty value.
func (Either5[T1, T2, T3, T4, T5]) Arg3 ¶ added in v1.2.0
Arg3 returns the third argument of a Either5 struct.
func (Either5[T1, T2, T3, T4, T5]) Arg3OrElse ¶ added in v1.2.0
func (e Either5[T1, T2, T3, T4, T5]) Arg3OrElse(fallback T3) T3
Arg3OrElse returns the third argument of a Either5 struct or fallback.
func (Either5[T1, T2, T3, T4, T5]) Arg3OrEmpty ¶ added in v1.2.0
func (e Either5[T1, T2, T3, T4, T5]) Arg3OrEmpty() T3
Arg3OrEmpty returns the third argument of a Either5 struct or empty value.
func (Either5[T1, T2, T3, T4, T5]) Arg4 ¶ added in v1.2.0
Arg4 returns the fourth argument of a Either5 struct.
func (Either5[T1, T2, T3, T4, T5]) Arg4OrElse ¶ added in v1.2.0
func (e Either5[T1, T2, T3, T4, T5]) Arg4OrElse(fallback T4) T4
Arg4OrElse returns the fourth argument of a Either5 struct or fallback.
func (Either5[T1, T2, T3, T4, T5]) Arg4OrEmpty ¶ added in v1.2.0
func (e Either5[T1, T2, T3, T4, T5]) Arg4OrEmpty() T4
Arg4OrEmpty returns the fourth argument of a Either5 struct or empty value.
func (Either5[T1, T2, T3, T4, T5]) Arg5 ¶ added in v1.2.0
Arg5 returns the fith argument of a Either5 struct.
func (Either5[T1, T2, T3, T4, T5]) Arg5OrElse ¶ added in v1.2.0
func (e Either5[T1, T2, T3, T4, T5]) Arg5OrElse(fallback T5) T5
Arg5OrElse returns the fith argument of a Either5 struct or fallback.
func (Either5[T1, T2, T3, T4, T5]) Arg5OrEmpty ¶ added in v1.2.0
func (e Either5[T1, T2, T3, T4, T5]) Arg5OrEmpty() T5
Arg5OrEmpty returns the fifth argument of a Either5 struct or empty value.
func (Either5[T1, T2, T3, T4, T5]) ForEach ¶ added in v1.2.0
func (e Either5[T1, T2, T3, T4, T5]) ForEach(arg1Cb func(T1), arg2Cb func(T2), arg3Cb func(T3), arg4Cb func(T4), arg5Cb func(T5))
ForEach executes the given side-effecting function, depending of the argument set.
func (Either5[T1, T2, T3, T4, T5]) IsArg1 ¶ added in v1.2.0
IsArg1 returns true if Either5 uses the first argument.
Example ¶
either5Arg1 := NewEither5Arg1[int, bool, float64, string, byte](42) result1 := either5Arg1.IsArg1() result2 := either5Arg1.IsArg4() fmt.Println(result1, result2)
Output: true false
func (Either5[T1, T2, T3, T4, T5]) IsArg2 ¶ added in v1.2.0
IsArg2 returns true if Either5 uses the second argument.
func (Either5[T1, T2, T3, T4, T5]) IsArg3 ¶ added in v1.2.0
IsArg3 returns true if Either5 uses the third argument.
func (Either5[T1, T2, T3, T4, T5]) IsArg4 ¶ added in v1.2.0
IsArg4 returns true if Either5 uses the fourth argument.
func (Either5[T1, T2, T3, T4, T5]) IsArg5 ¶ added in v1.2.0
IsArg5 returns true if Either5 uses the fith argument.
func (Either5[T1, T2, T3, T4, T5]) MapArg1 ¶ added in v1.2.0
func (e Either5[T1, T2, T3, T4, T5]) MapArg1(mapper func(T1) Either5[T1, T2, T3, T4, T5]) Either5[T1, T2, T3, T4, T5]
MapArg1 executes the given function, if Either5 use the first argument, and returns result.
Example ¶
either5Arg1 := NewEither5Arg1[int, bool, float64, string, byte](42)
result1 := either5Arg1.MapArg1(
func(v int) Either5[int, bool, float64, string, byte] {
return NewEither5Arg1[int, bool, float64, string, byte](21)
},
)
result2 := either5Arg1.MapArg4(
func(v string) Either5[int, bool, float64, string, byte] {
return NewEither5Arg4[int, bool, float64, string, byte]("Bye")
},
)
fmt.Println(result1.MustArg1(), result2.MustArg1())
Output: 21 42
func (Either5[T1, T2, T3, T4, T5]) MapArg2 ¶ added in v1.2.0
func (e Either5[T1, T2, T3, T4, T5]) MapArg2(mapper func(T2) Either5[T1, T2, T3, T4, T5]) Either5[T1, T2, T3, T4, T5]
MapArg2 executes the given function, if Either5 use the second argument, and returns result.
func (Either5[T1, T2, T3, T4, T5]) MapArg3 ¶ added in v1.2.0
func (e Either5[T1, T2, T3, T4, T5]) MapArg3(mapper func(T3) Either5[T1, T2, T3, T4, T5]) Either5[T1, T2, T3, T4, T5]
MapArg3 executes the given function, if Either5 use the third argument, and returns result.
func (Either5[T1, T2, T3, T4, T5]) MapArg4 ¶ added in v1.2.0
func (e Either5[T1, T2, T3, T4, T5]) MapArg4(mapper func(T4) Either5[T1, T2, T3, T4, T5]) Either5[T1, T2, T3, T4, T5]
MapArg4 executes the given function, if Either5 use the fourth argument, and returns result.
func (Either5[T1, T2, T3, T4, T5]) MapArg5 ¶ added in v1.2.0
func (e Either5[T1, T2, T3, T4, T5]) MapArg5(mapper func(T5) Either5[T1, T2, T3, T4, T5]) Either5[T1, T2, T3, T4, T5]
MapArg5 executes the given function, if Either5 use the fith argument, and returns result.
func (Either5[T1, T2, T3, T4, T5]) Match ¶ added in v1.2.0
func (e Either5[T1, T2, T3, T4, T5]) Match( onArg1 func(T1) Either5[T1, T2, T3, T4, T5], onArg2 func(T2) Either5[T1, T2, T3, T4, T5], onArg3 func(T3) Either5[T1, T2, T3, T4, T5], onArg4 func(T4) Either5[T1, T2, T3, T4, T5], onArg5 func(T5) Either5[T1, T2, T3, T4, T5]) Either5[T1, T2, T3, T4, T5]
Match executes the given function, depending of the argument set, and returns result.
Example ¶
either5Arg1 := NewEither5Arg1[int, bool, float64, string, byte](42)
result1 := either5Arg1.Match(func(v int) Either5[int, bool, float64, string, byte] {
return NewEither5Arg1[int, bool, float64, string, byte](21)
}, func(v bool) Either5[int, bool, float64, string, byte] {
return NewEither5Arg2[int, bool, float64, string, byte](false)
}, func(v float64) Either5[int, bool, float64, string, byte] {
return NewEither5Arg3[int, bool, float64, string, byte](2.1)
}, func(v string) Either5[int, bool, float64, string, byte] {
return NewEither5Arg4[int, bool, float64, string, byte]("Bye")
}, func(v byte) Either5[int, bool, float64, string, byte] {
return NewEither5Arg5[int, bool, float64, string, byte](10)
})
fmt.Println(result1.MustArg1())
Output: 21
func (Either5[T1, T2, T3, T4, T5]) MustArg1 ¶ added in v1.2.0
func (e Either5[T1, T2, T3, T4, T5]) MustArg1() T1
MustArg1 returns the first argument of a Either5 struct or panics.
Example ¶
either5Arg1 := NewEither5Arg1[int, bool, float64, string, byte](42) // result = either5Arg1.MustArg4() // Panics result := either5Arg1.MustArg1() fmt.Println(result)
Output: 42
func (Either5[T1, T2, T3, T4, T5]) MustArg2 ¶ added in v1.2.0
func (e Either5[T1, T2, T3, T4, T5]) MustArg2() T2
MustArg2 returns the second argument of a Either5 struct or panics.
func (Either5[T1, T2, T3, T4, T5]) MustArg3 ¶ added in v1.2.0
func (e Either5[T1, T2, T3, T4, T5]) MustArg3() T3
MustArg3 returns the third argument of a Either5 struct or panics.
func (Either5[T1, T2, T3, T4, T5]) MustArg4 ¶ added in v1.2.0
func (e Either5[T1, T2, T3, T4, T5]) MustArg4() T4
MustArg4 returns the fourth argument of a Either5 struct or panics.
func (Either5[T1, T2, T3, T4, T5]) MustArg5 ¶ added in v1.2.0
func (e Either5[T1, T2, T3, T4, T5]) MustArg5() T5
MustArg5 returns the fith argument of a Either5 struct or panics.
func (Either5[T1, T2, T3, T4, T5]) Unpack ¶ added in v1.6.0
func (e Either5[T1, T2, T3, T4, T5]) Unpack() (T1, T2, T3, T4, T5)
Unpack returns all values
Example ¶
either5 := NewEither5Arg1[int, bool, float64, string, byte](42) a, b, c, d, e := either5.Unpack() fmt.Println(a, b, c, d, e)
Output: 42 false 0 0
type Foldable ¶ added in v1.12.0
Foldable represents a type that can be folded into a single value based on its state.
- T: the type of the value in the failure state (e.g., an error type). - U: the type of the value in the success state.
type Future ¶
type Future[T any] struct { // contains filtered or unexported fields }
Future represents a value which may or may not currently be available, but will be available at some point, or an exception if that value could not be made available.
func NewFuture ¶
NewFuture instanciate a new future.
Example (Reject) ¶
value, err := NewFuture(func(resolve func(string), reject func(error)) {
reject(fmt.Errorf("failure"))
}).Collect()
fmt.Println(value)
fmt.Println(err)
Output: failure
Example (Resolve) ¶
value, err := NewFuture(func(resolve func(string), reject func(error)) {
resolve("foobar")
}).Collect()
fmt.Println(value)
fmt.Println(err)
Output: foobar <nil>
func (*Future[T]) Cancel ¶
func (f *Future[T]) Cancel()
Cancel cancels the Future chain.
Example (Reject) ¶
NewFuture(func(resolve func(string), reject func(error)) {
reject(fmt.Errorf("failure"))
}).Cancel()
Example (Resolve) ¶
NewFuture(func(resolve func(string), reject func(error)) {
resolve("foobar")
}).Cancel()
func (*Future[T]) Catch ¶
Catch is called when Future is rejected. It returns a new Future.
Example (Reject) ¶
result := NewFuture(func(resolve func(string), reject func(error)) {
reject(fmt.Errorf("failure"))
}).Catch(func(err error) (string, error) {
return "foobar", nil
}).Result()
fmt.Println(result.OrEmpty())
fmt.Println(result.Error())
Output: foobar <nil>
Example (Resolve) ¶
result := NewFuture(func(resolve func(string), reject func(error)) {
resolve("foobar")
}).Catch(func(err error) (string, error) {
return "baz", nil
}).Result()
fmt.Println(result.OrEmpty())
fmt.Println(result.Error())
Output: foobar <nil>
func (*Future[T]) Collect ¶
Collect awaits and return result of the Future.
Example (Reject) ¶
value, err := NewFuture(func(resolve func(string), reject func(error)) {
reject(fmt.Errorf("failure"))
}).Collect()
fmt.Println(value)
fmt.Println(err)
Output: failure
Example (Resolve) ¶
value, err := NewFuture(func(resolve func(string), reject func(error)) {
resolve("foobar")
}).Collect()
fmt.Println(value)
fmt.Println(err)
Output: foobar <nil>
func (*Future[T]) Finally ¶
Finally is called when Future is processed either resolved or rejected. It returns a new Future.
Example (Reject) ¶
result := NewFuture(func(resolve func(string), reject func(error)) {
reject(fmt.Errorf("failure"))
}).Finally(func(value string, err error) (string, error) {
return "foobar", nil
}).Result()
fmt.Println(result.OrEmpty())
fmt.Println(result.Error())
Output: foobar <nil>
Example (Resolve) ¶
result := NewFuture(func(resolve func(string), reject func(error)) {
resolve("foobar")
}).Finally(func(value string, err error) (string, error) {
return "baz", nil
}).Result()
fmt.Println(result.OrEmpty())
fmt.Println(result.Error())
Output: baz <nil>
func (*Future[T]) Result ¶
Result wraps Collect and returns a Result.
Example (Reject) ¶
result := NewFuture(func(resolve func(string), reject func(error)) {
reject(fmt.Errorf("failure"))
}).Result()
fmt.Println(result.OrEmpty())
fmt.Println(result.Error())
Output: failure
Example (Resolve) ¶
result := NewFuture(func(resolve func(string), reject func(error)) {
resolve("foobar")
}).Result()
fmt.Println(result.OrEmpty())
fmt.Println(result.Error())
Output: foobar <nil>
func (*Future[T]) Then ¶
Then is called when Future is resolved. It returns a new Future.
Example (Reject) ¶
result := NewFuture(func(resolve func(string), reject func(error)) {
reject(fmt.Errorf("failure"))
}).Then(func(s string) (string, error) {
return "foobar", nil
}).Result()
fmt.Println(result.OrEmpty())
fmt.Println(result.Error())
Output: failure
Example (Resolve) ¶
result := NewFuture(func(resolve func(string), reject func(error)) {
resolve("foobar")
}).Then(func(s string) (string, error) {
return "baz", nil
}).Result()
fmt.Println(result.OrEmpty())
fmt.Println(result.Error())
Output: baz <nil>
type IO ¶
type IO[R any] struct { // contains filtered or unexported fields }
IO represents a non-deterministic synchronous computation that can cause side effects, yields a value of type `R` and never fails.
Example ¶
t := time.Date(2024, 6, 22, 0, 0, 0, 0, time.Local)
io := NewIO(func() int {
return t.Year()
})
result1 := io.Run()
result2 := io.Run()
result3 := io.Run()
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
Output: 2024 2024 2024
type IO1 ¶
IO1 represents a non-deterministic synchronous computation that can cause side effects, yields a value of type `R` and never fails.
type IO2 ¶
IO2 represents a non-deterministic synchronous computation that can cause side effects, yields a value of type `R` and never fails.
type IO3 ¶
IO3 represents a non-deterministic synchronous computation that can cause side effects, yields a value of type `R` and never fails.
type IO4 ¶
IO4 represents a non-deterministic synchronous computation that can cause side effects, yields a value of type `R` and never fails.
type IO5 ¶
type IO5[R any, A any, B any, C any, D any, E any] struct { // contains filtered or unexported fields }
IO5 represents a non-deterministic synchronous computation that can cause side effects, yields a value of type `R` and never fails.
type IOEither ¶
type IOEither[R any] struct { // contains filtered or unexported fields }
IOEither represents a non-deterministic synchronous computation that can cause side effects, yields a value of type `R` and can fail.
type IOEither1 ¶
IOEither1 represents a non-deterministic synchronous computation that can cause side effects, yields a value of type `R` and can fail.
Example ¶
io := NewIOEither1(func(path string) (bool, error) {
_, err := os.Stat(path)
if errors.Is(err, os.ErrNotExist) {
return false, nil
} else if err != nil {
// other errors
return false, err
}
return true, nil
})
either1 := io.Run("./io_either.go")
either2 := io.Run("./foo_bar.go")
exist1, _ := either1.Right()
exist2, _ := either2.Right()
fmt.Println(exist1)
fmt.Println(exist2)
Output: true false
func NewIOEither1 ¶
NewIOEither1 instanciates a new IO1.
type IOEither2 ¶
IOEither2 represents a non-deterministic synchronous computation that can cause side effects, yields a value of type `R` and can fail.
func NewIOEither2 ¶
NewIOEither2 instanciates a new IO2.
type IOEither3 ¶
IOEither3 represents a non-deterministic synchronous computation that can cause side effects, yields a value of type `R` and can fail.
func NewIOEither3 ¶
NewIOEither3 instanciates a new IO3.
type IOEither4 ¶
type IOEither4[R any, A any, B any, C any, D any] struct { // contains filtered or unexported fields }
IOEither4 represents a non-deterministic synchronous computation that can cause side effects, yields a value of type `R` and can fail.
func NewIOEither4 ¶
NewIOEither4 instanciates a new IO4.
type IOEither5 ¶
type IOEither5[R any, A any, B any, C any, D any, E any] struct { // contains filtered or unexported fields }
IOEither5 represents a non-deterministic synchronous computation that can cause side effects, yields a value of type `R` and can fail.
type Option ¶
type Option[T any] struct { // contains filtered or unexported fields }
Option is a container for an optional value of type T. If value exists, Option is of type Some. If the value is absent, Option is of type None.
Example (None) ¶
none := None[int]() result := none.OrElse(1234) fmt.Println(result)
Output: 1234
Example (Some) ¶
some := Some(42) result := some.OrElse(1234) fmt.Println(result)
Output: 42
func EmptyableToOption ¶ added in v1.5.0
EmptyableToOption builds a Some Option when value is not empty, or None. Play: https://go.dev/play/p/GSpQQ-q-UES
Example ¶
cb := func(ok bool) error {
if ok {
return nil
}
return fmt.Errorf("an error")
}
err := cb(false)
none := EmptyableToOption(err)
result, ok := none.Get()
fmt.Println(result)
fmt.Println(ok)
Output: an error true
func None ¶
None builds an Option when value is absent. Play: https://go.dev/play/p/yYQPsYCSYlD
Example ¶
none := None[int]() result := none.OrElse(1234) fmt.Println(result)
Output: 1234
func PointerToOption ¶ added in v1.8.0
PointerToOption builds a Some Option when value is not nil, or None. Play: https://go.dev/play/p/yPVMj4DUb-I
func Some ¶
Some builds an Option when value is present. Play: https://go.dev/play/p/iqz2n9n0tDM
Example ¶
some := Some(42) result := some.OrElse(1234) fmt.Println(result)
Output: 42
func TupleToOption ¶
TupleToOption builds a Some Option when second argument is true, or None. Play: https://go.dev/play/p/gkrg2pZwOty
Example ¶
m := map[string]int{
"foo": 21,
"bar": 42,
"baz": 84,
}
value, ok := m["hello world"]
none := TupleToOption(value, ok)
result := none.OrElse(1234)
fmt.Println(result)
Output: 1234
func (Option[T]) Equal ¶ added in v1.16.0
Equal compares two Option[T] instances for equality
Example ¶
none1 := None[int]()
none2 := None[int]()
some42a := Some(42)
some42b := Some(42)
some24 := Some(24)
fmt.Println(none1.Equal(none2)) // None == None
fmt.Println(some42a.Equal(some42b)) // Some(42) == Some(42)
fmt.Println(some42a.Equal(some24)) // Some(42) == Some(24)
fmt.Println(none1.Equal(some42a)) // None == Some(42)
// String comparison
hello1 := Some("hello")
hello2 := Some("hello")
world := Some("world")
fmt.Println(hello1.Equal(hello2)) // Some("hello") == Some("hello")
fmt.Println(hello1.Equal(world)) // Some("hello") == Some("world")
Output: true true false false true false
func (Option[T]) FlatMap ¶
FlatMap executes the mapper function if value is present or returns None if absent. Play: https://go.dev/play/p/OXO-zJx6n5r
Example (None) ¶
none := None[int]()
result := none.FlatMap(func(i int) Option[int] {
return Some(21)
})
fmt.Println(result.IsPresent(), result.OrEmpty())
Output: false 0
Example (Some) ¶
some := Some(42)
result := some.FlatMap(func(i int) Option[int] {
return Some(21)
})
fmt.Println(result.IsPresent(), result.OrEmpty())
Output: true 21
func (Option[T]) ForEach ¶
func (o Option[T]) ForEach(onValue func(value T))
ForEach executes the given side-effecting function of value is present.
func (Option[T]) Get ¶
Get returns value and presence. Play: https://go.dev/play/p/0-JBa1usZRT
Example (None) ¶
none := None[int]() result, ok := none.Get() fmt.Println(result) fmt.Println(ok)
Output: 0 false
Example (Some) ¶
some := Some(42) result, ok := some.Get() fmt.Println(result) fmt.Println(ok)
Output: 42 true
func (Option[T]) IsAbsent ¶
IsAbsent returns false when value is present. Play: https://go.dev/play/p/23e2zqyVOQm
Example (None) ¶
none := None[int]() result := none.IsAbsent() fmt.Println(result)
Output: true
Example (Some) ¶
some := Some(42) result := some.IsAbsent() fmt.Println(result)
Output: false
func (Option[T]) IsNone ¶ added in v1.16.0
IsNone is an alias to IsAbsent. Play: https://go.dev/play/p/EdqxKhborIP
func (Option[T]) IsPresent ¶
IsPresent returns false when value is absent. Play: https://go.dev/play/p/nDqIaiihyCA
Example (None) ¶
none := None[int]() result := none.IsPresent() fmt.Println(result)
Output: false
Example (Some) ¶
some := Some(42) result := some.IsPresent() fmt.Println(result)
Output: true
func (Option[T]) IsSome ¶ added in v1.16.0
IsSome is an alias to IsPresent. Play: https://go.dev/play/p/DyvGRy7fP9m
func (Option[T]) Map ¶
Map executes the mapper function if value is present or returns None if absent. Play: https://go.dev/play/p/mvfP3pcP_eJ
Example (None) ¶
none := None[int]()
result := none.Map(func(i int) (int, bool) {
return 1234, true
})
fmt.Println(result.IsPresent(), result.OrEmpty())
Output: false 0
Example (Some) ¶
some := Some(42)
result := some.Map(func(i int) (int, bool) {
return 1234, true
})
fmt.Println(result.IsPresent(), result.OrEmpty())
Output: true 1234
func (Option[T]) MapNone ¶
MapNone executes the mapper function if value is absent or returns Option. Play: https://go.dev/play/p/_KaHWZ6Q17b
Example (None) ¶
none := None[int]()
result := none.MapNone(func() (int, bool) {
return 1234, true
})
fmt.Println(result.IsPresent(), result.OrEmpty())
Output: true 1234
Example (Some) ¶
some := Some(42)
result := some.MapNone(func() (int, bool) {
return 1234, true
})
fmt.Println(result.IsPresent(), result.OrEmpty())
Output: true 42
func (Option[T]) MapValue ¶ added in v1.14.0
MapValue executes the mapper function if value is present or returns None if absent.
func (Option[T]) MarshalBinary ¶ added in v1.4.0
MarshalBinary is the interface implemented by an object that can marshal itself into a binary form.
func (Option[T]) MarshalJSON ¶ added in v1.3.0
MarshalJSON encodes Option into json. Go 1.20+ relies on the IsZero method when the `omitempty` tag is used unless a custom MarshalJSON method is defined. Then the IsZero method is ignored. current best workaround is to instead use `omitzero` tag with Go 1.24+
Example (None) ¶
type test struct {
Email Option[string] `json:"email"`
}
value := test{Email: None[string]()}
result, err := json.Marshal(value)
fmt.Println(string(result))
fmt.Println(err)
Output: {"email":null} <nil>
Example (Some) ¶
type test struct {
Email Option[string] `json:"email"`
}
value := test{Email: Some("[email protected]")}
result, err := json.Marshal(value)
fmt.Println(string(result))
fmt.Println(err)
Output: {"email":"[email protected]"} <nil>
func (Option[T]) MarshalText ¶ added in v1.4.0
MarshalText implements the encoding.TextMarshaler interface.
func (Option[T]) Match ¶
Match executes the first function if value is present and second function if absent. It returns a new Option. Play: https://go.dev/play/p/1V6st3LDJsM
Example (None) ¶
none := None[int]()
result := none.Match(
func(i int) (int, bool) {
return 0, false
},
func() (int, bool) {
return 2, true
},
)
fmt.Println(result.IsPresent(), result.OrEmpty())
Output: true 2
Example (Some) ¶
some := Some(42)
result := some.Match(
func(i int) (int, bool) {
return 0, false
},
func() (int, bool) {
return 2, true
},
)
fmt.Println(result.IsPresent(), result.OrEmpty())
Output: false 0
func (Option[T]) MustGet ¶
func (o Option[T]) MustGet() T
MustGet returns value if present or panics instead. Play: https://go.dev/play/p/RVBckjdi5WR
Example (Some) ¶
some := Some(42) result := some.MustGet() fmt.Println(result)
Output: 42
func (Option[T]) OrElse ¶
func (o Option[T]) OrElse(fallback T) T
OrElse returns value if present or default value. Play: https://go.dev/play/p/TrGByFWCzXS
Example (None) ¶
none := None[int]() result := none.OrElse(1234) fmt.Println(result)
Output: 1234
Example (Some) ¶
some := Some(42) result := some.OrElse(1234) fmt.Println(result)
Output: 42
func (Option[T]) OrEmpty ¶
func (o Option[T]) OrEmpty() T
OrEmpty returns value if present or empty value. Play: https://go.dev/play/p/SpSUJcE-tQm
Example (None) ¶
none := None[int]() result := none.OrEmpty() fmt.Println(result)
Output: 0
Example (Some) ¶
some := Some(42) result := some.OrEmpty() fmt.Println(result)
Output: 42
func (Option[T]) Size ¶
Size returns 1 when value is present or 0 instead. Play: https://go.dev/play/p/7ixCNG1E9l7
Example (None) ¶
none := None[int]() result := none.Size() fmt.Println(result)
Output: 0
Example (Some) ¶
some := Some(42) result := some.Size() fmt.Println(result)
Output: 1
func (Option[T]) ToPointer ¶ added in v1.10.0
func (o Option[T]) ToPointer() *T
ToPointer returns value if present or a nil pointer. Play: https://go.dev/play/p/N43w92SM-Bs
Example (None) ¶
none := None[int]() result := none.ToPointer() fmt.Println(result)
Output: <nil>
Example (Some) ¶
some := Some(42) result := some.ToPointer() fmt.Println(*result)
Output: 42
func (*Option[T]) UnmarshalBinary ¶ added in v1.4.0
UnmarshalBinary is the interface implemented by an object that can unmarshal a binary representation of itself.
func (*Option[T]) UnmarshalJSON ¶ added in v1.3.0
UnmarshalJSON decodes Option from json.
Example (None) ¶
type test struct {
Email Option[string] `json:"email"`
}
value := []byte(`{"email":null}`)
var result test
err := json.Unmarshal(value, &result)
fmt.Println(result.Email.Get())
fmt.Println(err)
Output: false <nil>
Example (Some) ¶
type test struct {
Email Option[string] `json:"email"`
}
value := []byte(`{"email":"[email protected]"}`)
var result test
err := json.Unmarshal(value, &result)
fmt.Println(result.Email.Get())
fmt.Println(err)
Output: [email protected] true <nil>
func (*Option[T]) UnmarshalText ¶ added in v1.4.0
UnmarshalText implements the encoding.TextUnmarshaler interface.
type Result ¶
type Result[T any] struct { // contains filtered or unexported fields }
Result represents a result of an action having one of the following output: success or failure. An instance of Result is an instance of either Ok or Err. It could be compared to `Either[error, T]`.
Example (Err) ¶
ko := Err[int](err) result := ko.OrElse(1234) _err := ko.Error() fmt.Println(result, _err)
Output: 1234 error
Example (Ok) ¶
ok := Ok(42) result := ok.OrElse(1234) _err := ok.Error() fmt.Println(result, _err)
Output: 42 <nil>
func Do ¶ added in v1.12.0
Do executes a function within a monadic context, capturing any errors that occur. If the function executes successfully, its result is wrapped in a successful Result. If the function panics (indicating a failure), the panic is caught and converted into an error Result.
Example ¶
a := Ok("Hello, World!")
b := Some("42")
result := Do(func() []string {
return []string{
a.MustGet(),
b.MustGet(),
}
})
fmt.Println(result.IsError())
fmt.Println(result.MustGet())
Output: false [Hello, World! 42]
Example (Panic) ¶
a := Ok("Hello, World!")
b := Some("42")
c := Err[string](errors.New("result error"))
result := Do(func() []string {
return []string{
a.MustGet(),
b.MustGet(),
c.MustGet(), // would panic without Do-notation
}
})
fmt.Println(result.IsError())
fmt.Println(result.Error().Error())
Output: true result error
func Err ¶
Err builds a Result when value is invalid. Play: https://go.dev/play/p/PDwADdzNoyZ
Example ¶
ko := Err[int](err) result := ko.OrElse(1234) _err := ko.Error() fmt.Println(result, _err)
Output: 1234 error
func Errf ¶ added in v1.10.0
Errf builds a Result when value is invalid. Errf formats according to a format specifier and returns the error as a value that satisfies Result[T]. Play: https://go.dev/play/p/N43w92SM-Bs
Example ¶
ko := Errf[int]("error")
result := ko.OrElse(1234)
_err := ko.Error()
fmt.Println(result, _err)
Output: 1234 error
func Ok ¶
Ok builds a Result when value is valid. Play: https://go.dev/play/p/PDwADdzNoyZ
Example ¶
ok := Ok(42) result := ok.OrElse(1234) _err := ok.Error() fmt.Println(result, _err)
Output: 42 <nil>
func Try ¶ added in v1.1.0
Try returns either a Ok or Err object. Play: https://go.dev/play/p/ilOlQx-Mx42
Example (Err) ¶
randomFunc := func() (int, error) {
return 42, err
}
result := Try(randomFunc)
value, err := result.Get()
fmt.Println(value)
fmt.Println(err)
Output: 0 error
Example (Ok) ¶
randomFunc := func() (int, error) {
return 42, nil
}
result := Try(randomFunc)
value, err := result.Get()
fmt.Println(value)
fmt.Println(err)
Output: 42 <nil>
func TupleToResult ¶
TupleToResult convert a pair of T and error into a Result. Play: https://go.dev/play/p/KWjfqQDHQwa
Example ¶
randomFunc := func() (int, error) {
return 42, err
}
value, _err := randomFunc()
none := TupleToResult(value, _err)
result := none.OrElse(1234)
fmt.Println(result)
Output: 1234
func (Result[T]) Error ¶
Error returns error when value is invalid or nil. Play: https://go.dev/play/p/CSkHGTyiXJ5
Example (Err) ¶
ko := Err[int](err) result := ko.Error() fmt.Println(result)
Output: error
Example (Ok) ¶
ok := Ok(42) result := ok.Error() fmt.Println(result)
Output: <nil>
func (Result[T]) FlatMap ¶
FlatMap executes the mapper function if Result is valid. It returns a new Result. Play: https://go.dev/play/p/Ud5QjZOqg-7
Example (Err) ¶
ko := Err[int](err)
result := ko.FlatMap(
func(i int) Result[int] {
return Ok(1234)
},
)
fmt.Println(result.IsError(), result.OrEmpty(), result.Error())
Output: true 0 error
Example (Ok) ¶
ok := Ok(42)
result := ok.FlatMap(
func(i int) Result[int] {
return Ok(1234)
},
)
fmt.Println(result.IsError(), result.OrEmpty(), result.Error())
Output: false 1234 <nil>
func (Result[T]) ForEach ¶
func (r Result[T]) ForEach(mapper func(value T))
ForEach executes the given side-effecting function if Result is valid.
func (Result[T]) Get ¶
Get returns value and error. Play: https://go.dev/play/p/8KyX3z6TuNo
Example (Err) ¶
ko := Err[int](err) result, err := ko.Get() fmt.Println(result) fmt.Println(err)
Output: 0 error
Example (Ok) ¶
ok := Ok(42) result, err := ok.Get() fmt.Println(result) fmt.Println(err)
Output: 42 <nil>
func (Result[T]) IsError ¶
IsError returns true when value is invalid. Play: https://go.dev/play/p/xkV9d464scV
Example (Err) ¶
ko := Err[int](err) result := ko.IsError() fmt.Println(result)
Output: true
Example (Ok) ¶
ok := Ok(42) result := ok.IsError() fmt.Println(result)
Output: false
func (Result[T]) IsOk ¶
IsOk returns true when value is valid. Play: https://go.dev/play/p/sfNvBQyZfgU
Example (Err) ¶
ko := Err[int](err) result := ko.IsOk() fmt.Println(result)
Output: false
Example (Ok) ¶
ok := Ok(42) result := ok.IsOk() fmt.Println(result)
Output: true
func (Result[T]) Map ¶
Map executes the mapper function if Result is valid. It returns a new Result. Play: https://go.dev/play/p/-ndpN_b_OSc
Example (Err) ¶
ko := Err[int](err)
result := ko.Map(
func(i int) (int, error) {
return i * 2, nil
},
)
fmt.Println(result.IsError(), result.OrEmpty(), result.Error())
Output: true 0 error
Example (Ok) ¶
ok := Ok(42)
result := ok.Map(
func(i int) (int, error) {
return i * 2, nil
},
)
fmt.Println(result.IsError(), result.OrEmpty(), result.Error())
Output: false 84 <nil>
func (Result[T]) MapErr ¶
MapErr executes the mapper function if Result is invalid. It returns a new Result. Play: https://go.dev/play/p/WraZixg9GGf
Example (Err) ¶
ko := Err[int](err)
result := ko.MapErr(
func(_err error) (int, error) {
return 1234, nil
},
)
fmt.Println(result.IsError(), result.OrEmpty(), result.Error())
Output: false 1234 <nil>
Example (Ok) ¶
ok := Ok(42)
result := ok.MapErr(
func(_err error) (int, error) {
return 1234, nil
},
)
fmt.Println(result.IsError(), result.OrEmpty(), result.Error())
Output: false 42 <nil>
func (Result[T]) MapValue ¶ added in v1.15.0
MapValue executes the mapper function if Result is valid. It returns a new Result.
Example (Err) ¶
ko := Err[string](err) result := ko.MapValue(strings.ToTitle) fmt.Println(result.IsError(), result.OrEmpty(), result.Error())
Output: true error
Example (Ok) ¶
ok := Ok("hello")
result := ok.MapValue(strings.ToTitle)
fmt.Println(result.IsError(), result.OrEmpty(), result.Error())
Output: false HELLO <nil>
func (Result[T]) MarshalJSON ¶ added in v1.11.0
MarshalJSON encodes Result into json, following the JSON-RPC specification for results, with one exception: when the result is an error, the "code" field is not included. Reference: https://www.jsonrpc.org/specification
func (Result[T]) Match ¶
func (r Result[T]) Match(onSuccess func(value T) (T, error), onError func(err error) (T, error)) Result[T]
Match executes the first function if Result is valid and second function if invalid. It returns a new Result. Play: https://go.dev/play/p/-_eFaLJ31co
Example (Err) ¶
ko := Err[int](err)
result := ko.Match(
func(i int) (int, error) {
return i * 2, nil
},
func(err error) (int, error) {
return 21, nil
},
)
fmt.Println(result.IsError(), result.OrEmpty(), result.Error())
Output: false 21 <nil>
Example (Ok) ¶
ok := Ok(42)
result := ok.Match(
func(i int) (int, error) {
return i * 2, nil
},
func(err error) (int, error) {
return 21, nil
},
)
fmt.Println(result.IsError(), result.OrEmpty(), result.Error())
Output: false 84 <nil>
func (Result[T]) MustGet ¶
func (r Result[T]) MustGet() T
MustGet returns value when Result is valid or panics. Play: https://go.dev/play/p/8LSlndHoTAE
Example (Ok) ¶
ok := Ok(42) result := ok.MustGet() fmt.Println(result)
Output: 42
func (Result[T]) OrElse ¶
func (r Result[T]) OrElse(fallback T) T
OrElse returns value when Result is valid or default value. Play: https://go.dev/play/p/MN_ULx0soi6
Example (Err) ¶
ko := Err[int](err) result := ko.OrElse(1234) fmt.Println(result)
Output: 1234
Example (Ok) ¶
ok := Ok(42) result := ok.OrElse(1234) fmt.Println(result)
Output: 42
func (Result[T]) OrEmpty ¶
func (r Result[T]) OrEmpty() T
OrEmpty returns value when Result is valid or empty value. Play: https://go.dev/play/p/rdKtBmOcMLh
Example (Err) ¶
ko := Err[int](err) result := ko.OrEmpty() fmt.Println(result)
Output: 0
Example (Ok) ¶
ok := Ok(42) result := ok.OrEmpty() fmt.Println(result)
Output: 42
func (Result[T]) ToEither ¶
ToEither transforms a Result into an Either type. Play: https://go.dev/play/p/Uw1Zz6b952q
Example (Err) ¶
ko := Err[int](err) either := ko.ToEither() err, isLeft := either.Left() value, isRight := either.Right() fmt.Println(isLeft, isRight) fmt.Println(err) fmt.Println(value)
Output: true false error 0
Example (Ok) ¶
ok := Ok(42) either := ok.ToEither() err, isLeft := either.Left() value, isRight := either.Right() fmt.Println(isLeft, isRight) fmt.Println(err) fmt.Println(value)
Output: false true <nil> 42
func (*Result[T]) UnmarshalJSON ¶ added in v1.11.0
UnmarshalJSON decodes json into Result. If "error" is set, the result is an Err containing the error message as a generic error object. Otherwise, the result is an Ok containing the result. If the JSON object contains netiher an error nor a result, the result is an Ok containing an empty value. If the JSON object contains both an error and a result, the result is an Err. Finally, if the JSON object contains an error but is not structured correctly (no message field), the unmarshaling fails.
type State ¶
State represents a function `(S) -> (A, S)`, where `S` is state, `A` is result.
func ReturnState ¶
type Task ¶
type Task[R any] struct { // contains filtered or unexported fields }
Task represents a non-deterministic asynchronous computation that can cause side effects, yields a value of type `R` and never fails.
Example ¶
t := time.Date(2024, 6, 22, 0, 0, 0, 0, time.Local)
task := NewTask(func() *Future[int] {
return NewFuture(func(resolve func(int), reject func(error)) {
resolve(t.Year())
})
})
// returns a future
future := task.Run()
// a Task never fail
result, _ := future.Collect()
fmt.Println(result)
Output: 2024
func NewTaskFromIO ¶
NewTaskFromIO instanciates a new Task from an existing IO.
type Task1 ¶
Task1 represents a non-deterministic asynchronous computation that can cause side effects, yields a value of type `R` and never fails.
func NewTaskFromIO1 ¶
NewTaskFromIO1 instanciates a new Task1 from an existing IO1.
type Task2 ¶
Task2 represents a non-deterministic asynchronous computation that can cause side effects, yields a value of type `R` and never fails.
func NewTaskFromIO2 ¶
NewTaskFromIO2 instanciates a new Task2 from an existing IO2.
type Task3 ¶
Task3 represents a non-deterministic asynchronous computation that can cause side effects, yields a value of type `R` and never fails.
func NewTaskFromIO3 ¶
NewTaskFromIO3 instanciates a new Task3 from an existing IO3.
type Task4 ¶
Task4 represents a non-deterministic asynchronous computation that can cause side effects, yields a value of type `R` and never fails.
func NewTaskFromIO4 ¶
NewTaskFromIO4 instanciates a new Task4 from an existing IO4.
type Task5 ¶
type Task5[R any, A any, B any, C any, D any, E any] struct { // contains filtered or unexported fields }
Task5 represents a non-deterministic asynchronous computation that can cause side effects, yields a value of type `R` and never fails.
func NewTask5 ¶
func NewTask5[R any, A any, B any, C any, D any, E any](f ff5[R, A, B, C, D, E]) Task5[R, A, B, C, D, E]
NewTask5 instanciates a new Task5.
type TaskEither ¶
TaskEither represents a non-deterministic asynchronous computation that can cause side effects, yields a value of type `R` and can fail.
func NewTaskEither ¶
func NewTaskEither[R any](f ff0[R]) TaskEither[R]
NewTaskEither instanciates a new TaskEither.
func NewTaskEitherFromIO ¶
func NewTaskEitherFromIO[R any](io IO[R]) TaskEither[R]
NewTaskEitherFromIO instanciates a new TaskEither from an existing IO.
func (TaskEither[R]) Match ¶
func (t TaskEither[R]) Match(onLeft func(error) Either[error, R], onRight func(R) Either[error, R]) Either[error, R]
Match executes the first function if task succeeded and second function if task failed. It returns a new Option.
func (TaskEither[R]) OrElse ¶
func (t TaskEither[R]) OrElse(fallback R) R
OrElse returns value if task succeeded or default value.
func (TaskEither[R]) ToEither ¶
func (t TaskEither[R]) ToEither() Either[error, R]
ToEither converts TaskEither to Either.
func (TaskEither[R]) ToTask ¶
func (t TaskEither[R]) ToTask(fallback R) Task[R]
ToTask converts TaskEither to Task
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package either provides cross type transformations for `mo.Either`.
|
Package either provides cross type transformations for `mo.Either`. |
|
Package either3 provides cross type transformations for `mo.Either`.
|
Package either3 provides cross type transformations for `mo.Either`. |
|
Package either4 provides cross type transformations for `mo.Either`.
|
Package either4 provides cross type transformations for `mo.Either`. |
|
Package either5 provides cross type transformations for `mo.Either`.
|
Package either5 provides cross type transformations for `mo.Either`. |
|
Package option provides cross type transformations for `mo.Option`.
|
Package option provides cross type transformations for `mo.Option`. |
|
Package result provides cross type transformations for `mo.Result`.
|
Package result provides cross type transformations for `mo.Result`. |