Pure Functions
Definition and Characteristics of Pure Functions
A pure function is a fundamental concept in functional programming meaning that:
- The function's result depends solely on its input parameters (determinism)
- The function's execution does not cause side effects
A side effect is any action performed by a function beyond simply returning a computed value, including:
- Modifying variables (both local and global)
- Mutating data structures "in place"
- Updating object fields
- Throwing exceptions or crashing
-
Interacting with the outside world:
- Input/output (console, file system)
- Graphic operations
- Network requests
- Working with system time
Formal Specification of Pure Functions
From a mathematical perspective, a pure function f: A => B represents:
- A total mapping from the domain (type
A) to the codomain (typeB) - Where each input value
a ∈ Amaps to exactly one output valueb ∈ B -
And where value
bis computed solely based ona, without considering:- The internal state of the system
- The external environment
- Execution time
This definition makes pure functions analogous to mathematical functions, where f(x) = x² will always give the same result for the same x.
Referential Transparency: In-Depth Analysis
Reference transparency is a key property of functional programs, meaning that:
- Any expression can be replaced with its value without changing the program's semantics
- For the same arguments, the result is always identical
- It does not depend on the execution context or the order of computation
Advantages of referential transparency:
- Correctness verification: Ability to formally prove code correctness
- Simplified refactoring: Safe replacement of expressions with equivalent ones
-
Optimizations:
- Memoization (caching results)
- Lazy evaluation
- Parallel execution
- Predictability: The program's behavior becomes fully predictable
In imperative languages, most functions are not referentially transparent due to state dependencies.
Exercise 1.2.1: Identifying Pure Functions
Repository with exercises and instructions
Analyze three implementations of a division function and choose the pure version:
def pureFunction(a: Int, b: Int): Int =
def innerFirst(a: Int, b: Int): Int = a / b
def innerSecond(a: Int, b: Int): Int =
if (b == 0) Int.MaxValue else a / b
def innerThird(a: Int, b: Int): Int =
require(b != 0, "Division by zero!")
a / b
// innerFirst(a, b)
// innerSecond(a, b)
// innerThird(a, b)
???
🔍 Show Solution
Analysis criteria:
-
innerFirst:- Not pure: at
b=0it throws an exception (side effect) - Violates referential transparency (different behavior for
b=0)
- Not pure: at
-
innerSecond:- Pure: always returns a value
- Deterministic: same inputs → same outputs
- No side effects
-
innerThird:- Not pure:
requirethrows anIllegalArgumentException - Alters execution flow (side effect)
- Not pure:
Conclusion: Only innerSecond satisfies all purity conditions:
- Complete handling of all input cases
- No exceptions
- Independence from external state
Links:
- Link to repository with exercises
- Harrison - Introduction to Functional Programming
- Functional Programming by Alexander Alvin
- Functional Programming in Scala, Second Edition, Chapter 1-2 by Michael Pilquist, Rúnar Bjarnason, and Paul Chiusano
- Introduction to Functional Programming - Bird, Wadler
- Scala3 book
- What Purity Is and Isn't