maybe

package
v2.0.14 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2022 License: MIT Imports: 1 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Lift

func Lift[A any, B any](
	f func(a A) B,
) func(a A) Maybe[B]

Lift converts a function of the form f(a) => b to the form f(a) => Maybe(b).

func UnwrapFunc

func UnwrapFunc[A any, B any](
	f func(a A) Maybe[B],
) func(a A) (B, bool)

UnwrapFunc converts a function of the form f(x) => Maybe{value, ok} to the form f(x) => (value, ok)

func WrapFunc

func WrapFunc[A any, B any](
	f func(a A) (B, bool),
) func(a A) Maybe[B]

WrapFunc converts a function of the form f(x) => (value, ok) to the form f(x) => Maybe{value, ok}.

Types

type Maybe

type Maybe[V any] struct {
	Value V
	Ok    bool
}

Maybe is a (value, ok) "sum type" that has a value only when ok is true.

func Apply

func Apply[A any, B any](
	a Maybe[A],
	f Maybe[func(a A) B],
) Maybe[B]

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

func FlatApply[A any, B any](
	a Maybe[A],
	f Maybe[func(a A) Maybe[B]],
) Maybe[B]

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

func FlatMap[A any, B any](
	a Maybe[A],
	f func(a A) Maybe[B],
) Maybe[B]

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

func Map[A any, B any](
	a Maybe[A],
	f func(a A) B,
) Maybe[B]

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

func New[V any](value V, ok bool) Maybe[V]

New returns a Maybe. It is syntax sugar for Maybe{value, ok}. If ok is a known constant, use Some or Nothing instead.

func Nothing

func Nothing[V any]() Maybe[V]

Nothing returns a (typed) Maybe that has no value.

func Some

func Some[V any](value V) Maybe[V]

Some (a.k.a. "Just") returns a Maybe that contains a value.

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

func (m Maybe[V]) Filter(f func(v V) bool) Maybe[V]

Filter examines a Maybe. If ok and f(value) returns true, returns Some(value), otherwise returns Nothing.

func (Maybe[V]) Match

func (m Maybe[V]) Match(f func(v V) bool) bool

Match examines a Maybe and returns true if ok and if the provided predicate returns true for the value.

func (Maybe[V]) Must

func (m Maybe[V]) Must() V

Must returns a Maybe's value. If the Maybe is not ok, panics.

func (Maybe[V]) MustNot

func (m Maybe[V]) MustNot()

MustNot panics if the Maybe is ok.

func (Maybe[V]) Unpack

func (m Maybe[V]) Unpack() (V, bool)

Unpack returns a plain (value, ok) tuple from a Maybe.

Jump to

Keyboard shortcuts

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