mirror of
https://github.com/roc-lang/roc.git
synced 2025-08-03 03:42:17 +00:00
builtin(list): implement List.walkBackwardsUntil
Signed-off-by: Prajwal S N <prajwalnadig21@gmail.com>
This commit is contained in:
parent
fd42879dc4
commit
cbceeff902
3 changed files with 65 additions and 0 deletions
|
@ -61,6 +61,7 @@ interface List
|
||||||
sortAsc,
|
sortAsc,
|
||||||
sortDesc,
|
sortDesc,
|
||||||
reserve,
|
reserve,
|
||||||
|
walkBackwardsUntil,
|
||||||
]
|
]
|
||||||
imports [
|
imports [
|
||||||
Bool.{ Bool },
|
Bool.{ Bool },
|
||||||
|
@ -436,6 +437,13 @@ walkUntil = \list, initial, step ->
|
||||||
Continue new -> new
|
Continue new -> new
|
||||||
Break new -> new
|
Break new -> new
|
||||||
|
|
||||||
|
## Same as [List.walkUntil], but does it from the end of the list instead.
|
||||||
|
walkBackwardsUntil : List elem, state, (state, elem -> [Continue state, Break state]) -> state
|
||||||
|
walkBackwardsUntil = \list, initial, func ->
|
||||||
|
when List.iterateBackwards list initial func 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
|
||||||
|
|
|
@ -1374,6 +1374,7 @@ define_builtins! {
|
||||||
73 LIST_CAPACITY: "capacity"
|
73 LIST_CAPACITY: "capacity"
|
||||||
74 LIST_MAP_TRY: "mapTry"
|
74 LIST_MAP_TRY: "mapTry"
|
||||||
75 LIST_WALK_TRY: "walkTry"
|
75 LIST_WALK_TRY: "walkTry"
|
||||||
|
76 LIST_WALK_BACKWARDS_UNTIL: "walkBackwardsUntil"
|
||||||
}
|
}
|
||||||
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
|
||||||
|
|
|
@ -3395,3 +3395,59 @@ fn list_let_generalization() {
|
||||||
usize
|
usize
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[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]
|
||||||
|
#[cfg(any(feature = "gen-llvm", feature = "gen-wasm"))]
|
||||||
|
fn list_walk_backwards_implements_position() {
|
||||||
|
assert_evals_to!(
|
||||||
|
r#"
|
||||||
|
Option a : [Some a, None]
|
||||||
|
|
||||||
|
find : List a, a -> Option Nat
|
||||||
|
find = \list, needle ->
|
||||||
|
findHelp list needle
|
||||||
|
|> .v
|
||||||
|
|
||||||
|
findHelp = \list, needle ->
|
||||||
|
List.walkBackwardsUntil list { n: 0, v: None } \{ n, v }, element ->
|
||||||
|
if element == needle then
|
||||||
|
Break { n, v: Some n }
|
||||||
|
else
|
||||||
|
Continue { n: n + 1, v }
|
||||||
|
|
||||||
|
when find [1, 2, 3] 3 is
|
||||||
|
None -> 0
|
||||||
|
Some v -> v
|
||||||
|
"#,
|
||||||
|
0,
|
||||||
|
usize
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[cfg(any(feature = "gen-llvm", feature = "gen-wasm"))]
|
||||||
|
fn list_walk_backwards_until_even_prefix_sum() {
|
||||||
|
assert_evals_to!(
|
||||||
|
r#"
|
||||||
|
helper = \a, b ->
|
||||||
|
if Num.isEven b then
|
||||||
|
Continue (a + b)
|
||||||
|
|
||||||
|
else
|
||||||
|
Break a
|
||||||
|
|
||||||
|
List.walkBackwardsUntil [9, 8, 4, 2] 0 helper"#,
|
||||||
|
2 + 4 + 8,
|
||||||
|
i64
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue