Documentation
¶
Overview ¶
Package maybe defines interfaces and structs for encapsulating a value (a Just) or an absent value (a Nothing).
Without Maybe, code must rely on a convention like nil or zero value to indicate that a value is not supplied. With a Maybe, the intention becomes more clear.
Two constructors are provided for creating a Maybe; Just which creates a Maybe encapsulating a value, and Nothing which creates a Maybe encapsulating the absence of a value. See those functions for more information.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type FlatMapper ¶
FlatMapper defines a function with one argument that maps from the argument's type to a Maybe of that type.
type Getter ¶
type Getter[T any] func() T
Getter defines a function that gets the default value for a Maybe that encapsulates an absent value.
type MapperNoError ¶
type MapperNoError[T, R any] func( T, ) R
MapperNoError defines a function with one argument of any type that returns a single value.
type Maybe ¶
type Maybe[T any] interface { // IsJust returns true if the Maybe encapsulates a value and false if the // Maybe encapsulates nothing. IsJust() bool // MustGet returns the encapsulated value if the Maybe encapsulates a value // and panics if the Maybe encapsulates nothing. MustGet() T // OrElse returns the encapsulated value if the Maybe encapsulates a value // and returns the given default if the Maybe encapsulates nothing. OrElse(d T) T // OrElseGet returns the encapsulated value if the Maybe encapsulates a value // and returns the result of calling the given Getter function if the Maybe // encapsulates nothing. OrElseGet(Getter[T]) T }
Maybe encapsulates an optional value that may or may not exist.
func FlatMap ¶
func FlatMap[T, R any](f FlatMapper[T, R], m Maybe[T]) Maybe[R]
FlatMap applies the given FlatMapper to the Maybe. If the Maybe encapsulates a value then that value is passed to the Mapper and the Mapper's return value is returned. If the Maybe encapsulates an absent value then Maybe encapsulating an absent value of the return type of the Mapper is returned.
func Map ¶
func Map[T, R any](f MapperNoError[T, R], m Maybe[T]) Maybe[R]
Map applies the given MapperNoError to the Maybe. If the Maybe encapsulates a value then that value is passed to the Mapper and a Maybe of the return type is returned. If the Maybe encapsulates nothing then the nothing is propagated to a Maybe of the return type.