Skip to content

Commit b5e268f

Browse files
committed
Result.mapError
1 parent 7c2f372 commit b5e268f

File tree

7 files changed

+107
-3
lines changed

7 files changed

+107
-3
lines changed

src/Core__Result.mjs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,17 @@ function cmp(a, b, f) {
102102
}
103103
}
104104

105+
function mapError(r, f) {
106+
if (r.TAG === /* Ok */0) {
107+
return r;
108+
} else {
109+
return {
110+
TAG: /* Error */1,
111+
_0: Curry._1(f, r._0)
112+
};
113+
}
114+
}
115+
105116
export {
106117
getExn ,
107118
mapWithDefault ,
@@ -112,5 +123,6 @@ export {
112123
isError ,
113124
eq ,
114125
cmp ,
126+
mapError ,
115127
}
116128
/* No side effect */

src/Core__Result.res

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,15 @@ let cmpU = (a, b, f) =>
9191
}
9292

9393
let cmp = (a, b, f) => cmpU(a, b, (. x, y) => f(x, y))
94+
95+
// If the source result is Ok, should we return that instance, or
96+
// create it again? In this implementation I'm returning that specific
97+
// instance. However this is not consistent with the implementation for
98+
// other functions like mapU and flatMapU, which recreate the result.
99+
// This is more efficient. I'm not sure why the other implementations
100+
// return a new instance.
101+
let mapError = (r, f) =>
102+
switch r {
103+
| Ok(_) as ok => ok
104+
| Error(e) => Error(f(e))
105+
}

src/Core__Result.resi

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,3 +197,17 @@ let eq: (t<'a, 'c>, t<'b, 'd>, ('a, 'b) => bool) => bool
197197
```
198198
*/
199199
let cmp: (t<'a, 'c>, t<'b, 'd>, ('a, 'b) => int) => int
200+
201+
/**
202+
`mapError(res, f)` - If the source is `Ok`, return it. Otherwise apply
203+
the provided mapping function to the `Error` value.
204+
205+
## Examples
206+
```rescript
207+
208+
let formatError = n => `Error code: ${n->Int.toString}`
209+
mapError(Error(14), formatError) // evaluates to Error("Error code: 14")
210+
mapError(Ok("abc"), formatError) // evaluates to Ok("abc")
211+
```
212+
*/
213+
let mapError: (result<'a, 'b>, 'b => 'c) => result<'a, 'c>

test/ResultTests.mjs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Generated by ReScript, PLEASE EDIT WITH CARE
2+
3+
import * as Test from "./Test.mjs";
4+
import * as Caml_obj from "rescript/lib/es6/caml_obj.js";
5+
import * as Core__Result from "../src/Core__Result.mjs";
6+
7+
var eq = Caml_obj.equal;
8+
9+
Test.run([
10+
[
11+
"ResultTests.res",
12+
9,
13+
20,
14+
48
15+
],
16+
"mapError: if ok, return it"
17+
], Core__Result.mapError({
18+
TAG: /* Ok */0,
19+
_0: 5
20+
}, (function (i) {
21+
return Math.imul(i, 3);
22+
})), eq, {
23+
TAG: /* Ok */0,
24+
_0: 5
25+
});
26+
27+
Test.run([
28+
[
29+
"ResultTests.res",
30+
12,
31+
13,
32+
42
33+
],
34+
"mapError: if error, apply f"
35+
], Core__Result.mapError({
36+
TAG: /* Error */1,
37+
_0: 5
38+
}, (function (i) {
39+
return Math.imul(i, 3);
40+
})), eq, {
41+
TAG: /* Error */1,
42+
_0: 15
43+
});
44+
45+
export {
46+
eq ,
47+
}
48+
/* Not a pure module */

test/ResultTests.res

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
open RescriptCore
2+
3+
let eq = (a, b) => a == b
4+
5+
// ========
6+
// mapError
7+
// ========
8+
9+
Test.run(__POS_OF__("mapError: if ok, return it"), Ok(5)->Result.mapError(i => i * 3), eq, Ok(5))
10+
11+
Test.run(
12+
__POS_OF__("mapError: if error, apply f"),
13+
Error(5)->Result.mapError(i => i * 3),
14+
eq,
15+
Error(15),
16+
)

test/TestSuite.mjs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as TestTests from "./TestTests.mjs";
55
import * as ArrayTests from "./ArrayTests.mjs";
66
import * as ErrorTests from "./ErrorTests.mjs";
77
import * as PromiseTest from "./PromiseTest.mjs";
8+
import * as ResultTests from "./ResultTests.mjs";
89

910
var bign = TestTests.bign;
1011

@@ -26,10 +27,10 @@ var Concurrently = PromiseTest.Concurrently;
2627

2728
var panicTest = ErrorTests.panicTest;
2829

29-
var eq = IntTests.eq;
30-
3130
var $$catch = IntTests.$$catch;
3231

32+
var eq = ResultTests.eq;
33+
3334
export {
3435
bign ,
3536
TestError ,
@@ -41,7 +42,7 @@ export {
4142
Catching ,
4243
Concurrently ,
4344
panicTest ,
44-
eq ,
4545
$$catch ,
46+
eq ,
4647
}
4748
/* IntTests Not a pure module */

test/TestSuite.res

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ include PromiseTest
33
include ErrorTests
44
include ArrayTests
55
include IntTests
6+
include ResultTests

0 commit comments

Comments
 (0)