Module: maybe/func

Members

(static, constant) Nothing

Nothing is the absence of a value, the opposite of Just for a Maybe object. Since all nothings are the same nothing, there is only one Nothing object. (c.f. Wallace Stevens, "The Snow Man").
Haskell> Nothing :: Maybe a

Source:

Methods

(static) catMaybes(xs) → {List}

Take a List of Maybe values and return a List of all the Just values.
Haskell> catMaybes :: [Maybe a] -> [a]

Parameters:
Name Type Description
xs List

A List

Source:
Returns:

A List of the Just values from xs

Type
List
Example
const lst = list(just(1), just(2), just(null), just(3), Nothing, just(), just(4), just(5));
catMaybes(lst); // => [1:2:3:4:5:[]]

(static) fromJust(m) → {*}

Extract the value from a Just. Throws an error if the Maybe is Nothing.
Haskell> fromJust :: Maybe a -> a

Parameters:
Name Type Description
m Maybe

A Maybe

Source:
Returns:

The value contained in the Just

Type
*

(static) fromMaybe(d, m) → {*}

Return the value contained in a Maybe if it is a Just or a default value if it is Nothing.
Haskell> fromMaybe :: a -> Maybe a -> a

Parameters:
Name Type Description
d *

The default value to return if m is Nothing

m Maybe

A Maybe

Source:
Returns:

The value contained in the Maybe or d if it is Nothing

Type
*

(static) isJust(m) → {boolean}

Determine whether an object is a Just.
Haskell> isJust :: Maybe a -> Bool

Parameters:
Name Type Description
m Maybe

Any object

Source:
Returns:

true if the object is a Just and false otherwise

Type
boolean

(static) isMaybe(a) → {boolean}

Determine whether an object is a Maybe.

Parameters:
Name Type Description
a *

Any object

Source:
Returns:

true if the object is a Maybe and false otherwise

Type
boolean

(static) isNothing(m) → {boolean}

Determine whether an object is Nothing.
Haskell> isNothing :: Maybe a -> Bool

Parameters:
Name Type Description
m Maybe

Any object

Source:
Returns:

true if the object is Nothing and false otherwise

Type
boolean

(static) just(a) → {Maybe}

Construct a Maybe value. Returns Nothing if the argument value is undefined, null, or NaN.
Haskell> Just :: a -> Maybe a

Parameters:
Name Type Description
a *

The value to wrap in a Maybe

Source:
Returns:

Just a or Nothing

Type
Maybe

(static) listToMaybe(xs) → {Maybe}

Take a List and return Just a where a is the first element of the List or Nothing if the List is empty.
Haskell> listToMaybe :: [a] -> Maybe a

Parameters:
Name Type Description
xs List

A List

Source:
Returns:

A Just containing the head of xs or Nothing if xs is an empty List

Type
Maybe
Example
const lst = list(1,2,3);
listToMaybe(lst);        // => Just 1
listToMaybe(emptyList);  // => Nothing

(static) mapMaybe(f, as) → {List}

Map a function f that returns a Maybe over a List. For each element of the list, if the result of applying the function is a Just, then the value it contains is included in the result list. If it is Nothing, then no element is added to the result list.
Haskell> mapMaybe :: (a -> Maybe b) -> [a] -> [b]

Parameters:
Name Type Description
f function

A function that returns a Maybe

as List

A List to map over

Source:
Returns:

A List of Just values returned from f mapped over as

Type
List
Example
const lst1 = list(1,2,3);
const lst2 = listRange(1, 25);
const f = x => even(x) ? just(x * 2) : Nothing;
mapMaybe(just, lst1);                           // => [1:2:3:[]]
mapMaybe(f, lst2);                              // => [4:8:12:16:20:24:28:32:36:40:44:48:[]]

(static) maybe(n, f, m) → {*}

Take a default value, a function, and a Maybe value. If the Maybe value is Nothing, return the default value. Otherwise, apply the function to the Just value and return the result.
Haskell> maybe :: b -> (a -> b) -> Maybe a -> b

Parameters:
Name Type Description
n *

The default value to return if m is Nothing

f function

The function to apply to the value inside m if it is a Just

m Maybe

A Maybe

Source:
Returns:

f applied to the Just value or n if the Maybe is Nothing

Type
*
Example
const m1 = just(100);
const m2 = just(null);
const f = x => x * 10;
maybe(0, f, m1);       // => 1000
maybe(0, f, m2);       // => 0

(static) maybeToList(m) → {List}

Take a Maybe and return a singleton List containing the Just value or an empty List if the Maybe is Nothing.
Haskell> maybeToList :: Maybe a -> [a]

Parameters:
Name Type Description
m Maybe

A Maybe

Source:
Returns:

A new List

Type
List
Example
const l = list(1,2,3);
const m = just(10);
maybeToList(m);        // => [10:[]]
maybeToList(Nothing);  // =>  [[]]