Result
getExn
let getExn: result<'a, 'b> => 'aResult 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.
mapOr
let mapOr: (result<'a, 'c>, 'b, 'a => 'b) => 'bmapOr(res, default, f): When res is Ok(n), returns f(n), otherwise default.
Examples
RESCRIPTlet ok = Ok(42)
Result.mapOr(ok, 0, (x) => x / 2) == 21
let error = Error("Invalid data")
Result.mapOr(error, 0, (x) => x / 2) == 0
mapWithDefault
Deprecated
Use mapOr instead
let mapWithDefault: (result<'a, 'c>, 'b, 'a => 'b) => 'bmap
let map: (result<'a, 'c>, 'a => 'b) => result<'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.
Examples
RESCRIPTlet 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: (result<'a, 'c>, 'a => result<'b, 'c>) => result<'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.
Examples
RESCRIPTlet recip = (x) =>
if (x !== 0.0) {
Ok(1.0 /. x)
} else {
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: (result<'a, 'b>, 'a) => 'agetOr(res, defaultValue): If res is Ok(n), returns n, otherwise default
Examples
RESCRIPTResult.getOr(Ok(42), 0) == 42
Result.getOr(Error("Invalid Data"), 0) == 0
getWithDefault
Deprecated
Use getOr instead
let getWithDefault: (result<'a, 'b>, 'a) => 'aisOk
let isOk: result<'a, 'b> => boolisOk(res): Returns true if res is of the form Ok(n), false if it is the Error(e) variant.
isError
let isError: result<'a, 'b> => boolisError(res): Returns true if res is of the form Error(e), false if it is the Ok(n) variant.
equal
let equal: (result<'a, 'c>, result<'b, 'd>, ('a, 'b) => bool) => boolequal(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
Examples
RESCRIPTlet good1 = Ok(42)
let good2 = Ok(32)
let bad1 = Error("invalid")
let bad2 = 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: (
result<'a, 'c>,
result<'b, 'd>,
('a, 'b) => Core__Ordering.t,
) => Core__Ordering.tcompare(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)
Examples
RESCRIPTlet good1 = Ok(59)
let good2 = Ok(37)
let bad1 = Error("invalid")
let bad2 = Error("really invalid")
let mod10cmp = (a, b) => Int.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: (result<'a, 'b>, 'a => unit) => unitforEach(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>mapError(r, f) generates a new result by applying the function f to the Error value. If the source is Ok, return it as-is.
Examples
RESCRIPTlet format = n => `Error code: ${n->Int.toString}`
Result.mapError(Error(14), format) // Error("Error code: 14")
Result.mapError(Ok("abc"), format) // Ok("abc")