Monadic interface for Belt.Result
This implementation is inspired by JavaScript Fantasy Land and closely follows the Funfix.org Either implementation.
- Install the
bs-result
package.
yarn add bs-result
- Add
bs-result
to yourbsconfig.json
{
"dependencies": [
"bs-result"
]
}
let r =
Result.return(20)
|> Result.map(x => x + 20)
|> Result.flatMap(x => Result.return(x + 2))
|> Result.getOrElse(-1);
Js.log2("Result: ", r);
let r =
Belt.Result.Ok(x => x + 10)
|> Result.ap(Result.return(20))
|> Belt.Result.map(_, x => x + 10)
|> Result.flatMap(x => Belt.Result.Ok(x + 2))
|> Belt.Result.getWithDefault(_, -1);
Js.log2("Result: ", r);
Js.Promise.resolve(20)
|> Js.Promise.then_(x => Result.Promise.return(x))
|> Result.Promise.flatMap(x => Result.return(x + 10))
|> Result.Promise.map(x => x + 10)
|> Result.Promise.andThen(x =>
x + 2 |> Result.return |> Js.Promise.resolve
)
|> Result.Promise.unsafeResolve
|> Js.Promise.then_(result => {
Js.log2("Result: ", result);
Js.Promise.resolve();
})
- Integration with reason-future.
- Comonad implementation - functions are mis-named.