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

IO combination without Fibers

Consider that we need to compute a sum of two ranges, and then sum the results. A naive way to combine is as follows:

def sequential: IO[Int] = for {   s1 <- sum(1 , 10)   s2 <- sum(10, 20) } yield s1 + s2

In the preceding code, we combine our computations using the Monadic flow. Let's see what happens if we try and run the competition under a benchmark function:

benchmarkFlush(sequential).unsafeRunSync

The result of the preceding execution is as follows:

First, notice that the first range gets computed first. The second range does not even start until the first range finishes. Also notice how both of the threads ...

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