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 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.