Converts a Maybe
to an Result
. Nothing
s map to Error
s, Just
s map to
Ok
s.
forall a, b:
(Maybe a, b) => Result b a
Converts a Maybe
to an Result
. Nothing
s map to Error
s, Just
s map to
Ok
s.
Note that since Maybe
s don't hold a value for failures in the Nothing
tag,
you must provide one to this function.
const maybeToResult = require('folktale/conversions/maybe-to-result');
const { Error, Ok } = require('folktale/result');
const { Nothing, Just } = require('folktale/maybe');
maybeToResult(Nothing(), 2); // ==> Error(2)
maybeToResult(Just(1), 2); // ==> Ok(1)
(aMaybe, failureValue) =>
aMaybe.matchWith({
Nothing: () => Error(failureValue),
Just: ({ value }) => Ok(value)
})