diff --git a/lib/es6/Option.js b/lib/es6/Option.js index b965c0d7d4..98e8467077 100644 --- a/lib/es6/Option.js +++ b/lib/es6/Option.js @@ -99,19 +99,18 @@ function compare(a, b, cmp) { function all(options) { let acc = []; - let returnValue; + let hasNone = false; let index = 0; - while (returnValue === undefined && index < options.length) { + while (hasNone === false && index < options.length) { let value = options[index]; if (value !== undefined) { acc.push(Primitive_option.valFromOption(value)); index = index + 1 | 0; } else { - returnValue = Primitive_option.some(undefined); + hasNone = true; } }; - let match = returnValue; - if (match !== undefined) { + if (hasNone) { return; } else { return acc; diff --git a/lib/js/Option.js b/lib/js/Option.js index 3bf6857601..ffa98d5746 100644 --- a/lib/js/Option.js +++ b/lib/js/Option.js @@ -99,19 +99,18 @@ function compare(a, b, cmp) { function all(options) { let acc = []; - let returnValue; + let hasNone = false; let index = 0; - while (returnValue === undefined && index < options.length) { + while (hasNone === false && index < options.length) { let value = options[index]; if (value !== undefined) { acc.push(Primitive_option.valFromOption(value)); index = index + 1 | 0; } else { - returnValue = Primitive_option.some(undefined); + hasNone = true; } }; - let match = returnValue; - if (match !== undefined) { + if (hasNone) { return; } else { return acc; diff --git a/runtime/Option.res b/runtime/Option.res index e60367a25d..6f4cafb477 100644 --- a/runtime/Option.res +++ b/runtime/Option.res @@ -107,20 +107,17 @@ let compare = (a, b, cmp) => let all = options => { let acc = [] - let returnValue = ref(None) + let hasNone = ref(false) let index = ref(0) - while returnValue.contents == None && index.contents < options->Array.length { + while hasNone.contents == false && index.contents < options->Array.length { switch options->Array.getUnsafe(index.contents) { - | None => returnValue.contents = Some(None) + | None => hasNone.contents = true | Some(value) => acc->Array.push(value) index.contents = index.contents + 1 } } - switch returnValue.contents { - | Some(_) => None - | None => Some(acc) - } + hasNone.contents ? None : Some(acc) } let all2 = ((a, b)) => { diff --git a/tests/tests/src/core/Core_ResultTests.mjs b/tests/tests/src/core/Core_ResultTests.mjs index 1179e2210e..149b1a1d6c 100644 --- a/tests/tests/src/core/Core_ResultTests.mjs +++ b/tests/tests/src/core/Core_ResultTests.mjs @@ -84,6 +84,239 @@ Test.run([ _0: 15 }); +Test.run([ + [ + "Core_ResultTests.res", + 34, + 20, + 25 + ], + "all" +], Result.all([]), eq, { + TAG: "Ok", + _0: [] +}); + +Test.run([ + [ + "Core_ResultTests.res", + 35, + 20, + 25 + ], + "all" +], Result.all([ + { + TAG: "Ok", + _0: 1 + }, + { + TAG: "Ok", + _0: 2 + }, + { + TAG: "Ok", + _0: 3 + } +]), eq, { + TAG: "Ok", + _0: [ + 1, + 2, + 3 + ] +}); + +Test.run([ + [ + "Core_ResultTests.res", + 36, + 20, + 25 + ], + "all" +], Result.all([ + { + TAG: "Ok", + _0: 1 + }, + { + TAG: "Error", + _0: 2 + } +]), eq, { + TAG: "Error", + _0: 2 +}); + +Test.run([ + [ + "Core_ResultTests.res", + 37, + 20, + 25 + ], + "all" +], Result.all2([ + { + TAG: "Ok", + _0: 1 + }, + { + TAG: "Ok", + _0: 2 + } +]), eq, { + TAG: "Ok", + _0: [ + 1, + 2 + ] +}); + +Test.run([ + [ + "Core_ResultTests.res", + 38, + 20, + 25 + ], + "all" +], Result.all2([ + { + TAG: "Ok", + _0: 1 + }, + { + TAG: "Error", + _0: 2 + } +]), eq, { + TAG: "Error", + _0: 2 +}); + +Test.run([ + [ + "Core_ResultTests.res", + 39, + 20, + 25 + ], + "all" +], Result.all3([ + { + TAG: "Ok", + _0: 1 + }, + { + TAG: "Ok", + _0: 2 + }, + { + TAG: "Ok", + _0: 3 + } +]), eq, { + TAG: "Ok", + _0: [ + 1, + 2, + 3 + ] +}); + +Test.run([ + [ + "Core_ResultTests.res", + 40, + 20, + 25 + ], + "all" +], Result.all3([ + { + TAG: "Ok", + _0: 1 + }, + { + TAG: "Error", + _0: 2 + }, + { + TAG: "Ok", + _0: 3 + } +]), eq, { + TAG: "Error", + _0: 2 +}); + +Test.run([ + [ + "Core_ResultTests.res", + 41, + 20, + 25 + ], + "all" +], Result.all4([ + { + TAG: "Ok", + _0: 1 + }, + { + TAG: "Ok", + _0: 2 + }, + { + TAG: "Ok", + _0: 3 + }, + { + TAG: "Ok", + _0: 4 + } +]), eq, { + TAG: "Ok", + _0: [ + 1, + 2, + 3, + 4 + ] +}); + +Test.run([ + [ + "Core_ResultTests.res", + 42, + 20, + 25 + ], + "all" +], Result.all4([ + { + TAG: "Ok", + _0: 1 + }, + { + TAG: "Error", + _0: 2 + }, + { + TAG: "Ok", + _0: 3 + }, + { + TAG: "Ok", + _0: 4 + } +]), eq, { + TAG: "Error", + _0: 2 +}); + export { eq, forEachIfOkCallFunction,