Skip to content

Commit 95ec318

Browse files
jmagaramzth
authored andcommitted
Result.mapError
remove extra space before code comment shorter docs, add Examples header change log
1 parent ad03448 commit 95ec318

File tree

6 files changed

+90
-0
lines changed

6 files changed

+90
-0
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Next version
44

5+
### API changes
6+
7+
- Add `Result.mapError` https://github.com/rescript-association/rescript-core/pull/98
8+
59
## 0.4.0
610

711
### API changes

src/Core__Result.mjs

+12
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,17 @@ function forEach(r, f) {
107107

108108
}
109109

110+
function mapError(r, f) {
111+
if (r.TAG === /* Ok */0) {
112+
return r;
113+
} else {
114+
return {
115+
TAG: /* Error */1,
116+
_0: Curry._1(f, r._0)
117+
};
118+
}
119+
}
120+
110121
export {
111122
getExn ,
112123
mapWithDefault ,
@@ -118,5 +129,6 @@ export {
118129
equal ,
119130
compare ,
120131
forEach ,
132+
mapError ,
121133
}
122134
/* No side effect */

src/Core__Result.res

+12
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,15 @@ let forEach = (r, f) =>
9393
| Ok(ok) => f(ok)
9494
| Error(_) => ()
9595
}
96+
97+
// If the source result is Ok, should we return that instance, or
98+
// create it again? In this implementation I'm returning that specific
99+
// instance. However this is not consistent with the implementation for
100+
// other functions like mapU and flatMapU, which recreate the result.
101+
// This is more efficient. I'm not sure why the other implementations
102+
// return a new instance.
103+
let mapError = (r, f) =>
104+
switch r {
105+
| Ok(_) as ok => ok
106+
| Error(e) => Error(f(e))
107+
}

src/Core__Result.resi

+13
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,16 @@ Result.forEach(Error("x"), Console.log) // Does nothing, returns ()
209209
```
210210
*/
211211
let forEach: (t<'a, 'b>, 'a => unit) => unit
212+
213+
/**
214+
`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.
215+
216+
## Examples
217+
218+
```rescript
219+
let format = n => `Error code: ${n->Int.toString}`
220+
mapError(Error(14), format) // Error("Error code: 14")
221+
mapError(Ok("abc"), format) // Ok("abc")
222+
```
223+
*/
224+
let mapError: (result<'a, 'b>, 'b => 'c) => result<'a, 'c>

test/ResultTests.mjs

+36
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,42 @@ function forEachIfErrorDoNotCallFunction(param) {
5252

5353
forEachIfErrorDoNotCallFunction(undefined);
5454

55+
Test.run([
56+
[
57+
"ResultTests.res",
58+
27,
59+
20,
60+
48
61+
],
62+
"mapError: if ok, return it"
63+
], Core__Result.mapError({
64+
TAG: /* Ok */0,
65+
_0: 5
66+
}, (function (i) {
67+
return Math.imul(i, 3);
68+
})), eq, {
69+
TAG: /* Ok */0,
70+
_0: 5
71+
});
72+
73+
Test.run([
74+
[
75+
"ResultTests.res",
76+
30,
77+
13,
78+
42
79+
],
80+
"mapError: if error, apply f"
81+
], Core__Result.mapError({
82+
TAG: /* Error */1,
83+
_0: 5
84+
}, (function (i) {
85+
return Math.imul(i, 3);
86+
})), eq, {
87+
TAG: /* Error */1,
88+
_0: 15
89+
});
90+
5591
export {
5692
eq ,
5793
forEachIfOkCallFunction ,

test/ResultTests.res

+13
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,16 @@ let forEachIfErrorDoNotCallFunction = () => {
1919
Test.run(__POS_OF__("forEach: if error, do not call function"), called.contents, eq, [])
2020
}
2121
forEachIfErrorDoNotCallFunction()
22+
23+
// ========
24+
// mapError
25+
// ========
26+
27+
Test.run(__POS_OF__("mapError: if ok, return it"), Ok(5)->Result.mapError(i => i * 3), eq, Ok(5))
28+
29+
Test.run(
30+
__POS_OF__("mapError: if error, apply f"),
31+
Error(5)->Result.mapError(i => i * 3),
32+
eq,
33+
Error(15),
34+
)

0 commit comments

Comments
 (0)