Pure vs. Impure Functions

When we can’t predict the results of a function, the function is impure. But before we can devise a strategy to handle impure functions, we need to know how to identify them. In this section you’ll learn all the differences between predictable pure functions and unpredictable impure functions. In this book, you’ve seen many examples that contain impure and pure functions, but we didn’t stop to understand their properties. Now the time has come. We’ll explore them and understand how to identify them by writing some examples.

Pure Functions

Pure functions always return consistent output when given the same input, and never produce effects beyond the function’s scope, making them predictable. For example, write the following ...

Get Learn Functional Programming with Elixir now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.