Some useful functions for dealing with Some
and None
.
Warning! You probably don't need this if you're using a version of BuckleScript that includes Belt and the Option module. See more information in this PR: rescript-lang/rescript-compiler#2622
Install bs-option
using yarn:
yarn add bs-option
Then add bs-option
to bs-dependencies
in bsconfig.json
.
let someValue = Some(42);
let fourtyTwo = Option.unwrap(someValue);
Option.isSome(o)
- returnstrue
ifo
isSome(a)
Option.isNone(o)
- returnstrue
ifo
isNone
Option.unwrap(o)
- returnsa
inSome(a)
or raisesOption.UnexpectedNone
Option.expect(o, message)
- returnsa
inSome(a)
or raisesOption.ExpectedSome(message)
Option.map(o, f)
- returnsSome(f(a))
forSome(a)
, orNone
ifo
isNone
Option.unwrapOr(o, default)
- same asunwrap
but returnsdefault
ifo
isNone
Option.unwrapOrElse(o, f)
- same asunwrap
but returns the result of the application off
ifo
isNone
Option.mapOr(o, default)
- same asmap
but returnsSome(default)
ifo
isNone
Option.mapOrElse(o, f)
- same asmap
but returnsSome(f())
ifo
isNone