Add List.forEach! builtin

This commit is contained in:
Agus Zubiaga 2024-11-09 22:23:19 -03:00
parent 644702a2b3
commit ce568c01c1
No known key found for this signature in database
3 changed files with 20 additions and 10 deletions

View file

@ -70,6 +70,7 @@ module [
countIf,
chunksOf,
concatUtf8,
forEach!,
]
import Bool exposing [Bool, Eq]
@ -1383,3 +1384,20 @@ concatUtf8 : List U8, Str -> List U8
expect (List.concatUtf8 [1, 2, 3, 4] "🐦") == [1, 2, 3, 4, 240, 159, 144, 166]
## Run an effectful function for each element on the list.
##
## ```roc
## List.forEach! ["Alice", "Bob", "Charlie"] \name ->
## createAccount! name
## log! "Account created"
## ```
forEach! : List a, (a => {}) => {}
forEach! = \l, f! ->
when l is
[] ->
{}
[x, .. as xs] ->
f! x
forEach! xs f!

View file

@ -1509,7 +1509,7 @@ define_builtins! {
87 LIST_CLONE: "clone"
88 LIST_LEN_USIZE: "lenUsize"
89 LIST_CONCAT_UTF8: "concatUtf8"
90 LIST_WALK_FX: "walk!"
90 LIST_FOR_EACH_FX: "forEach!"
}
7 RESULT: "Result" => {
0 RESULT_RESULT: "Result" exposed_type=true // the Result.Result type alias

View file

@ -5,7 +5,7 @@ import pf.Effect
main! : {} => {}
main! = \{} ->
["Welcome!", "What's your name?"]
|> forEach! Effect.putLine!
|> List.forEach! Effect.putLine!
line = Effect.getLine! {}
@ -17,11 +17,3 @@ main! = \{} ->
Effect.putLine! "You entered: $(line)"
Effect.putLine! "It is known"
forEach! : List a, (a => {}) => {}
forEach! = \l, f! ->
when l is
[] -> {}
[x, .. as xs] ->
f! x
forEach! xs f!