Pages
Standard Library

Search
Entities

# Maybe

## Functions

andThen
(
transform
:
Function(a, Maybe(b))
maybe
:
Maybe(a)
)
:
Maybe(b)

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(a))
)
:
Maybe(a)

Flattens a nested maybe.

``````(Maybe.just("A")
|> Maybe.just()
|> Maybe.flatten()) == Maybe.just("A")
``````
isJust
(
maybe
:
Maybe(a)
)
:
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(a)
)
:
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
:
a
)
:
Maybe(a)

Returns a maybe containing just the given value.

map
(
func
:
Function(a, b)
maybe
:
Maybe(a)
)
:
Maybe(b)

Maps the value of a maybe.

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

Returns nothing.

oneOf
(
array
:
Array(Maybe(a))
)
:
Maybe(a)

Returns the first maybe with value of the array or nothing if it's all nothing.

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

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

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

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

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

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

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