Skip to Content
Mastering Functional Programming
book

Mastering Functional Programming

by Anatolii Kmetiuk
August 2018
Intermediate to advanced
380 pages
10h 2m
English
Packt Publishing
Content preview from Mastering Functional Programming

Running tasks in bunches

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning Functional Programming

Learning Functional Programming

Jack Widman

Publisher Resources

ISBN: 9781788620796Supplemental Content