Skip to content

Commit 3b84757

Browse files
authored
Result.forEach with tests and docs (#116)
* Result.forEach * Changelog - Result.forEach
1 parent 49537f2 commit 3b84757

8 files changed

+122
-3
lines changed

CHANGELOG.md

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

33
## main
44

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

711
### API changes

src/Core__Result.mjs

+8
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,13 @@ function cmp(a, b, f) {
102102
}
103103
}
104104

105+
function forEach(r, f) {
106+
if (r.TAG === /* Ok */0) {
107+
return Curry._1(f, r._0);
108+
}
109+
110+
}
111+
105112
export {
106113
getExn ,
107114
mapWithDefault ,
@@ -112,5 +119,6 @@ export {
112119
isError ,
113120
eq ,
114121
cmp ,
122+
forEach ,
115123
}
116124
/* No side effect */

src/Core__Result.res

+6
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,9 @@ let cmpU = (a, b, f) =>
9191
}
9292

9393
let cmp = (a, b, f) => cmpU(a, b, (. x, y) => f(x, y))
94+
95+
let forEach = (r, f) =>
96+
switch r {
97+
| Ok(ok) => f(ok)
98+
| Error(_) => ()
99+
}

src/Core__Result.resi

+12
Original file line numberDiff line numberDiff line change
@@ -197,3 +197,15 @@ 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+
`forEach(res, f)` runs the provided function `f` on the `Ok` value. If `res` is `Error`, nothing happens.
203+
204+
## Examples
205+
206+
```rescript
207+
Result.forEach(Ok(3), Console.log) // Logs "3", returns ()
208+
Result.forEach(Error("x"), Console.log) // Does nothing, returns ()
209+
```
210+
*/
211+
let forEach: (t<'a, 'b>, 'a => unit) => unit

test/ResultTests.mjs

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
function forEachIfOkCallFunction(param) {
10+
var called = {
11+
contents: []
12+
};
13+
Core__Result.forEach({
14+
TAG: /* Ok */0,
15+
_0: 3
16+
}, (function (i) {
17+
called.contents.push(i);
18+
}));
19+
Test.run([
20+
[
21+
"ResultTests.res",
22+
12,
23+
22,
24+
72
25+
],
26+
"forEach: if ok, call function with ok value once"
27+
], called.contents, eq, [3]);
28+
}
29+
30+
forEachIfOkCallFunction(undefined);
31+
32+
function forEachIfErrorDoNotCallFunction(param) {
33+
var called = {
34+
contents: []
35+
};
36+
Core__Result.forEach({
37+
TAG: /* Error */1,
38+
_0: 3
39+
}, (function (i) {
40+
called.contents.push(i);
41+
}));
42+
Test.run([
43+
[
44+
"ResultTests.res",
45+
19,
46+
22,
47+
63
48+
],
49+
"forEach: if error, do not call function"
50+
], called.contents, eq, []);
51+
}
52+
53+
forEachIfErrorDoNotCallFunction(undefined);
54+
55+
export {
56+
eq ,
57+
forEachIfOkCallFunction ,
58+
forEachIfErrorDoNotCallFunction ,
59+
}
60+
/* Not a pure module */

test/ResultTests.res

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
open RescriptCore
2+
3+
let eq = (a, b) => a == b
4+
5+
// =======
6+
// forEach
7+
// =======
8+
9+
let forEachIfOkCallFunction = () => {
10+
let called = ref([])
11+
Ok(3)->Result.forEach(i => called.contents->Array.push(i))
12+
Test.run(__POS_OF__("forEach: if ok, call function with ok value once"), called.contents, eq, [3])
13+
}
14+
forEachIfOkCallFunction()
15+
16+
let forEachIfErrorDoNotCallFunction = () => {
17+
let called = ref([])
18+
Error(3)->Result.forEach(i => called.contents->Array.push(i))
19+
Test.run(__POS_OF__("forEach: if error, do not call function"), called.contents, eq, [])
20+
}
21+
forEachIfErrorDoNotCallFunction()

test/TestSuite.mjs

+10-3
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,14 @@ 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+
34+
var forEachIfOkCallFunction = ResultTests.forEachIfOkCallFunction;
35+
36+
var forEachIfErrorDoNotCallFunction = ResultTests.forEachIfErrorDoNotCallFunction;
37+
3338
export {
3439
bign ,
3540
TestError ,
@@ -41,7 +46,9 @@ export {
4146
Catching ,
4247
Concurrently ,
4348
panicTest ,
44-
eq ,
4549
$$catch ,
50+
eq ,
51+
forEachIfOkCallFunction ,
52+
forEachIfErrorDoNotCallFunction ,
4653
}
4754
/* IntTests Not a pure module */

test/TestSuite.res

+1
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)