lean4-htt/src/Init/Task.lean
2025-04-23 17:21:33 +00:00

31 lines
781 B
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/-
Copyright (c) 2025 Lean FRO. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sebastian Ullrich
Additional `Task` definitions.
-/
module
prelude
import Init.Core
import Init.Data.List.Basic
namespace Task
/--
Creates a task that, when all `tasks` have finished, computes the result of `f` applied to their
results.
-/
def mapList (f : List α → β) (tasks : List (Task α)) (prio := Task.Priority.default)
(sync := false) : Task β :=
go tasks []
where
go
| [], as =>
if sync then
.pure <| f as.reverse
else
Task.spawn (prio := prio) fun _ => f as.reverse
| [t], as => t.map (fun a => f (a :: as).reverse) prio sync
| t::ts, as => t.bind (fun a => go ts (a :: as)) prio sync