maybe.md

December 15, 2024 ยท View on GitHub

Maybe

Maybe is a structure for values that may not be present or for situations that may fail. A Maybe can help in dealing with optional values, arguments, records with optional fields, etc.

Implements: Alt, Monad, Semigroup, Setoid

Maybe.of(v)

Creates a Just v.

ParamTypeDescription
vanyValue

Maybe.zero()

Creates a Nothing.

Maybe.Just(v)

Creates a Just v.

ParamTypeDescription
vanyValue

Maybe.Nothing()

Creates a Nothing.

Maybe.fromNullable(v)

Creates a Just if the value is not null or undefined; otherwise, creates a Nothing.

ParamTypeDescription
vanyValue

Maybe.withDefault(def, v)

Creates a Just if the value v is not null or undefined; otherwise, creates a Just with the default value def.

ParamTypeDescription
defanyDefault value
vanyValue

Maybe.catMaybes(ar)

A static method that takes an array of Maybe values and returns an array of the values of all the Just elements in the passed array.

ParamTypeDescription
arArray.<any>Array of Maybe values

Maybe.isNothing(v)

A static method that returns true if the passed Maybe is a Nothing.

ParamTypeDescription
vanyMaybe

Maybe.isJust(v)

A static method that returns true if the passed Maybe is a Just.

ParamTypeDescription
vanyMaybe

Maybe.equals(n)

Returns true if the current and the passed elements are of the Maybe type with the same value.

ParamTypeDescription
nanyAny value of type Setoid

Maybe.map(f)

Applies the passed function to the value of the current Maybe if it is a Just.

ParamTypeDescription
ffunctionFunction

Maybe.chain(f)

Chains together many computations that return a Maybe type.

ParamTypeDescription
ffunctionFunction that returns another Maybe

Maybe.isNothing()

Returns true if the current Maybe is a Nothing.

Maybe.isJust()

Returns true if the current Maybe is a Just.

Maybe.alt(v)

An instance method that returns the current Maybe if it is a Just; otherwise, returns the passed Maybe.

ParamTypeDescription
vanyMaybe

Maybe.ap(j)

Applies the function inside the passed Maybe to the current Maybe if it is a Just.

ParamTypeDescription
janyMaybe with a function

Maybe.getValue()

Gets the value within the Maybe.