add List.splitOn

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

View file

@ -57,6 +57,7 @@ module [
intersperse, intersperse,
split, split,
splitAt, splitAt,
splitOn,
splitFirst, splitFirst,
splitLast, splitLast,
startsWith, startsWith,
@ -1257,6 +1258,17 @@ splitAt = \elements, userSplitIndex ->
{ before, others } { before, others }
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 [] []
## DEPRECATED: will be removed soon ## DEPRECATED: will be removed soon
split : List elem, U64 -> { before : List elem, others : List elem } split : List elem, U64 -> { before : List elem, others : List elem }
split = \elements, userSplitIndex -> split = \elements, userSplitIndex ->

View file

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

View file

@ -421,6 +421,68 @@ fn list_split_at() {
); );
} }
#[test]
#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))]
fn list_split_on() {
assert_evals_to!(
r"
List.splitOn [] 1
",
RocList::<RocList<i64>>::from_slice(&[RocList::<i64>::from_slice(&[])]),
RocList<RocList<i64>>
);
assert_evals_to!(
r"
List.splitOn [1] 1
",
RocList::<RocList<i64>>::from_slice(&[
RocList::<i64>::from_slice(&[]),
RocList::<i64>::from_slice(&[]),
]),
RocList<RocList<i64>>
);
assert_evals_to!(
r"
List.splitOn [1, 2, 3] 47
",
RocList::<RocList<i64>>::from_slice(&[RocList::<i64>::from_slice(&[1, 2, 3])]),
RocList<RocList<i64>>
);
assert_evals_to!(
r"
List.splitOn [1, 2, 3, 4, 5] 3
",
RocList::<RocList<i64>>::from_slice(&[
RocList::<i64>::from_slice(&[1, 2]),
RocList::<i64>::from_slice(&[4, 5]),
]),
RocList<RocList<i64>>
);
assert_evals_to!(
r"
List.splitOn [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>>
);
assert_evals_to!(
r"
List.splitOn [1, 0, 1, 0, 1] 0
",
RocList::<RocList<i64>>::from_slice(&[
RocList::<i64>::from_slice(&[1]),
RocList::<i64>::from_slice(&[1]),
RocList::<i64>::from_slice(&[1]),
]),
RocList<RocList<i64>>
);
}
#[test] #[test]
#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))] #[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))]
fn list_split_first() { fn list_split_first() {