Documentation ΒΆ
Index ΒΆ
- 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]
- 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]) 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]) IsAbsent() bool
- func (o Option[T]) IsPresent() 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]) 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]) Size() int
- 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]) 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]
- 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 ΒΆ
- 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)
- Err
- 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
- NewFuture (Reject)
- NewFuture (Resolve)
- None
- Ok
- Option (None)
- Option (Some)
- 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.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)
- 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.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
- TupleToOption
- TupleToResult
Constants ΒΆ
This section is empty.
Variables ΒΆ
This section is empty.
Functions ΒΆ
This section is empty.
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)
Output:
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
Output:
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()
Output:
Example (Resolve) ΒΆ
NewFuture(func(resolve func(string), reject func(error)) { resolve("foobar") }).Cancel()
Output:
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 ΒΆ
Catch 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 ΒΆ
io := NewIO(func() int { return time.Now().Year() }) result1 := io.Run() result2 := io.Run() result3 := io.Run() fmt.Println(result1) fmt.Println(result2) fmt.Println(result3)
Output: 2022 2022 2022
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 }
IO represents a non-deterministic synchronous computation that can cause side effects, yields a value of type `R` and can fail.
func NewIOEither ΒΆ
NewIOEither instanciates a new IO.
type IOEither1 ΒΆ
IO1 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 ΒΆ
IO2 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 ΒΆ
IO3 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 }
IO4 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 }
IO5 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 None ΒΆ
None builds an Option when value is absent.
Example ΒΆ
none := None[int]() result := none.OrElse(1234) fmt.Println(result)
Output: 1234
func Some ΒΆ
Some builds an Option when value is present.
Example ΒΆ
some := Some(42) result := some.OrElse(1234) fmt.Println(result)
Output: 42
func TupleToOption ΒΆ
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]) FlatMap ΒΆ
FlatMap executes the mapper function if value is present or returns None if absent.
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.
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 true when value is present.
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]) IsPresent ΒΆ
IsPresent returns true when value is absent.
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]) Map ΒΆ
Map executes the mapper function if value is present or returns None if absent.
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.
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]) Match ΒΆ
Match executes the first function if value is present and second function if absent. It returns a new Option.
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.
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.
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
type Result ΒΆ
type Result[T any] struct { // contains filtered or unexported fields }
Result respresent 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 Err ΒΆ
Err builds a Result when value is invalid.
Example ΒΆ
ko := Err[int](err) result := ko.OrElse(1234) _err := ko.Error() fmt.Println(result, _err)
Output: 1234 error
func Ok ΒΆ
Ok builds a Result when value is valid.
Example ΒΆ
ok := Ok(42) result := ok.OrElse(1234) _err := ok.Error() fmt.Println(result, _err)
Output: 42 <nil>
func TupleToResult ΒΆ
TupleToResult convert a pair of T and error into a Result.
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.
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.
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 ΒΆ
MustGet returns value and error.
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.
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.
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.
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.
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]) 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.
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.
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.
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.
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.
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
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 ΒΆ
task := NewTask(func() *Future[int] { return NewFuture(func(resolve func(int), reject func(error)) { resolve(time.Now().Year()) }) }) // returns a future future := task.Run() // a Task never fail result, _ := future.Collect() fmt.Println(result)
Output: 2022
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]
ToTask converts TaskEither to Task
func (TaskEither[R]) ToTask ΒΆ
func (t TaskEither[R]) ToTask(fallback R) Task[R]
ToTask converts TaskEither to Task