Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Task.seq and Task.forEach #19

Merged
merged 3 commits into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

:eyes: examples: [0.4](https://github.com/roc-lang/basic-webserver/tree/0.4.0/examples), [0.3](https://github.com/roc-lang/basic-webserver/tree/0.3.0/examples), [0.2](https://github.com/roc-lang/basic-webserver/tree/0.2.0/examples), [0.1](https://github.com/roc-lang/basic-webserver/tree/0.1/examples)

:warning: On linux `--linker=legacy` is necessary for this package because of [this Roc issue](https://github.com/roc-lang/roc/issues/3609).

# Basic Web Server for [Roc](https://www.roc-lang.org/)

A webserver [platform](https://github.com/roc-lang/roc/wiki/Roc-concepts-explained#platform) with a simple interface.
Expand All @@ -10,8 +12,6 @@ Write a function which takes a `Http.Request`, perform I/O like fetching content

Behind the scenes, `basic-webserver` uses Rust's high-performance [hyper](https://hyper.rs) and [tokio](https://tokio.rs) libraries to execute your Roc function on incoming requests.

:warning: On linux `--linker=legacy` is necessary for this package because of [this Roc issue](https://github.com/roc-lang/roc/issues/3609).

## Example

Hello world webserver:
Expand Down
38 changes: 38 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,
sequence,
forEach,
]

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

InternalTask.fromEffect effect


## Apply each task in a list sequentially, and return a [Task] with the list of the resulting values.
## Each task will be awaited (see [Task.await]) before beginning the next task,
## execution will stop if an error occurs.
##
## ```
## fetchAuthorTasks : List (Task Author [DbError])
##
## getAuthors : Task (List Author) [DbError]
## getAuthors = Task.sequence fetchAuthorTasks
## ```
##
sequence : List (Task ok err) -> Task (List ok) err
sequence = \tasks ->
List.walk tasks (InternalTask.ok []) \state, task ->
value <- task |> await

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

## Apply a function that returns `Task {} _` for each item in a list.
## Each task will be awaited (see [Task.await]) before beginning the next task,
## execution will stop if an error occurs.
##
## ```
## authors : List Author
## saveAuthor : Author -> Task {} [DbError]
##
## saveAuthors : Task {} [DbError]
## saveAuthors = Task.forEach authors saveAuthor
## ```
##
forEach : List a, (a -> Task {} b) -> Task {} b
forEach = \items, fn ->
List.walk items (InternalTask.ok {}) \state, item ->
state |> await \_ -> fn item
Loading