Skip to content

Parallel IO Example #1439

Answered by louthy
glendonjklassen asked this question in Q&A
Jan 20, 2025 · 1 comments · 1 reply
Discussion options

You must be logged in to vote

To make any operation run in parallel you can use IO.fork, it works with IO<A> or any K<M, A> where the M trait-implementation supports MonadIO.ToIO.

You can use it like so:

var delayed    = IO.liftAsync(async () =>
                             {
                                 await Task.Delay(3000);
                                 return unit;
                             });
var readKey    = IO.lift(Console.ReadKey);
var startTimer = IO.lift(Stopwatch.StartNew);
var writeLine  = (object? value) => IO.lift(() => Console.WriteLine(value));

var op = from watch in startTimer
         from fork1 in IO.fork(delayed)
         from fork2 in IO.fork(delayed)
         from fork3 in IO.fork(de…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@glendonjklassen
Comment options

Answer selected by glendonjklassen
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants