Pure Functions in Clojure
Learn what makes a function pure, why purity matters for Java teams moving to Clojure, and how to separate deterministic calculations from hidden side effects.
A pure function always returns the same result for the same inputs, and it does not do anything observable besides returning a value. No I/O. No mutation. No reading the clock. No random numbers.
That sounds academic until you feel the payoff: pure functions are easy to test, easy to refactor, and safe to run concurrently.
How to think about it as a Java engineer
- A pure function is closer to a “static utility method” than to a method that mutates an object graph.
- The goal is not perfection. The goal is to keep most of your logic in a pure core, and keep side effects in a thin shell.
1(defn total [prices] (reduce + prices)) ;; pure
As you go through this section, practice reading code and asking: “what inputs does this depend on?” and “what does it change?” If the answer is “only its arguments” and “nothing,” you are looking at a pure function.
In this section
-
What Makes a Function Pure
Understand purity as deterministic behavior plus no observable side effects, and learn why Clojure treats it as a design goal rather than a hard rule.
-
Why Pure Functions Matter
See why pure functions make testing, refactoring, debugging, and concurrency easier for JVM teams.
-
Pure or Impure Function Checklist
Use a practical review checklist to spot hidden dependencies, observable side effects, mixed responsibilities, and functions that only look pure.