Skip to content

Instantly share code, notes, and snippets.

@jindraivanek
Created August 15, 2023 11:12
Show Gist options
  • Save jindraivanek/27f185de2cbc594e75690f1ec7e74840 to your computer and use it in GitHub Desktop.
Save jindraivanek/27f185de2cbc594e75690f1ec7e74840 to your computer and use it in GitHub Desktop.
Async CE with and! parallelism
type Parallel() =
member this.Bind (a, f) = async.Bind
member this.Bind2 (a, b, f) = async {
let! [|x; y|] = Async.Parallel [a; b]
return! f (x, y)
}
member this.Bind3 (a, b, c, f) = async {
let! [|x; y; z|] = Async.Parallel [a; b; c]
return! f (x, y, z)
}
member this.Return value = async.Return value
member this.ReturnFrom value = async.ReturnFrom value
member this.Delay f = async.Delay f
let parallel = Parallel()
let sleepAndReturn s x = async {
do! Async.Sleep (s*1000)
return x }
let a =
parallel {
let scale = 1
let! x = sleepAndReturn scale 1
and! y = sleepAndReturn scale 2
and! z = sleepAndReturn scale 3
return x + y + z }
#time
a |> Async.RunSynchronously
@jindraivanek
Copy link
Author

> a |> Async.RunSynchronously;;
Real: 00:00:01.001, CPU: 00:00:00.046, GC gen0: 0, gen1: 0, gen2: 0
val it: int = 6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment