Created
August 15, 2023 11:12
-
-
Save jindraivanek/27f185de2cbc594e75690f1ec7e74840 to your computer and use it in GitHub Desktop.
Async CE with and! parallelism
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Author
jindraivanek
commented
Aug 15, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment