Skip to content

Commit

Permalink
add seq and forEach to Task
Browse files Browse the repository at this point in the history
  • Loading branch information
lukewilliamboswell committed Jun 22, 2024
1 parent bab99de commit 14173e6
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions platform/Task.roc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ module [
fromResult,
batch,
result,
seq,
forEach,
]

import Effect
Expand Down Expand Up @@ -244,3 +246,34 @@ result = \task ->
\res -> res |> ok |> InternalTask.toEffect

InternalTask.fromEffect effect
## Apply a task repeatedly to a list of items, and return a list of the resulting values
##
## ```
## authors : List ID
## getAuthor : ID -> Task Author [DbError]
##
## getAuthors : Task (List Author) [DbError]
## getAuthors = Task.list authors getAuthor
## ```
##
seq : List (Task ok err) -> Task (List ok) err
seq = \tasks ->
List.walk tasks (Task.ok []) \state, task ->
value <- task |> Task.await

state |> Task.map \values -> List.append values value

## Apply a task repeatedly for each item in a list
##
## ```
## authors : List Author
## saveAuthor : Author -> Task {} [DbError]
##
## saveAuthors : Task (List Author) [DbError]
## saveAuthors = Task.forEach authors saveAuthor
## ```
##
forEach : List a, (a -> Task {} b) -> Task {} b
forEach = \items, fn ->
List.walk items (Task.ok {}) \state, item ->
state |> Task.await \_ -> fn item

0 comments on commit 14173e6

Please sign in to comment.