Skip to content

Commit

Permalink
Simplify Option.all code and check tests in
Browse files Browse the repository at this point in the history
  • Loading branch information
bloodyowl committed Dec 12, 2024
1 parent 6c8590d commit 4a058e0
Show file tree
Hide file tree
Showing 4 changed files with 245 additions and 17 deletions.
9 changes: 4 additions & 5 deletions lib/es6/Option.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 4 additions & 5 deletions lib/js/Option.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 4 additions & 7 deletions runtime/Option.res
Original file line number Diff line number Diff line change
Expand Up @@ -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)) => {
Expand Down
233 changes: 233 additions & 0 deletions tests/tests/src/core/Core_ResultTests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 4a058e0

Please sign in to comment.