mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-27 13:59:08 +00:00
builtin(list): add List.walkFrom, List.walkFromUntil
Signed-off-by: Prajwal S N <prajwalnadig21@gmail.com>
This commit is contained in:
parent
99ad019d75
commit
07224e9086
3 changed files with 64 additions and 10 deletions
|
@ -29,6 +29,8 @@ interface List
|
||||||
map3,
|
map3,
|
||||||
product,
|
product,
|
||||||
walkUntil,
|
walkUntil,
|
||||||
|
walkFrom,
|
||||||
|
walkFromUntil,
|
||||||
range,
|
range,
|
||||||
sortWith,
|
sortWith,
|
||||||
drop,
|
drop,
|
||||||
|
@ -443,6 +445,22 @@ walkBackwardsUntil = \list, initial, func ->
|
||||||
Continue new -> new
|
Continue new -> new
|
||||||
Break new -> new
|
Break new -> new
|
||||||
|
|
||||||
|
## Walks to the end of the list from a specified starting index
|
||||||
|
walkFrom : List elem, Nat, state, (state, elem -> state) -> state
|
||||||
|
walkFrom = \list, index, state, func ->
|
||||||
|
walkHelp : _, _ -> [Continue _, Break []]
|
||||||
|
walkHelp = \currentState, element -> Continue (func currentState element)
|
||||||
|
|
||||||
|
when List.iterHelp list state walkHelp index (List.len list) is
|
||||||
|
Continue new -> new
|
||||||
|
|
||||||
|
## A combination of [List.walkFrom] and [List.walkUntil]
|
||||||
|
walkFromUntil : List elem, Nat, state, (state, elem -> [Continue state, Break state]) -> state
|
||||||
|
walkFromUntil = \list, index, state, func ->
|
||||||
|
when List.iterHelp list state func index (List.len list) is
|
||||||
|
Continue new -> new
|
||||||
|
Break new -> new
|
||||||
|
|
||||||
sum : List (Num a) -> Num a
|
sum : List (Num a) -> Num a
|
||||||
sum = \list ->
|
sum = \list ->
|
||||||
List.walk list 0 Num.add
|
List.walk list 0 Num.add
|
||||||
|
|
|
@ -1398,6 +1398,8 @@ define_builtins! {
|
||||||
75 LIST_WALK_TRY: "walkTry"
|
75 LIST_WALK_TRY: "walkTry"
|
||||||
76 LIST_WALK_BACKWARDS_UNTIL: "walkBackwardsUntil"
|
76 LIST_WALK_BACKWARDS_UNTIL: "walkBackwardsUntil"
|
||||||
77 LIST_COUNT_IF: "countIf"
|
77 LIST_COUNT_IF: "countIf"
|
||||||
|
78 LIST_WALK_FROM: "walkFrom"
|
||||||
|
79 LIST_WALK_FROM_UNTIL: "walkFromUntil"
|
||||||
}
|
}
|
||||||
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
|
||||||
|
|
|
@ -956,6 +956,12 @@ fn list_walk_until_even_prefix_sum() {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[cfg(any(feature = "gen-llvm", feature = "gen-wasm"))]
|
||||||
|
fn list_walk_from_sum() {
|
||||||
|
assert_evals_to!(r#"List.walkFrom [1, 2, 3] 1 0 Num.add"#, 5, i64);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(any(feature = "gen-llvm", feature = "gen-wasm"))]
|
#[cfg(any(feature = "gen-llvm", feature = "gen-wasm"))]
|
||||||
fn list_keep_if_empty_list_of_int() {
|
fn list_keep_if_empty_list_of_int() {
|
||||||
|
@ -3482,16 +3488,6 @@ fn list_let_generalization() {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
#[cfg(any(feature = "gen-llvm", feature = "gen-wasm"))]
|
|
||||||
fn list_walk_backwards_until_sum() {
|
|
||||||
assert_evals_to!(
|
|
||||||
r#"List.walkBackwardsUntil [1, 2] 0 \a,b -> Continue (a + b)"#,
|
|
||||||
3,
|
|
||||||
i64
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(any(feature = "gen-llvm", feature = "gen-wasm"))]
|
#[cfg(any(feature = "gen-llvm", feature = "gen-wasm"))]
|
||||||
fn list_walk_backwards_implements_position() {
|
fn list_walk_backwards_implements_position() {
|
||||||
|
@ -3520,6 +3516,16 @@ fn list_walk_backwards_implements_position() {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[cfg(any(feature = "gen-llvm", feature = "gen-wasm"))]
|
||||||
|
fn list_walk_backwards_until_sum() {
|
||||||
|
assert_evals_to!(
|
||||||
|
r#"List.walkBackwardsUntil [1, 2] 0 \a,b -> Continue (a + b)"#,
|
||||||
|
3,
|
||||||
|
i64
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(any(feature = "gen-llvm", feature = "gen-wasm"))]
|
#[cfg(any(feature = "gen-llvm", feature = "gen-wasm"))]
|
||||||
fn list_walk_backwards_until_even_prefix_sum() {
|
fn list_walk_backwards_until_even_prefix_sum() {
|
||||||
|
@ -3537,3 +3543,31 @@ fn list_walk_backwards_until_even_prefix_sum() {
|
||||||
i64
|
i64
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[cfg(any(feature = "gen-llvm", feature = "gen-wasm"))]
|
||||||
|
fn list_walk_from_until_sum() {
|
||||||
|
assert_evals_to!(
|
||||||
|
r#"List.walkFromUntil [1, 2, 3, 4] 2 0 \a,b -> Continue (a + b)"#,
|
||||||
|
7,
|
||||||
|
i64
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[cfg(any(feature = "gen-llvm", feature = "gen-wasm"))]
|
||||||
|
fn list_walk_from_even_prefix_sum() {
|
||||||
|
assert_evals_to!(
|
||||||
|
r#"
|
||||||
|
helper = \a, b ->
|
||||||
|
if Num.isEven b then
|
||||||
|
Continue (a + b)
|
||||||
|
|
||||||
|
else
|
||||||
|
Break a
|
||||||
|
|
||||||
|
List.walkFromUntil [2, 4, 8, 9] 1 0 helper"#,
|
||||||
|
4 + 8,
|
||||||
|
i64
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue