Module: data.maybe

Stability:3 - Stable
Bug Tracker:https://github.com/folktale/data.maybe/issues
Version:1.2.0
Repository:https://github.com/folktale/data.maybe
Portability:Portable
npm package:data.maybe

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

The class models two different cases:

  • Just α — represents a Maybe(α) that contains a value. α may be any value, including null and undefined.
  • Nothing — represents a Maybe(α) that has no values. Or a failure that needs no additional information.

Loading

Require the data.maybe package, after installing it:

var Maybe = require('data.maybe')

This gives you back a data.maybe.Maybe object.

Why?

The Maybe(α) structure explicitly models the effects that are implicit in Nullable types, thus has none of the problems associated with using null or undefined, such as NullPointerException or TypeError: undefined is not a function.

Common uses of this structure includes modelling values that may or may not be present. For example, instead of having both a collection.has(a) and collection.get(a) operation, one may have the collection.get(a) operation return a Maybe(α) value. This avoids a problem of data incoherence (specially in asynchronous collections, where a value may be added between a call to .has() and .get()!).

Another common usage is for modelling functions that might fail to provide a value. E.g.: collection.find(predicate) can safely return a Maybe(α) instance, even if the collection allows nullable values.

Additional resources

Types and structures

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.