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 =

Get Mastering Functional Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.