From 790a5af001ed64c05fc21269220936a8cb052ce5 Mon Sep 17 00:00:00 2001 From: jmagaram Date: Mon, 20 Mar 2023 11:05:33 -0700 Subject: [PATCH 1/2] Result.forEach --- src/Core__Result.mjs | 8 ++++++ src/Core__Result.res | 6 +++++ src/Core__Result.resi | 12 +++++++++ test/ResultTests.mjs | 60 +++++++++++++++++++++++++++++++++++++++++++ test/ResultTests.res | 21 +++++++++++++++ test/TestSuite.mjs | 13 +++++++--- test/TestSuite.res | 1 + 7 files changed, 118 insertions(+), 3 deletions(-) create mode 100644 test/ResultTests.mjs create mode 100644 test/ResultTests.res diff --git a/src/Core__Result.mjs b/src/Core__Result.mjs index 832ab968..fd9e5ffb 100644 --- a/src/Core__Result.mjs +++ b/src/Core__Result.mjs @@ -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 , @@ -112,5 +119,6 @@ export { isError , eq , cmp , + forEach , } /* No side effect */ diff --git a/src/Core__Result.res b/src/Core__Result.res index 51032a64..423ae5a1 100644 --- a/src/Core__Result.res +++ b/src/Core__Result.res @@ -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(_) => () + } diff --git a/src/Core__Result.resi b/src/Core__Result.resi index d1e6636d..3f03b7bd 100644 --- a/src/Core__Result.resi +++ b/src/Core__Result.resi @@ -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 diff --git a/test/ResultTests.mjs b/test/ResultTests.mjs new file mode 100644 index 00000000..837ad952 --- /dev/null +++ b/test/ResultTests.mjs @@ -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 */ diff --git a/test/ResultTests.res b/test/ResultTests.res new file mode 100644 index 00000000..0dbda319 --- /dev/null +++ b/test/ResultTests.res @@ -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() diff --git a/test/TestSuite.mjs b/test/TestSuite.mjs index c968bd8f..2085495e 100644 --- a/test/TestSuite.mjs +++ b/test/TestSuite.mjs @@ -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; @@ -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 , @@ -41,7 +46,9 @@ export { Catching , Concurrently , panicTest , - eq , $$catch , + eq , + forEachIfOkCallFunction , + forEachIfErrorDoNotCallFunction , } /* IntTests Not a pure module */ diff --git a/test/TestSuite.res b/test/TestSuite.res index 6277bf57..56266ed3 100644 --- a/test/TestSuite.res +++ b/test/TestSuite.res @@ -3,3 +3,4 @@ include PromiseTest include ErrorTests include ArrayTests include IntTests +include ResultTests From 9544a450baa950db7d6076b46efb03abf762a3d7 Mon Sep 17 00:00:00 2001 From: jmagaram Date: Tue, 21 Mar 2023 14:04:28 -0700 Subject: [PATCH 2/2] Changelog - Result.forEach --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 41f580aa..96d70a5b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## main +### API changes + +- Add `Result.forEach` https://github.com/rescript-association/rescript-core/pull/116 + ## 0.2.0 ### API changes