bs-result
v2.2.0
Published
A Result type implementation for ReasonML
Downloads
11
Readme
bs-result
Monadic interface for Belt.Result
This implementation is inspired by JavaScript Fantasy Land and closely follows the Funfix.org Either implementation.
Installation
- Install the
bs-result
package.
yarn add bs-result
- Add
bs-result
to yourbsconfig.json
{
"dependencies": [
"bs-result"
]
}
Usage
Basic
let r =
Result.return(20)
|> Result.map(x => x + 20)
|> Result.flatMap(x => Result.return(x + 2))
|> Result.getOrElse(-1);
Js.log2("Result: ", r);
Integrations
Belt.Result
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
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();
})
Full API Docs
TODO
- [ ] Integration with reason-future.
- [ ] Comonad implementation - functions are mis-named.