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

Pure functions

In the previous sections, we have shown you how mutations and side effects can make the code harder to read and write. In this section, we will introduce the notion of pure functions, that is, the functions that do not produce side effects. It is central to pure functional programming. The functional paradigm dictates that you should express your program with the help of functions that do not produce any side effects. How would you model a situation where you need to throw an exception with the help of a pure function? Take the familiar Soda Machine example.

This is the slightly shortened version of the Soda Machine example we encountered in our previous discussions on side effects:

var cans = 0def insertCoin(): SodaCan =
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