Map over a Maybe
instance and get out the value if maybe
is a
Just
, or use a function to construct a default value if maybe
is
Nothing
.
const length = (s: string) => s.length;
const getDefault = () => 0;
const justAString = Maybe.just('string');
const theStringLength = mapOrElse(getDefault, length, justAString);
console.log(theStringLength); // 6
const notAString = Maybe.nothing<string>();
const notAStringLength = mapOrElse(getDefault, length, notAString)
console.log(notAStringLength); // 0
Map over a Maybe
instance and get out the value if maybe
is a
Just
, or use a function to construct a default value if maybe
is
Nothing
.
const length = (s: string) => s.length;
const getDefault = () => 0;
const justAString = Maybe.just('string');
const theStringLength = mapOrElse(getDefault, length, justAString);
console.log(theStringLength); // 6
const notAString = Maybe.nothing<string>();
const notAStringLength = mapOrElse(getDefault, length, notAString)
console.log(notAStringLength); // 0
The function to apply if maybe
is Nothing
.
Map over a
Maybe
instance and get out the value ifmaybe
is aJust
, or use a function to construct a default value ifmaybe
isNothing
.Examples