mirror of
https://github.com/roc-lang/roc.git
synced 2025-11-02 05:48:17 +00:00
Add List.forEach! builtin
This commit is contained in:
parent
644702a2b3
commit
ce568c01c1
3 changed files with 20 additions and 10 deletions
|
|
@ -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!
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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!
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue