Skip to content
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

Additional tests #1369

Open
wants to merge 17 commits into
base: dev
Choose a base branch
from
35 changes: 26 additions & 9 deletions src/util/ListUtil.re
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
let rev_if = (b: bool) => b ? List.rev : Fun.id;

let dedup = xs =>
List.fold_right(
(x, deduped) => List.mem(x, deduped) ? deduped : [x, ...deduped],
xs,
[],
);

let dedup_f = (f, xs) =>
List.fold_right(
(x, deduped) => List.exists(f(x), deduped) ? deduped : [x, ...deduped],
xs,
[],
);

let dedup = xs => dedup_f((==), xs);

let are_duplicates = xs =>
7h3kk1d marked this conversation as resolved.
Show resolved Hide resolved
List.length(List.sort_uniq(compare, xs)) == List.length(xs);

/**
Groups elements of a list by a specified key.

{b Note: The groups are not guaranteed to preserve the order of elements from the original list. }

@param key
The key function used to determine the grouping key.

@param xs
The list of elements to be grouped.

@return
A list of tuples where each tuple contains the grouping key and a list of elements that belong to that group.
*/
let group_by = (key: 'x => 'k, xs: list('x)): list(('k, list('x))) =>
List.fold_left(
(grouped, x) => {
Expand All @@ -32,7 +41,7 @@ let group_by = (key: 'x => 'k, xs: list('x)): list(('k, list('x))) =>
xs,
);

let rec range = (~lo=0, hi) =>
let rec range = (~lo: int=0, hi: int) =>
if (lo > hi) {
raise(Invalid_argument("ListUtil.range"));
} else if (lo == hi) {
Expand Down Expand Up @@ -171,7 +180,15 @@ let split_sublist_opt =
let split_sublist =
(i: int, j: int, xs: list('x)): (list('x), list('x), list('x)) =>
switch (split_sublist_opt(i, j, xs)) {
| None => raise(Invalid_argument("ListUtil.split_sublist"))
| None =>
raise(
Invalid_argument(
"ListUtil.split_sublist: "
++ string_of_int(i)
++ ", "
++ string_of_int(j),
),
)
| Some(r) => r
};
let sublist = ((i, j), xs: list('x)): list('x) => {
Expand Down
Loading
Loading