August 2018
Intermediate to advanced
380 pages
10h 2m
English
We can define a function to run an arbitrary IO task on a given execution context in multiple instances, as follows:
def bunch(n: Int)(gen: String => IO[Nothing]): IO[List[Fiber[IO, Nothing]]] = (1 to n).toList.map(i => s"Task $i").traverse(gen(_).start)
The bunch function takes the number of tasks we need to launch concurrently as the first argument. As a second argument, it takes a function gen to construct tasks. The function takes a string as its first argument, which is the name of the task. In the conditions where we have the same task to run in multiple instances, it is crucial to distinguish them somehow. Therefore, we need to provide the name to the generator function.
To understand the output type of the ...