21 What is purity?

After reading this lesson, you will be able to

  • Differentiate between pure and impure functions
  • Provide code examples in which impure functions cause unpredicted code behavior

In this lesson, you’ll learn about purity, a fundamental principle of functional programming. In particular, you’ll see that a pure function is total and has no side effects. Distinguishing between pure and impure functions can help you identify and prevent bugs in your code. For example, consider the following scenario:

Suppose you are developing the software for a smart thermostat. Your business requirements dictate your thermostat never to reach temperatures below the freezing point of 0°C (equivalent to 32°F) because it could damage its mechanical ...

Get Get Programming with Scala 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.