Pages
core

Search
Entities

# Maybe

## Functions

andThen
(
maybe
:
Maybe(value)
transform
:
Function(value, Maybe(result))
)
:
Maybe(result)

Maps the value of a maybe with a possibility to discard it.

``````Maybe::Just(4)
|> Maybe.andThen((num : Number) : Maybe(String) {
if (num > 4) {
Maybe::Just(Number.toString(num))
} else {
Maybe::Nothing
}
})
``````
flatten
(
maybe
:
Maybe(Maybe(value))
)
:
Maybe(value)

Flattens a nested maybe.

``````(Maybe.just("A")
|> Maybe.just()
|> Maybe.flatten()) == Maybe.just("A")
``````
isJust
(
maybe
:
Maybe(value)
)
:
Bool

Returns whether or not the maybe is just a value or not.

`````` Maybe.isJust(Maybe.just("A")) == true
Maybe.isJust(Maybe.nothing()) == false
``````
isNothing
(
maybe
:
Maybe(value)
)
:
Bool

Returns whether or not the maybe is just nothing or not.

``````Maybe.isNothing(Maybe.just("A")) == false
Maybe.isNothing(Maybe.nothing("A")) == false
``````
just
(
value
:
value
)
:
Maybe(value)

Returns a maybe containing just the given value.

map
(
maybe
:
Maybe(value)
func
:
Function(value, result)
)
:
Maybe(result)

Maps the value of a maybe.

``````(Maybe.just(1)
|> Maybe.map((number : Number) : Number { number + 2 })) == 3
``````
nothing
:
Maybe(value)

Returns nothing.

oneOf
(
array
:
Array(Maybe(value))
)
:
Maybe(value)

Returns the first maybe with value of the array or nothing if all are nothing.

``````Maybe.oneOf([Maybe.just("A"), Maybe.nothing()]) == Maybe.just("A")
``````
toResult
(
maybe
:
Maybe(value)
error
:
error
)
:
Result(error, value)

Converts the maybe to a result using the given value as the error.

``````Maybe.toResult(Maybe.nothing(), "Error") == Result.error("Error")
Maybe.toResult(Maybe.just("A"), "Error") == Result.ok("A")
``````
withDefault
(
maybe
:
Maybe(value)
defaultValue
:
value
)
:
value

Returns the value of a maybe or the given value if it's nothing.

``````Maybe.withDefault(Maybe.nothing(), "A") == "A"
Maybe.withDefault(Maybe.just("B"), "A") == "B"
``````
withLazyDefault
(
maybe
:
Maybe(value)
func
:
Function(value)
)
:
value

Returns the value of a maybe, or calls the given func otherwise.

``````Maybe.withLazyDefault(Maybe.nothing(), () { "A" }) == "A"
Maybe.withLazyDefault(Maybe.just("B"), () { "A" }) == "B"
``````