Maybe
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
.
Param | Type | Description |
---|---|---|
v | any |
Value |
Maybe.zero()
Creates a Nothing
.
Maybe.Just(v)
Creates a Just v
.
Param | Type | Description |
---|---|---|
v | any |
Value |
Maybe.Nothing()
Creates a Nothing
.
Maybe.fromNullable(v)
Creates a Just
if the value is not null
or undefined
; otherwise, creates a Nothing
.
Param | Type | Description |
---|---|---|
v | any |
Value |
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
.
Param | Type | Description |
---|---|---|
def | any |
Default value |
v | any |
Value |
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.
Param | Type | Description |
---|---|---|
ar | Array.<any> |
Array of Maybe values |
Maybe.isNothing(v)
A static method that returns true
if the passed Maybe
is a Nothing
.
Param | Type | Description |
---|---|---|
v | any |
Maybe |
Maybe.isJust(v)
A static method that returns true
if the passed Maybe
is a Just
.
Param | Type | Description |
---|---|---|
v | any |
Maybe |
Maybe.equals(n)
Returns true
if the current and the passed elements are of the Maybe
type with the same value.
Param | Type | Description |
---|---|---|
n | any |
Any value of type Setoid |
Maybe.map(f)
Applies the passed function to the value of the current Maybe
if it is a Just
.
Param | Type | Description |
---|---|---|
f | function |
Function |
Maybe.chain(f)
Chains together many computations that return a Maybe
type.
Param | Type | Description |
---|---|---|
f | function |
Function 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
.
Param | Type | Description |
---|---|---|
v | any |
Maybe |
Maybe.ap(j)
Applies the function inside the passed Maybe
to the current Maybe
if it is a Just
.
Param | Type | Description |
---|---|---|
j | any |
Maybe with a function |
Maybe.getValue()
Gets the value within the Maybe
.