Result
t
type t<'a, 'b> = result<'a, 'b> = Ok('a) | Error('b)
Result types are really useful to describe the result of a certain operation
without relying on exceptions or option
types.
This module gives you useful utilities to create and combine Result
data.
getExn
let getExn: t<'a, 'b> => 'a
getExn(res)
: when res
is Ok(n)
, returns n
when res
is Error(m)
, raise an exception
RESResult.getExn(Result.Ok(42)) == 42
Result.getExn(Result.Error("Invalid data")) /* raises exception */
mapOr
let mapOr: (t<'a, 'c>, 'b, 'a => 'b) => 'b
mapOr(res, default, f)
: When res is Ok(n)
, returns f(n)
,
otherwise default
.
RESlet ok = Result.Ok(42)
Result.mapOr(ok, 0, (x) => x / 2) == 21
let error = Result.Error("Invalid data")
Result.mapOr(error, 0, (x) => x / 2) == 0
mapWithDefault
Deprecated
Use mapOr instead
let mapWithDefault: (t<'a, 'c>, 'b, 'a => 'b) => 'b
map
let map: (t<'a, 'c>, 'a => 'b) => t<'b, 'c>
map(res, f)
: When res is Ok(n)
, returns Ok(f(n))
. Otherwise returns res
unchanged. Function f
takes a value of the same type as n
and returns an
ordinary value.
RESlet f = (x) => sqrt(Int.toFloat(x))
Result.map(Ok(64), f) == Ok(8.0)
Result.map(Error("Invalid data"), f) == Error("Invalid data")
flatMap
let flatMap: (t<'a, 'c>, 'a => t<'b, 'c>) => t<'b, 'c>
flatMap(res, f)
: When res is Ok(n)
, returns f(n)
. Otherwise, returns res
unchanged. Function f
takes a value of the same type as n
and returns a
Result
.
RESlet recip = (x) =>
if (x !== 0.0) {
Result.Ok(1.0 /. x)
} else {
Result.Error("Divide by zero")
}
Result.flatMap(Ok(2.0), recip) == Ok(0.5)
Result.flatMap(Ok(0.0), recip) == Error("Divide by zero")
Result.flatMap(Error("Already bad"), recip) == Error("Already bad")
getOr
let getOr: (t<'a, 'b>, 'a) => 'a
getOr(res, defaultValue)
: If res
is Ok(n)
, returns n
,
otherwise default
RESResult.getOr(Ok(42), 0) == 42
Result.getOr(Error("Invalid Data"), 0) == 0
getWithDefault
Deprecated
Use getOr instead
let getWithDefault: (t<'a, 'b>, 'a) => 'a
isOk
let isOk: t<'a, 'b> => bool
isOk(res)
: Returns true
if res
is of the form Ok(n)
, false
if it is
the Error(e)
variant.
isError
let isError: t<'a, 'b> => bool
isError(res)
: Returns true
if res
is of the form Error(e)
, false
if
it is the Ok(n)
variant.
equal
let equal: (t<'a, 'c>, t<'b, 'd>, ('a, 'b) => bool) => bool
equal(res1, res2, f)
: Determine if two Result
variables are equal with
respect to an equality function. If res1
and res2
are of the form Ok(n)
and Ok(m)
, return the result of f(n, m)
. If one of res1
and res2
are of
the form Error(e)
, return false If both res1
and res2
are of the form
Error(e)
, return true
RESlet good1 = Result.Ok(42)
let good2 = Result.Ok(32)
let bad1 = Result.Error("invalid")
let bad2 = Result.Error("really invalid")
let mod10equal = (a, b) => mod(a, 10) === mod(b, 10)
Result.equal(good1, good2, mod10equal) == true
Result.equal(good1, bad1, mod10equal) == false
Result.equal(bad2, good2, mod10equal) == false
Result.equal(bad1, bad2, mod10equal) == true
compare
let compare: (
t<'a, 'c>,
t<'b, 'd>,
('a, 'b) => Core__Ordering.t,
) => Core__Ordering.t
compare(res1, res2, f)
: Compare two Result
variables with respect to a
comparison function. The comparison function returns -1. if the first variable
is "less than" the second, 0. if the two variables are equal, and 1. if the first
is "greater than" the second.
If res1
and res2
are of the form Ok(n)
and Ok(m)
, return the result of
f(n, m)
. If res1
is of the form Error(e)
and res2
of the form Ok(n)
,
return -1. (nothing is less than something) If res1
is of the form Ok(n)
and
res2
of the form Error(e)
, return 1. (something is greater than nothing) If
both res1
and res2
are of the form Error(e)
, return 0. (equal)
RESlet good1 = Result.Ok(59)
let good2 = Result.Ok(37)
let bad1 = Result.Error("invalid")
let bad2 = Result.Error("really invalid")
let mod10cmp = (a, b) => Pervasives.compare(mod(a, 10), mod(b, 10))
Result.compare(Ok(39), Ok(57), mod10cmp) == 1.
Result.compare(Ok(57), Ok(39), mod10cmp) == (-1.)
Result.compare(Ok(39), Error("y"), mod10cmp) == 1.
Result.compare(Error("x"), Ok(57), mod10cmp) == (-1.)
Result.compare(Error("x"), Error("y"), mod10cmp) == 0.
forEach
let forEach: (t<'a, 'b>, 'a => unit) => unit
forEach(res, f)
runs the provided function f
on the Ok
value. If res
is Error
, nothing happens.
Examples
RESCRIPTResult.forEach(Ok(3), Console.log) // Logs "3", returns ()
Result.forEach(Error("x"), Console.log) // Does nothing, returns ()
mapError
let mapError: (result<'a, 'b>, 'b => 'c) => result<'a, 'c>