Map over a Maybe instance and get out the value if maybe is a Just, or return a default value if maybe is a Nothing.
Maybe
maybe
Just
Nothing
const length = (s: string) => s.length;const justAString = Maybe.just('string');const theStringLength = mapOr(0, length, justAString);console.log(theStringLength); // 6const notAString = Maybe.nothing<string>();const notAStringLength = mapOr(0, length, notAString)console.log(notAStringLength); // 0 Copy
const length = (s: string) => s.length;const justAString = Maybe.just('string');const theStringLength = mapOr(0, length, justAString);console.log(theStringLength); // 6const notAString = Maybe.nothing<string>();const notAStringLength = mapOr(0, length, notAString)console.log(notAStringLength); // 0
The default value to use if maybe is Nothing
The function to apply the value to if Maybe is Just
The Maybe instance to map over.
T The type of the wrapped value.
U The type of the wrapped value of the returned Maybe.
Map over a
Maybe
instance and get out the value ifmaybe
is aJust
, or return a default value ifmaybe
is aNothing
.Examples