Documentation
¶
Overview ¶
Package maybe implements a Maybe{value, ok} "sum type" that has a value only when ok is true.
Note that in many cases, it is more idiomatic for a function to return a naked (value, ok). Use WrapFunc to convert such a function to return a Maybe type.
Index ¶
- func Lift[A any, B any](f func(a A) B) func(a A) Maybe[B]
- func UnwrapFunc[A any, B any](f func(a A) Maybe[B]) func(a A) (B, bool)
- func WrapFunc[A any, B any](f func(a A) (B, bool)) func(a A) Maybe[B]
- type Maybe
- func Apply[A any, B any](a Maybe[A], f Maybe[func(a A) B]) Maybe[B]
- func FlatApply[A any, B any](a Maybe[A], f Maybe[func(a A) Maybe[B]]) Maybe[B]
- func FlatMap[A any, B any](a Maybe[A], f func(a A) Maybe[B]) Maybe[B]
- func Map[A any, B any](a Maybe[A], f func(a A) B) Maybe[B]
- func New[V any](value V, ok bool) Maybe[V]
- func Nothing[V any]() Maybe[V]
- func Some[V any](value V) Maybe[V]
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func UnwrapFunc ¶
UnwrapFunc converts a function of the form f(x) => Maybe{value, ok} to the form f(x) => (value, ok)
Types ¶
type Maybe ¶
Maybe is a (value, ok) "sum type" that has a value only when ok is true.
func Apply ¶
Apply applies a function f(x) => y, but function f is itself a Maybe. If either a or f are Nothing, returns Nothing. Otherwise, returns Some(f(a.Value)).
This is useful for working with partial function application.
doubler := partial.Left2(mul, 2) maybe.Apply(Some(x), maybe.Map(Some(y), doubler))
For a function that applies f(x) => Maybe(y) instead, see FlatApply.
func FlatApply ¶
FlatApply applies Maybe(f(x)) => Maybe(y). That is, function f is itself a Maybe. If either a or f are Nothing, returns Nothing. Otherwise, returns f(a.Value).
This is useful for working with partial function application.)
This is called "flat" because applying function f with the ordinary apply would give Maybe(Maybe(y)).
For a function that applies f(x) => y instead, see Apply.
func FlatMap ¶
FlatMap applies a function f(x) => Maybe(y). If a is Nothing, returns Nothing, otherwise returns f(a.Value).
This is called "flat" because applying function f with the ordinary map would give Maybe(Maybe(y)).
For a function that applies f(x) => y instead, see Map.
func Map ¶
Map applies a function f(x) => y. If a is Nothing, returns Nothing. Otherwise, returns Some(f(a.Value)).
For a function that applies f(x) => Maybe(y) instead, see FlatMap.
func New ¶
New returns a Maybe. It is syntax sugar for Maybe{value, ok}. If ok is a known constant, use Some or Nothing instead.
func (Maybe[V]) Else ¶
func (m Maybe[V]) Else(v V) V
Else returns the Maybe's value (if ok), otherwise returns the provided argument instead.
func (Maybe[V]) Filter ¶
Filter examines a Maybe. If ok and f(value) returns true, returns Some(value), otherwise returns Nothing.
func (Maybe[V]) Match ¶
Match examines a Maybe and returns true if ok and if the provided predicate returns true for the value.