Type: Maybe

class data.maybe.Maybe
type Maybe(α) = Nothing | Just(α)

implements
  Applicative(α), Functor(α), Chain(α), Monad(α), ToString

A structure for values that may not be present, or computations that may fail.

Comparing and testing

#isNothing

Maybe.prototype.isNothing
Boolean

True if the Maybe(α) structure contains a Nothing.

#isJust

Maybe.prototype.isJust
Boolean

True if the Maybe(α) structure contains a Just.

#isEqual()

Maybe.prototype.isEqual(aMaybe)
@Maybe(α) => Maybe(α) → Boolean

Tests if two Maybe(α) contains are similar.

Contents are checked using reference equality.

Constructing

.Nothing()

static Maybe.Nothing()
Unit → Maybe(α)

Constructs a new Maybe(α) structure with an absent value. Commonly used to represent a failure.

.Just()

static Maybe.Just(value)
α → Maybe(α)

Constructs a new Maybe(α) structure that holds the single value α. Commonly used to represent a success.

.of()

static Maybe.of(value)
α → Maybe(α)

Constructs a new Maybe(α) structure that holds the single value α.

.fromNullable()

static Maybe.fromNullable(value)
α | null | undefined → Maybe(α)

Constructs a new Maybe(α) value from a nullable type.

If the value is null or undefined, returns a Nothing, otherwise returns the value wrapped in a Just.

.fromEither()

static Maybe.fromEither(value)
Either(α, β) → Maybe(β)

Constructs a new Maybe(β) from an Either(α, β) value.

.fromValidation()

static Maybe.fromValidation(value)
Validation(α, β) → Maybe(β)

Constructs a new Maybe(β) from a Validation(α, β) value.

Converting

#toString()

Maybe.prototype.toString()
@Maybe(α) => Unit → String

Returns a textual representation of the structure.

#toJSON()

Maybe.prototype.toJSON()
@Maybe(α) => Unit → Object

Returns a JSON serialisation of the structure.

Extracting

#get()

Maybe.prototype.get()
Raises:
  • TypeError - if the structure is a Nothing.
@Maybe(α) => Unit → α :: throws

Extracts the value out of the structure, if it exists.

#getOrElse()

Maybe.prototype.getOrElse(default)
@Maybe(α) => α → α

Extracts the value out of the structure, if it exists. Otherwise return the given default value.

Transforming

#ap()

Maybe.prototype.ap(anApplicative)
@Maybe(α → β), f:Applicative(_) => f(α) → f(β)

Applies the function inside the structure to another Applicative type.

#map()

Maybe.prototype.map(transformation)
@Maybe(α) => (α → β) → Maybe(β)

Transforms the value of this structure using a regular unary function.

#chain()

Maybe.prototype.chain(transformation)
@Maybe(α), m:Monad(_) => (α → m(β)) → m(β)

Transforms the value of this structure using an unary function over monads.

#orElse()

Maybe.prototype.orElse(transformation)
@Maybe(α) => (Unit → Maybe(β)) → Maybe(β)

Transforms the failure into a new Maybe structure.

#cata()

Maybe.prototype.cata(aPattern)
@Maybe(α) => { Nothing: Unit → β, Just: α → β } → β

Applies a function to each case in the data structure.