Skip to content

Add Option.all & Result.all helpers #202

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

Closed
wants to merge 2 commits into from
Closed
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
107 changes: 107 additions & 0 deletions src/Core__Option.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,107 @@ function compare(a, b, cmp) {
}
}

function all(options) {
var acc = [];
var returnValue;
var index = 0;
while(returnValue === undefined && index < options.length) {
var value = options[index];
if (value !== undefined) {
acc.push(Caml_option.valFromOption(value));
index = index + 1 | 0;
} else {
returnValue = Caml_option.some(undefined);
}
};
var match = returnValue;
if (match !== undefined) {
return ;
} else {
return acc;
}
}

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

}

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

}

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

}

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

}

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

}

var mapWithDefault = mapOr;

var getWithDefault = getOr;
Expand All @@ -117,5 +218,11 @@ export {
isNone ,
equal ,
compare ,
all ,
all2 ,
all3 ,
all4 ,
all5 ,
all6 ,
}
/* No side effect */
53 changes: 53 additions & 0 deletions src/Core__Option.res
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,56 @@ let compare = (a, b, cmp) =>
| (Some(_), None) => Core__Ordering.greater
| (None, None) => Core__Ordering.equal
}

let all = options => {
let acc = []
let returnValue = ref(None)
let index = ref(0)
while returnValue.contents == None && index.contents < options->Core__Array.length {
switch options->Core__Array.getUnsafe(index.contents) {
| None => returnValue.contents = Some(None)
| Some(value) =>
acc->Core__Array.push(value)
index.contents = index.contents + 1
}
}
switch returnValue.contents {
| Some(_) => None
| None => Some(acc)
}
}

let all2 = ((a, b)) => {
switch (a, b) {
| (Some(a), Some(b)) => Some((a, b))
| _ => None
}
}

let all3 = ((a, b, c)) => {
switch (a, b, c) {
| (Some(a), Some(b), Some(c)) => Some((a, b, c))
| _ => None
}
}

let all4 = ((a, b, c, d)) => {
switch (a, b, c, d) {
| (Some(a), Some(b), Some(c), Some(d)) => Some((a, b, c, d))
| _ => None
}
}

let all5 = ((a, b, c, d, e)) => {
switch (a, b, c, d, e) {
| (Some(a), Some(b), Some(c), Some(d), Some(e)) => Some((a, b, c, d, e))
| _ => None
}
}

let all6 = ((a, b, c, d, e, f)) => {
switch (a, b, c, d, e, f) {
| (Some(a), Some(b), Some(c), Some(d), Some(e), Some(f)) => Some((a, b, c, d, e, f))
| _ => None
}
}
50 changes: 50 additions & 0 deletions src/Core__Option.resi
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,53 @@ Option.compare(None, None, clockCompare) // 0.
```
*/
let compare: (option<'a>, option<'b>, ('a, 'b) => Core__Ordering.t) => Core__Ordering.t

/**
`all(options)` returns an option of array if all options are Some, otherwise returns None.

## Examples

```rescript
Option.all([Some(1), Some(2), Some(3)]) // Some([1, 2, 3])
Option.all([Some(1), None]) // None
```
*/
let all: array<option<'a>> => option<array<'a>>

/**
`all2((o1, o2))`. Like `all()`, but with a fixed size tuple of 2
*/
let all2: ((option<'a>, option<'b>)) => option<('a, 'b)>

/**
`all3((o1, o2, o3))`. Like `all()`, but with a fixed size tuple of 2
*/
let all3: ((option<'a>, option<'b>, option<'c>)) => option<('a, 'b, 'c)>

/**
`all4((o1, o2, o3, o4))`. Like `all()`, but with a fixed size tuple of 2
*/
let all4: ((option<'a>, option<'b>, option<'c>, option<'d>)) => option<('a, 'b, 'c, 'd)>

/**
`all5((o1, o2, o3, o4, o5))`. Like `all()`, but with a fixed size tuple of 2
*/
let all5: ((option<'a>, option<'b>, option<'c>, option<'d>, option<'e>)) => option<(
'a,
'b,
'c,
'd,
'e,
)>

/**
`all6((o1, o2, o3, o4, o5, o6))`. Like `all()`, but with a fixed size tuple of 2
*/
let all6: ((option<'a>, option<'b>, option<'c>, option<'d>, option<'e>, option<'f>)) => option<(
'a,
'b,
'c,
'd,
'e,
'f,
)>
Loading
Loading