Scala flatmap and Parallel programming

We usually use flatmap and map in scala. Of course, if we use future we chaining by using flatmap or map… etc. But is this run parallel? In fact (as you know) it’s not parallel. If you chaining some monads, it runs sequentially.

Future chaining is not parallel

Let’s see some example. Guess that, there is some db dependent function like beneath

Each takes 2 seconds, 2 seconds, 3 seconds. And we want to make userInfo by calling functions. You can call like this.

As you can see, It takes 7 seconds (2 sec + 2 sec + 3 sec). It is not parallel.

How to be parallel?

by function

There easy 2 ways to be parallel. Solve this by function. You can make like this.

It takes 5 seconds(2 seconds + max(2 seconds, 3 seconds)). It’s because, before chaining, Future already started. And after getting an email address, 2 seconds, we only need to wait for 1 second to get accountInfo because it runs parallel.

by using the Cartesian product and applicative

But it has to make function. It’s annoying. We can simply make this by using cats.

It also takes 5 seconds, But it is simpler than by using a function.

2 thoughts on “Scala flatmap and Parallel programming”

  1. Great article. Especially applicative usage.
    When I read, i remembered this:
    So can I suggest another solution?

    val start4: Instant =

    val fUserInfo4 = for {
    nickname <- getNicknameById(userId)
    emailAddrF = getEmailAddressByNickname(nickname)
    accountInfoF = getAccountInfoByNickname(nickname)
    emailAddr <- emailAddrF
    accountInfo <- accountInfoF
    } yield UserInfo(userId, nickname, emailAddr, accountInfo)

    val userInfo4 = Await.result(fUserInfo4, 10.second)

    val end4: Instant =

    println(printInterval(userInfo4, start4, end4)) // UserInfo(31337,ktz,,None) take 5 seconds


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s