Add Task.combine

This commit is contained in:
Sam Mohr 2024-12-25 11:44:24 -08:00
parent 57cab7f69a
commit 2b6eaf48d8
No known key found for this signature in database
GPG key ID: EA41D161A3C1BC99
2 changed files with 30 additions and 5 deletions

View file

@ -11,6 +11,7 @@ module [
loop,
fromResult,
batch,
combine,
sequence,
forEach,
result,
@ -187,8 +188,11 @@ fromResult : Result a b -> Task a b
fromResult = \res ->
@Task \{} -> res
## Apply a task to another task applicatively. This can be used with
## [ok] to build a [Task] that returns a record.
## Apply a task to another task applicatively.
##
## DEPRECATED: Modern record builders use [combine].
##
## This can be used with [ok] to build a [Task] that returns a record.
##
## The following example returns a Record with two fields, `apples` and
## `oranges`, each of which is a `List Str`. If it fails it returns the tag
@ -207,6 +211,26 @@ batch = \current ->
await next \f ->
map current f
## Combine the values of two tasks with a custom combining function.
##
## This is primarily used with record builders.
##
## ```
## { a, b, c } =
## { Task.combine <-
## a: Task.ok 123,
## b: File.read "file.txt",
## c: Http.get "http://api.com/",
## }!
## ```
combine : Task a err, Task b err, (a, b -> c) -> Task c err
combine = \@Task leftTask, @Task rightTask, combiner ->
@Task \{} ->
left = try leftTask {}
right = try rightTask {}
Ok (combiner left right)
## Apply each task in a list sequentially, and return a list of the resulting values.
## Each task will be awaited before beginning the next task.
##