add foldr

This commit is contained in:
Scott Richmond 2025-06-12 16:55:17 -04:00
parent 763219d111
commit fe029573cf

View File

@ -228,6 +228,22 @@ fn fold {
} }
} }
fn foldr {
"Folds a list, right-associative."
(f as :fn, []) -> []
(f as :fn, xs as :list) -> fold (f, xs, f ())
(f as :fn, [], root) -> []
(f as :fn, xs as :list, root) -> loop (root, first (xs), rest (xs)) with {
(prev, curr, []) -> f (curr, prev)
(prev, curr, remaining) -> recur (
f (curr, prev)
first (remaining)
rest (remaining)
)
}
}
& TODO: optimize these with base :conj! & TODO: optimize these with base :conj!
fn map { fn map {
"Maps a function over a list: returns a new list with elements that are the result of applying the function to each element in the original list. E.g., `map ([1, 2, 3], inc) &=> [2, 3, 4]`." "Maps a function over a list: returns a new list with elements that are the result of applying the function to each element in the original list. E.g., `map ([1, 2, 3], inc) &=> [2, 3, 4]`."
@ -1359,6 +1375,7 @@ pkg Prelude {
floor & math floor & math
fn? & functions fn? & functions
fold & lists fold & lists
foldr & lists
forward! & turtles forward! & turtles
get & dicts get & dicts
goto! & turtles goto! & turtles