Skip to content

Add Option.all* & Result.all* helpers #7181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

# 12.0.0-alpha.6 (Unreleased)
- Fix exponential notation syntax. https://github.com/rescript-lang/rescript/pull/7174
- Add `Option.all` & `Result.all` helpers. https://github.com/rescript-lang/rescript/pull/7181

#### :bug: Bug fix
- Fix bug where a ref assignment is moved ouside a conditional. https://github.com/rescript-lang/rescript/pull/7176
Expand Down
106 changes: 106 additions & 0 deletions lib/es6/Option.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,106 @@ function compare(a, b, cmp) {
}
}

function all(options) {
let acc = [];
let hasNone = false;
let index = 0;
while (hasNone === false && index < options.length) {
let value = options[index];
if (value !== undefined) {
acc.push(Primitive_option.valFromOption(value));
index = index + 1 | 0;
} else {
hasNone = true;
}
};
if (hasNone) {
return;
} else {
return acc;
}
}

function all2(param) {
let b = param[1];
let a = param[0];
if (a !== undefined && b !== undefined) {
return [
Primitive_option.valFromOption(a),
Primitive_option.valFromOption(b)
];
}

}

function all3(param) {
let c = param[2];
let b = param[1];
let a = param[0];
if (a !== undefined && b !== undefined && c !== undefined) {
return [
Primitive_option.valFromOption(a),
Primitive_option.valFromOption(b),
Primitive_option.valFromOption(c)
];
}

}

function all4(param) {
let d = param[3];
let c = param[2];
let b = param[1];
let a = param[0];
if (a !== undefined && b !== undefined && c !== undefined && d !== undefined) {
return [
Primitive_option.valFromOption(a),
Primitive_option.valFromOption(b),
Primitive_option.valFromOption(c),
Primitive_option.valFromOption(d)
];
}

}

function all5(param) {
let e = param[4];
let d = param[3];
let c = param[2];
let b = param[1];
let a = param[0];
if (a !== undefined && b !== undefined && c !== undefined && d !== undefined && e !== undefined) {
return [
Primitive_option.valFromOption(a),
Primitive_option.valFromOption(b),
Primitive_option.valFromOption(c),
Primitive_option.valFromOption(d),
Primitive_option.valFromOption(e)
];
}

}

function all6(param) {
let f = param[5];
let e = param[4];
let d = param[3];
let c = param[2];
let b = param[1];
let a = param[0];
if (a !== undefined && b !== undefined && c !== undefined && d !== undefined && e !== undefined && f !== undefined) {
return [
Primitive_option.valFromOption(a),
Primitive_option.valFromOption(b),
Primitive_option.valFromOption(c),
Primitive_option.valFromOption(d),
Primitive_option.valFromOption(e),
Primitive_option.valFromOption(f)
];
}

}

let mapWithDefault = mapOr;

let getWithDefault = getOr;
Expand All @@ -116,5 +216,11 @@ export {
isNone,
equal,
compare,
all,
all2,
all3,
all4,
all5,
all6,
}
/* No side effect */
250 changes: 250 additions & 0 deletions lib/es6/Result.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,250 @@ function mapError(r, f) {
}
}

function all(results) {
let acc = [];
let returnValue;
let index = 0;
while (returnValue === undefined && index < results.length) {
let err = results[index];
if (err.TAG === "Ok") {
acc.push(err._0);
index = index + 1 | 0;
} else {
returnValue = err;
}
};
let error = returnValue;
if (error !== undefined) {
return error;
} else {
return {
TAG: "Ok",
_0: acc
};
}
}

function all2(param) {
let b = param[1];
let a = param[0];
if (a.TAG === "Ok") {
if (b.TAG === "Ok") {
return {
TAG: "Ok",
_0: [
a._0,
b._0
]
};
} else {
return {
TAG: "Error",
_0: b._0
};
}
} else {
return {
TAG: "Error",
_0: a._0
};
}
}

function all3(param) {
let c = param[2];
let b = param[1];
let a = param[0];
if (a.TAG === "Ok") {
if (b.TAG === "Ok") {
if (c.TAG === "Ok") {
return {
TAG: "Ok",
_0: [
a._0,
b._0,
c._0
]
};
} else {
return {
TAG: "Error",
_0: c._0
};
}
} else {
return {
TAG: "Error",
_0: b._0
};
}
} else {
return {
TAG: "Error",
_0: a._0
};
}
}

function all4(param) {
let d = param[3];
let c = param[2];
let b = param[1];
let a = param[0];
if (a.TAG === "Ok") {
if (b.TAG === "Ok") {
if (c.TAG === "Ok") {
if (d.TAG === "Ok") {
return {
TAG: "Ok",
_0: [
a._0,
b._0,
c._0,
d._0
]
};
} else {
return {
TAG: "Error",
_0: d._0
};
}
} else {
return {
TAG: "Error",
_0: c._0
};
}
} else {
return {
TAG: "Error",
_0: b._0
};
}
} else {
return {
TAG: "Error",
_0: a._0
};
}
}

function all5(param) {
let e = param[4];
let d = param[3];
let c = param[2];
let b = param[1];
let a = param[0];
if (a.TAG === "Ok") {
if (b.TAG === "Ok") {
if (c.TAG === "Ok") {
if (d.TAG === "Ok") {
if (e.TAG === "Ok") {
return {
TAG: "Ok",
_0: [
a._0,
b._0,
c._0,
d._0,
e._0
]
};
} else {
return {
TAG: "Error",
_0: e._0
};
}
} else {
return {
TAG: "Error",
_0: d._0
};
}
} else {
return {
TAG: "Error",
_0: c._0
};
}
} else {
return {
TAG: "Error",
_0: b._0
};
}
} else {
return {
TAG: "Error",
_0: a._0
};
}
}

function all6(param) {
let f = param[5];
let e = param[4];
let d = param[3];
let c = param[2];
let b = param[1];
let a = param[0];
if (a.TAG === "Ok") {
if (b.TAG === "Ok") {
if (c.TAG === "Ok") {
if (d.TAG === "Ok") {
if (e.TAG === "Ok") {
if (f.TAG === "Ok") {
return {
TAG: "Ok",
_0: [
a._0,
b._0,
c._0,
d._0,
e._0,
f._0
]
};
} else {
return {
TAG: "Error",
_0: f._0
};
}
} else {
return {
TAG: "Error",
_0: e._0
};
}
} else {
return {
TAG: "Error",
_0: d._0
};
}
} else {
return {
TAG: "Error",
_0: c._0
};
}
} else {
return {
TAG: "Error",
_0: b._0
};
}
} else {
return {
TAG: "Error",
_0: a._0
};
}
}

let mapWithDefault = mapOr;

let getWithDefault = getOr;
Expand All @@ -116,5 +360,11 @@ export {
compare,
forEach,
mapError,
all,
all2,
all3,
all4,
all5,
all6,
}
/* No side effect */
Loading
Loading