Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#### :boom: Breaking Change

- Removed "rescript legacy" subcommand in favor of separate "rescript-legacy" binary. https://github.com/rescript-lang/rescript/pull/7928
- Add comparison fn for Error in Result.equal and compare. https://github.com/rescript-lang/rescript/pull/7933

#### :eyeglasses: Spec Compliance

Expand Down
12 changes: 6 additions & 6 deletions packages/@rescript/runtime/Stdlib_Result.res
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,20 @@ let isError = x =>
| Error(_) => true
}

let equal = (a, b, f) =>
let equal = (a, b, eqOk, eqError) =>
switch (a, b) {
| (Ok(a), Ok(b)) => f(a, b)
| (Ok(a), Ok(b)) => eqOk(a, b)
| (Error(_), Ok(_))
| (Ok(_), Error(_)) => false
| (Error(_), Error(_)) => true
| (Error(a), Error(b)) => eqError(a, b)
}

let compare = (a, b, f) =>
let compare = (a, b, cmpOk, cmpError) =>
switch (a, b) {
| (Ok(a), Ok(b)) => f(a, b)
| (Ok(a), Ok(b)) => cmpOk(a, b)
| (Error(_), Ok(_)) => Stdlib_Ordering.less
| (Ok(_), Error(_)) => Stdlib_Ordering.greater
| (Error(_), Error(_)) => Stdlib_Ordering.equal
| (Error(a), Error(b)) => cmpError(a, b)
}

let forEach = (r, f) =>
Expand Down
43 changes: 24 additions & 19 deletions packages/@rescript/runtime/Stdlib_Result.resi
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,11 @@ let isOk: result<'a, 'b> => bool
let isError: result<'a, 'b> => 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
`equal(res1, res2, eqOk, eqError)`: Determine if two `Result` variables are equal with
respect to equality functions. If `res1` and `res2` are of the form `Ok(n)`
and `Ok(m)`, return the result of `eqOk(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
`Error(e)`, return the result of `eqError(e1, e2)`.

## Examples

Expand All @@ -191,28 +191,28 @@ let bad2 = Error("really invalid")

let mod10equal = (a, b) => mod(a, 10) === mod(b, 10)

Result.equal(good1, good2, mod10equal) == true
Result.equal(good1, good2, mod10equal, String.equal) == true

Result.equal(good1, bad1, mod10equal) == false
Result.equal(good1, bad1, mod10equal, String.equal) == false

Result.equal(bad2, good2, mod10equal) == false
Result.equal(bad2, good2, mod10equal, String.equal) == false

Result.equal(bad1, bad2, mod10equal) == true
Result.equal(bad1, bad2, mod10equal, String.equal) == false
```
*/
let equal: (result<'a, 'c>, result<'b, 'd>, ('a, 'b) => bool) => bool
let equal: (result<'a, 'c>, result<'b, 'd>, ('a, 'b) => bool, ('c, 'd) => bool) => bool

/**
`compare(res1, res2, f)`: Compare two `Result` variables with respect to a
`compare(res1, res2, cmpOk, cmpError)`: 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)`,
`cmpOk(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)
`res2` is of the form `Error(e)`, return 1. (something is greater than nothing) If
both `res1` and `res2` are of the form `Error(e)`, return cmpError(e1, e2).

## Examples

Expand All @@ -227,18 +227,23 @@ 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(39), Ok(57), mod10cmp, String.compare) == 1.

Result.compare(Ok(57), Ok(39), mod10cmp) == -1.
Result.compare(Ok(57), Ok(39), mod10cmp, String.compare) == -1.

Result.compare(Ok(39), Error("y"), mod10cmp) == 1.
Result.compare(Ok(39), Error("y"), mod10cmp, String.compare) == 1.

Result.compare(Error("x"), Ok(57), mod10cmp) == -1.
Result.compare(Error("x"), Ok(57), mod10cmp, String.compare) == -1.

Result.compare(Error("x"), Error("y"), mod10cmp) == 0.
Result.compare(Error("x"), Error("y"), mod10cmp, String.compare) == -1.
```
*/
let compare: (result<'a, 'c>, result<'b, 'd>, ('a, 'b) => Stdlib_Ordering.t) => Stdlib_Ordering.t
let compare: (
result<'a, 'c>,
result<'b, 'd>,
('a, 'b) => Stdlib_Ordering.t,
('c, 'd) => Stdlib_Ordering.t,
) => Stdlib_Ordering.t

/**
`forEach(res, f)` runs the provided function `f` on the `Ok` value. If `res` is `Error`, nothing happens.
Expand Down
14 changes: 8 additions & 6 deletions packages/@rescript/runtime/lib/es6/Stdlib_Result.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,29 +53,31 @@ function isError(x) {
return x.TAG !== "Ok";
}

function equal(a, b, f) {
function equal(a, b, eqOk, eqError) {
if (a.TAG === "Ok") {
if (b.TAG === "Ok") {
return f(a._0, b._0);
return eqOk(a._0, b._0);
} else {
return false;
}
} else if (b.TAG === "Ok") {
return false;
} else {
return b.TAG !== "Ok";
return eqError(a._0, b._0);
}
}

function compare(a, b, f) {
function compare(a, b, cmpOk, cmpError) {
if (a.TAG === "Ok") {
if (b.TAG === "Ok") {
return f(a._0, b._0);
return cmpOk(a._0, b._0);
} else {
return 1;
}
} else if (b.TAG === "Ok") {
return -1;
} else {
return 0;
return cmpError(a._0, b._0);
}
}

Expand Down
14 changes: 8 additions & 6 deletions packages/@rescript/runtime/lib/js/Stdlib_Result.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,29 +53,31 @@ function isError(x) {
return x.TAG !== "Ok";
}

function equal(a, b, f) {
function equal(a, b, eqOk, eqError) {
if (a.TAG === "Ok") {
if (b.TAG === "Ok") {
return f(a._0, b._0);
return eqOk(a._0, b._0);
} else {
return false;
}
} else if (b.TAG === "Ok") {
return false;
} else {
return b.TAG !== "Ok";
return eqError(a._0, b._0);
}
}

function compare(a, b, f) {
function compare(a, b, cmpOk, cmpError) {
if (a.TAG === "Ok") {
if (b.TAG === "Ok") {
return f(a._0, b._0);
return cmpOk(a._0, b._0);
} else {
return 1;
}
} else if (b.TAG === "Ok") {
return -1;
} else {
return 0;
return cmpError(a._0, b._0);
}
}

Expand Down
Loading