2 Pure functions

In this chapter you will learn

  • why we need pure functions
  • how to pass copies of the data
  • how to recalculate instead of storing
  • how to pass the state
  • how to test pure functions

Sometimes, the elegant implementation is just a function. Not a method. Not a class. Not a framework. Just a function.

—John Carmack

Why do we need pure functions?

In the last chapter we learned about functions that don’t lie. Their signatures tell the whole story about their body. We concluded that these are the functions we can trust: the fewer surprises there are when we code, the fewer bugs there will be in the applications we create. In this chapter we will learn about the most trustworthy of all the functions that don’t lie: a pure function.

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