Stream: beginners

Topic: ✔ task of list ... of tasks?


view this post on Zulip Szymon Kolorz (Jan 09 2024 at 07:09):

Hi there!
I've just begun my journey with Roc .

I'm attempting to create a program that lists files in a directory and extracts some data from each file. I have a function processFile: Path -> Task Str err
and from Dir.list, I receive Task (List Path). However, I don't know how to combine them do create a(Set Str).
I tried to map the list and created Task List (Task Str) but I'm not sure if it is a good approach.
Please, give me some advice

view this post on Zulip Luke Boswell (Jan 09 2024 at 07:31):

You can use either Task.await or Task.loop

view this post on Zulip Luke Boswell (Jan 09 2024 at 07:32):

1sec I'll grab an example

view this post on Zulip Luke Boswell (Jan 09 2024 at 07:33):

Here is an example from roc-wasm4 using Task.loop

getRandomFruit : Snake -> Task Fruit []
getRandomFruit = \{ head, body } ->
    Task.loop {} \{} ->
        x <- W4.randBetween { start: 0, before: 20 } |> Task.await
        y <- W4.randBetween { start: 0, before: 20 } |> Task.await

        fruit = { x, y }
        if fruit == head || List.contains body fruit then
            Step {} |> Task.ok
        else
            Done fruit |> Task.ok

view this post on Zulip Luke Boswell (Jan 09 2024 at 07:34):

Or this may be closer to what you need

drawSnakeBody : Snake -> Task {} []
drawSnakeBody = \snake ->
    List.walk snake.body (Task.ok {}) \task, part ->
        {} <- task |> Task.await

        W4.rect { x: (part.x * 8), y: (part.y * 8), width: 8, height: 8 }

view this post on Zulip Luke Boswell (Jan 09 2024 at 07:34):

Here you could pass in your List Path instead of Snake and walk the list using Task.await

view this post on Zulip Luke Boswell (Jan 09 2024 at 07:37):

So I imagine you want to create a function like processFiles : List Path -> Task (List Str) [YourErrorsTypes]

view this post on Zulip Szymon Kolorz (Jan 09 2024 at 21:31):

It worked with Task.loop,
Thank you!

view this post on Zulip Notification Bot (Jan 09 2024 at 22:08):

Szymon Kolorz has marked this topic as resolved.


Last updated: Jul 06 2025 at 12:14 UTC