Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 8 additions & 0 deletions src/Core__Result.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ function cmp(a, b, f) {
}
}

function forEach(r, f) {
if (r.TAG === /* Ok */0) {
return Curry._1(f, r._0);
}

}

export {
getExn ,
mapWithDefault ,
Expand All @@ -112,5 +119,6 @@ export {
isError ,
eq ,
cmp ,
forEach ,
}
/* No side effect */
6 changes: 6 additions & 0 deletions src/Core__Result.res
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,9 @@ let cmpU = (a, b, f) =>
}

let cmp = (a, b, f) => cmpU(a, b, (. x, y) => f(x, y))

let forEach = (r, f) =>
switch r {
| Ok(ok) => f(ok)
| Error(_) => ()
}
12 changes: 12 additions & 0 deletions src/Core__Result.resi
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,15 @@ let eq: (t<'a, 'c>, t<'b, 'd>, ('a, 'b) => bool) => bool
```
*/
let cmp: (t<'a, 'c>, t<'b, 'd>, ('a, 'b) => int) => int

/**
`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 ()
```
*/
let forEach: (t<'a, 'b>, 'a => unit) => unit
60 changes: 60 additions & 0 deletions test/ResultTests.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Generated by ReScript, PLEASE EDIT WITH CARE

import * as Test from "./Test.mjs";
import * as Caml_obj from "rescript/lib/es6/caml_obj.js";
import * as Core__Result from "../src/Core__Result.mjs";

var eq = Caml_obj.equal;

function forEachIfOkCallFunction(param) {
var called = {
contents: []
};
Core__Result.forEach({
TAG: /* Ok */0,
_0: 3
}, (function (i) {
called.contents.push(i);
}));
Test.run([
[
"ResultTests.res",
12,
22,
72
],
"forEach: if ok, call function with ok value once"
], called.contents, eq, [3]);
}

forEachIfOkCallFunction(undefined);

function forEachIfErrorDoNotCallFunction(param) {
var called = {
contents: []
};
Core__Result.forEach({
TAG: /* Error */1,
_0: 3
}, (function (i) {
called.contents.push(i);
}));
Test.run([
[
"ResultTests.res",
19,
22,
63
],
"forEach: if error, do not call function"
], called.contents, eq, []);
}

forEachIfErrorDoNotCallFunction(undefined);

export {
eq ,
forEachIfOkCallFunction ,
forEachIfErrorDoNotCallFunction ,
}
/* Not a pure module */
21 changes: 21 additions & 0 deletions test/ResultTests.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
open RescriptCore

let eq = (a, b) => a == b

// =======
// forEach
// =======

let forEachIfOkCallFunction = () => {
let called = ref([])
Ok(3)->Result.forEach(i => called.contents->Array.push(i))
Test.run(__POS_OF__("forEach: if ok, call function with ok value once"), called.contents, eq, [3])
}
forEachIfOkCallFunction()

let forEachIfErrorDoNotCallFunction = () => {
let called = ref([])
Error(3)->Result.forEach(i => called.contents->Array.push(i))
Test.run(__POS_OF__("forEach: if error, do not call function"), called.contents, eq, [])
}
forEachIfErrorDoNotCallFunction()
13 changes: 10 additions & 3 deletions test/TestSuite.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as TestTests from "./TestTests.mjs";
import * as ArrayTests from "./ArrayTests.mjs";
import * as ErrorTests from "./ErrorTests.mjs";
import * as PromiseTest from "./PromiseTest.mjs";
import * as ResultTests from "./ResultTests.mjs";

var bign = TestTests.bign;

Expand All @@ -26,10 +27,14 @@ var Concurrently = PromiseTest.Concurrently;

var panicTest = ErrorTests.panicTest;

var eq = IntTests.eq;

var $$catch = IntTests.$$catch;

var eq = ResultTests.eq;

var forEachIfOkCallFunction = ResultTests.forEachIfOkCallFunction;

var forEachIfErrorDoNotCallFunction = ResultTests.forEachIfErrorDoNotCallFunction;

export {
bign ,
TestError ,
Expand All @@ -41,7 +46,9 @@ export {
Catching ,
Concurrently ,
panicTest ,
eq ,
$$catch ,
eq ,
forEachIfOkCallFunction ,
forEachIfErrorDoNotCallFunction ,
}
/* IntTests Not a pure module */
1 change: 1 addition & 0 deletions test/TestSuite.res
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ include PromiseTest
include ErrorTests
include ArrayTests
include IntTests
include ResultTests