add List.splitOnList

This commit is contained in:
Isaac Van Doren 2024-11-10 18:34:34 -06:00
parent 0a7e98ab65
commit ec59acea89
No known key found for this signature in database
GPG key ID: CFA524CD470E5B94
3 changed files with 85 additions and 1 deletions

View file

@ -58,6 +58,7 @@ module [
split,
splitAt,
splitOn,
splitOnList,
splitFirst,
splitLast,
startsWith,
@ -1258,17 +1259,34 @@ splitAt = \elements, userSplitIndex ->
{ before, others }
splitOn: List a, a -> List (List a) where a implements Eq
splitOn : List a, a -> List (List a) where a implements Eq
splitOn = \elements, delimiter ->
help = \remaining, chunks, currentChunk ->
when remaining is
[] -> List.append chunks currentChunk
[x, .. as rest] if x == delimiter ->
help rest (List.append chunks currentChunk) []
[x, .. as rest] ->
help rest chunks (List.append currentChunk x)
help elements [] []
splitOnList : List a, List a -> List (List a) where a implements Eq
splitOnList = \elements, delimiter ->
if delimiter == [] then
[elements]
else
help = \remaining, chunks, currentChunk ->
when remaining is
[] -> List.append chunks currentChunk
[x, .. as rest] ->
if List.startsWith remaining delimiter then
help (List.dropFirst remaining (List.len delimiter)) (List.append chunks currentChunk) []
else
help rest chunks (List.append currentChunk x)
help elements [] []
## DEPRECATED: will be removed soon
split : List elem, U64 -> { before : List elem, others : List elem }
split = \elements, userSplitIndex ->

View file

@ -1512,6 +1512,7 @@ define_builtins! {
90 LIST_WALK_FX: "walk!"
91 LIST_SPLIT_AT: "splitAt"
92 LIST_SPLIT_ON: "splitOn"
93 LIST_SPLIT_ON_LIST: "splitOnList"
}
7 RESULT: "Result" => {
0 RESULT_RESULT: "Result" exposed_type=true // the Result.Result type alias

View file

@ -483,6 +483,71 @@ fn list_split_on() {
);
}
#[test]
#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))]
fn list_split_on_list() {
assert_evals_to!(
r"
List.splitOnList [] []
",
RocList::<RocList<i64>>::from_slice(&[RocList::<i64>::from_slice(&[])]),
RocList<RocList<i64>>
);
assert_evals_to!(
r"
List.splitOnList [] [1, 2, 3]
",
RocList::<RocList<i64>>::from_slice(&[RocList::<i64>::from_slice(&[]),]),
RocList<RocList<i64>>
);
assert_evals_to!(
r"
List.splitOnList [1, 2, 3] []
",
RocList::<RocList<i64>>::from_slice(&[RocList::<i64>::from_slice(&[1, 2, 3]),]),
RocList<RocList<i64>>
);
assert_evals_to!(
r"
List.splitOnList [1] [1]
",
RocList::<RocList<i64>>::from_slice(&[
RocList::<i64>::from_slice(&[]),
RocList::<i64>::from_slice(&[]),
]),
RocList<RocList<i64>>
);
assert_evals_to!(
r"
List.splitOnList [1, 2, 3] [47]
",
RocList::<RocList<i64>>::from_slice(&[RocList::<i64>::from_slice(&[1, 2, 3])]),
RocList<RocList<i64>>
);
assert_evals_to!(
r"
List.splitOnList [1, 2, 3, 4, 5] [2, 3]
",
RocList::<RocList<i64>>::from_slice(&[
RocList::<i64>::from_slice(&[1]),
RocList::<i64>::from_slice(&[4, 5]),
]),
RocList<RocList<i64>>
);
assert_evals_to!(
r"
List.splitOnList [1, 0, 1, 0, 1] [1]
",
RocList::<RocList<i64>>::from_slice(&[
RocList::<i64>::from_slice(&[]),
RocList::<i64>::from_slice(&[0]),
RocList::<i64>::from_slice(&[0]),
RocList::<i64>::from_slice(&[]),
]),
RocList<RocList<i64>>
);
}
#[test]
#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))]
fn list_split_first() {