API / Core / Result

Result

t

RESCRIPT
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

RESCRIPT
let getExn: t<'a, 'b> => 'a

getExn(res): when res is Ok(n), returns n when res is Error(m), raise an exception

RES
Result.getExn(Result.Ok(42)) == 42 Result.getExn(Result.Error("Invalid data")) /* raises exception */

mapOr

RESCRIPT
let mapOr: (t<'a, 'c>, 'b, 'a => 'b) => 'b

mapOr(res, default, f): When res is Ok(n), returns f(n), otherwise default.

RES
let 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

RESCRIPT
let mapWithDefault: (t<'a, 'c>, 'b, 'a => 'b) => 'b

map

RESCRIPT
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.

RES
let f = (x) => sqrt(Int.toFloat(x)) Result.map(Ok(64), f) == Ok(8.0) Result.map(Error("Invalid data"), f) == Error("Invalid data")

flatMap

RESCRIPT
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.

RES
let 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

RESCRIPT
let getOr: (t<'a, 'b>, 'a) => 'a

getOr(res, defaultValue): If res is Ok(n), returns n, otherwise default

RES
Result.getOr(Ok(42), 0) == 42 Result.getOr(Error("Invalid Data"), 0) == 0

getWithDefault

Deprecated

Use getOr instead

RESCRIPT
let getWithDefault: (t<'a, 'b>, 'a) => 'a

isOk

RESCRIPT
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

RESCRIPT
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

RESCRIPT
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

RES
let 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

RESCRIPT
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)

RES
let 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

RESCRIPT
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

RESCRIPT
Result.forEach(Ok(3), Console.log) // Logs "3", returns () Result.forEach(Error("x"), Console.log) // Does nothing, returns ()

mapError

RESCRIPT
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

RESCRIPT
let format = n => `Error code: ${n->Int.toString}` mapError(Error(14), format) // Error("Error code: 14") mapError(Ok("abc"), format) // Ok("abc")