Skip to Content
Learning Functional Programming
book

Learning Functional Programming

by Jack Widman
August 2022
Intermediate to advanced
135 pages
2h 51m
English
O'Reilly Media, Inc.
Content preview from Learning Functional Programming

Chapter 6. Questions of Concurrency

I am going to start this chapter with a sentence that sounds a bit mystical: with no mutation of state, we can ignore time.

Let us unpack this. First of all, what does time have to do with mutation of state, or with programming for that matter? Time enters programming when we have a set of processes that need to be executed in a particular order.

Let’s consider the following example.1 Consider two functions. The first takes x to x + 1. The second takes x to x × x. Let’s set x to 10. Now let’s imagine running these two functions in parallel. What would the answer be? Would you be surprised that there are five possible correct answers? Let’s break this down. Let P1 = xx × x and P2 = xx + 1:

  • If P1 sets x to 100 and then P2 sets 100 to 101, we get 101.

  • If P2 sets x to 11 and then P1 sets 11 to 121, we get 121.

  • P2 changes 10 to 11 between the two times that P1 accesses the value of x during the evaluation of x × x. In this case, we get 110.

  • P2 accesses x. Then P1 sets x to 100. Then P2 sets x. This gives 10.

  • P1 accesses x twice. Then P2 sets x to 11. Then P1 sets x. This gives 100.

Which is the correct answer? 101, 121, 110, 10, or 100? Well, that depends on what the programmer intended. The point is that if we just let P1 and P2 run in parallel, we can’t be sure which answer will be returned. The reason is that the computer can decide to evaluate these two functions in any of the earlier orders.

Note

When state is mutated, 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.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Mastering Functional Programming

Mastering Functional Programming

Anatolii Kmetiuk
Functional Programming in Kotlin

Functional Programming in Kotlin

Runar Bjarnason, Marco Vermeulen, Paul Chiusano

Publisher Resources

ISBN: 9781098111748Errata Page