Help us shape the future of Go! Take the 2026 Go Developer Survey

either

package
v0.11.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 6, 2022 License: MIT Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Exists

func Exists[L, R any](predicate func(right R) bool) func(Either[L, R]) bool

Returns `false` if `Left` or returns the boolean result of the application of the given predicate to the `Right` value

func FromOption

func FromOption[L, R any](onNone func() L) func(o opt.Option[R]) Either[L, R]

Constructor of Either from an Option. Returns a Left in case of None storing the callback return value as the error argument Returns a Right in case of Some with the option value.

func FromPredicate

func FromPredicate[L, R any](predicate func(value R) bool, onLeft func() L) func(R) Either[L, R]

Constructor of Either from a predicate. Returns a Left if the predicate function over the value return false. Returns a Right if the predicate function over the value return true.

func GetOrElse

func GetOrElse[L, R any](onLeft func(left L) R) func(Either[L, R]) R

Extracts the value out of the Either, if it exists. Otherwise returns the result of the callback function that takes the error as argument.

func IsLeft

func IsLeft[L, R any](e Either[L, R]) bool

Helper to check if the Either has an error

func IsRight

func IsRight[L, R any](e Either[L, R]) bool

Helper to check if the Either has a value

func Map

func Map[L, R, T any](onRight func(right R) T) func(Either[L, R]) Either[L, T]

Map over the Either value if it exists. Otherwise return the Either itself

func MapLeft

func MapLeft[L, R, T any](fn func(left L) T) func(Either[L, R]) Either[T, R]

Map over the Either error if it exists. Otherwise return the Either with the new error type

func Match

func Match[L, R, T any](onLeft func(left L) T, onRight func(right R) T) func(Either[L, R]) T

Extracts the value out of the Either. Returns a new type running the succes or error callbacks which are taking respectively the error or value as an argument.

Types

type Either

type Either[L, R any] struct {
	// contains filtered or unexported fields
}

Base struct

func Flatten

func Flatten[L, R any](e Either[L, Either[L, R]]) Either[L, R]

Removes one level of nesting. Returns its bound argument into the outer level.

func FromError

func FromError[R any](value R, e error) Either[error, R]

Constructor of Either from any couple of mutually exclusive `value` and `error`. Returns a Left in case we have an error, Right if we have a value and error is nil.

func FromErrorFn

func FromErrorFn[R any](fn func() (value R, e error)) Either[error, R]

Constructor of Either from any lazy function that returns a couple of mutually exclusive `value` and `error`. Returns a Left in case we have a return error, Right if we have return value and error is nil.

func Left

func Left[L, R any](value L) Either[L, R]

Constructor for Either with a value

func Right[L, R any](value R) Either[L, R]

Constructor for Either with an error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL