Maybe

data. Maybe

new Maybe()

The Maybe union type and its subtypes Some and None. Usually they are used whenever an operation might result in a null value

Version:
  • 3.0.0
Source:
Example
const {Maybe} = require('futils').data;
const {Some, None} = Maybe;

Maybe.Some(1); // -> Some(1)
Some(1);       // -> Some(1)

Maybe.None();  // -> None
None();        // -> None

Maybe.Some(1).value; // -> 1
Maybe.None().value;  // -> null

Extends

Methods

(static) empty() → {None}

Monoid implementation for Maybe. Returns a Maybe.None

Source:
Returns:
Type:
None

A Maybe.None

Example
const {Maybe} = require('futils').data;

Maybe.empty(); // -> None

(static) from(a) → {Some|None}

Lifts a value into a Maybe. Similiar to Maybe.of, but if the value is either null or undefined it returns Maybe.None

Source:
Parameters:
Name Type Description
a any

The value to lift

Returns:
Type:
Some | None

Maybe.Some if the value is not null or undefined, Maybe.None otherwise

Example
const {Maybe} = require('futils').data;

Maybe.from(1); // -> Some(1)
Maybe.from(null); // -> None

(static) fromEither(a) → {Some|None}

A natural transformation from a Either.Left or Either.Right into a Maybe

Source:
Parameters:
Name Type Description
a Left | Right

The Either to transform

Returns:
Type:
Some | None

Maybe.Some if given an Either.Right, Maybe.None otherwise

Example
const {Maybe, Either} = require('futils/either');

const l = Either.Left('a left');
const r = Either.Right('a right');

Maybe.fromEither(r); // -> Some('a right')
Maybe.fromEither(l); // -> None

(static) fromId(a) → {Some|None}

A natural transformation from an Id into a Maybe

Source:
Parameters:
Name Type Description
a Id

The Id to transform

Returns:
Type:
Some | None

Maybe.Some if the Id holds a value different from null or undefined

Example
const {Maybe, Id} = require('futils/identity');

const some = Id('a value');
const none = Id(null);

Maybe.fromId(some); // -> Some('a value')
Maybe.fromId(none); // -> None

(static) fromList(a) → {Some|None}

A natural transformation from a List into a Maybe. Please note that this transformation looses data, because only the first element of the list is taken. If the first element is null or undefined, a Maybe.None is returned

Source:
Parameters:
Name Type Description
a List

The List to transform

Returns:
Type:
Some | None

Maybe.Some if the first element is not null or undefined

Example
const {Maybe, List} = require('futils').data;

const ls = List.of(2).cons(1);
const ks = List.Nil();

Maybe.fromList(ls); // -> Some(1)
Maybe.fromList(ks); // -> None

(static) of(a) → {Some}

Lifts a value into a Maybe.Some

Source:
Parameters:
Name Type Description
a any

The value to lift

Returns:
Type:
Some

The value wrapped in a Maybe.Some

Example
const {Maybe} = require('futils').data;

Maybe.of(1); // -> Some(1)

alt(a) → {Some|None}

Alt implementation, allows to swap a Maybe.None

Source:
Parameters:
Name Type Description
a Some | None

The alternative Maybe

Returns:
Type:
Some | None

Choosen alternative

Example
const {Maybe} = require('futils').data;

const some = Maybe.Some(1);
const none = Maybe.None();

some.alt(Maybe.Some(2)); // -> Some(1)
some.alt(Maybe.None());  // -> Some(1)
none.alt(Maybe.Some(2)); // -> Some(2)
none.alt(Maybe.None());  // -> None

ap(a) → {Some|None}

Applies a function in a Maybe.Some to a value in another Maybe.Some

Source:
Parameters:
Name Type Description
a Some | None

The Maybe that holds the value

Returns:
Type:
Some | None

Maybe which contains the result of applying the function

Example
const {Maybe} = require('futils').data;

const some = Maybe.Some(1);
const none = Maybe.None();

const mInc = Maybe.Some((n) => n + 1);

mInc.ap(some); // -> Some(2)
mInc.ap(none); // -> None

biMap(f, g) → {Some|None}

Bifunctor interface, maps either of two functions over the value inside a Maybe

Source:
Parameters:
Name Type Description
f function

Function to map if the value is a Maybe.None

g function

Function to map if the value is a Maybe.Some

Returns:
Type:
Some | None

Maybe with the result of applying either of the functions

Example
const {Maybe} = require('futils').data;

const some = Maybe.Some('a');
const none = Maybe.None();

const upperCase = (v) => v.toUpperCase();
const defaultChar = () => 'X';

some.biMap(defaultChar, upperCase); // -> Some('A')
none.biMap(defaultChar, upperCase); // -> Some('X')

concat(a) → {Some|None}

Concatenates a Maybe.Some with another. concatenation with Maybe.None will result in Maybe.None. Please note, that the inner values have to be part of a Semigroup as well for concatenation to succeed

Source:
Parameters:
Name Type Description
a Some | None

The Maybe instance to concatenate with

Returns:
Type:
Some | None

A new Maybe

Example
const {Maybe} = require('futils').data;

const some = Maybe.Some('a');
const none = Maybe.None();

some.concat(Maybe.Some('b')); // -> Some('ab')
some.concat(Maybe.None());    // -> None
none.concat(Maybe.Some('b')); // -> None
none.concat(Maybe.None());    // -> None

extend(f) → {None|Some}

If given a function that takes a Maybe and returns a value, returns a Maybe

Source:
Parameters:
Name Type Description
f function

A function taking a Maybe.Some or Maybe.None

Returns:
Type:
None | Some

A new Maybe

Example
const {Maybe} = require('futils').data;

const some = Maybe.Some('a some');
const none = Maybe.None();

some.extend(({value}) => /some/.test(value)); // -> Some(true)
none.extend(({value}) => /some/.test(value)); // -> None

extract() → {any|null}

Extracts the value from a Maybe.Some or Maybe.None. For Maybe.None returns null

Source:
Returns:
Type:
any | null

The value

Example
const {Maybe} = require('futils').data;

Maybe.Some(1).extract(); // -> 1
Maybe.None().extract();  // -> null

flat() → {Some|None}

Flattens a nested Maybe.Some one level

Source:
Returns:
Type:
Some | None

A flat Maybe.Some

Example
const {Maybe} = require('futils').data;

const some = Maybe.Some(Maybe.Some(1));
const none = Maybe.None();

some.flat(); // -> Some(1)
none.flat(); // -> None

flatMap(f) → {Some|None}

Maps a Maybe returning function over a Maybe.Some and flattens the result

Source:
Parameters:
Name Type Description
f function

A Maybe returning function to map

Returns:
Type:
Some | None

A new Maybe

Example
const {Maybe} = require('futils').data;

const some = Maybe.Some(2);
const none = Maybe.None();

const even = (n) => n % 2 === 0 ? Maybe.Some(n) : Maybe.None()

some.flatMap(even); // -> Some(2)
none.flatMap(even); // -> None

isSome() → {Boolean}

Test if the instance is a Maybe.Some or a Maybe.None

Source:
Returns:
Type:
Boolean

True if called on a Maybe.Some

Example
const {Maybe} = require('futils').data;

const some = Maybe.Some(1);
const none = Maybe.None();

some.isSome(); // -> true
none.isSome(); // -> false

map(f) → {Some|None}

Maps a function over the inner value and wraps the result in a new Maybe. Does not map the function over a Maybe.None

Source:
Parameters:
Name Type Description
f function

The function to map

Returns:
Type:
Some | None

A new Maybe.Some or the instance for Maybe.None

Example
const {Maybe} = require('futils').data;

const some = Maybe.Some('a');
const none = Maybe.None();

const upperCase = (v) => v.toUpperCase();

some.map(upperCase); // -> Some('A')
none.map(upperCase); // -> None

reduce(f, x) → {any}

Works much like the Array.reduce method. If given a function and an initial value, returns the initial value for a Maybe.None and calls the function with the initial value and the current value of a Maybe.Some

Source:
Parameters:
Name Type Description
f function

The function to reduce with

x any

The seed value to reduce into

Returns:
Type:
any

Either the seed value or whatever the reducer function returned

Example
const {Maybe} = require('futils').data;

const some = Maybe.Some('world');
const none = Maybe.None();

const reducer = (a, b) => a.concat(b);

some.reduce(reducer, 'hello'); // -> 'helloworld'
none.reduce(reducer, 'hello'); // -> 'hello'

sequence(A) → {Applicative|Array}

Sequences a Maybe into another Applicative type

Source:
Parameters:
Name Type Description
A Applicative | Array

A constructor with of and ap methods

Returns:
Type:
Applicative | Array

A Maybe wrapped in the Applicative

Example
const {Maybe} = require('futils').data;

const some = Maybe.Some([1]);
const none = Maybe.None();

some.sequence(Array); // -> [Some(1)]
none.sequence(Array); // -> [None]

traverse(f, A) → {Applicative|Array}

Takes a function with signature (Applicable f) => a -> f a and an Applicative constructor and traverses the Maybe into the Applicative

Source:
Parameters:
Name Type Description
f function

Function to traverse with

A Applicative | Array

A constructor with of and ap methods

Returns:
Type:
Applicative | Array

A Maybe wrapped in the Applicative

Example
const {Maybe} = require('futils').data;

const some = Maybe.Some(1);
const none = Maybe.None();

const fn = (n) => [n];

some.traverse(fn, Array); // -> [Some(1)]
none.traverse(fn, Array); // -> [None]