Free preview.You're sampling one lesson — enroll free to unlock all 8 lessons and track your progress.
Enroll free
lesson

Pure Functions

Pure Functions

In this lesson — part of Functional Foundations — you'll learn pure functions in Clojure and why it matters in real work.

Why it matters

Functional style — pure functions and immutability — makes code easier to test and reason about.

Key ideas

  • Pure functions
  • Immutability
  • No shared mutable state
  • Predictable outputs

In practice

Here's how it looks in idiomatic Clojure:

(def double (partial * 2))     ; partial application
(def inc-then-str (comp str inc)) ; right-to-left composition

(double 21)        ; => 42
(inc-then-str 9)   ; => "10"
(map double [1 2 3])           ; => (2 4 6)
((juxt inc dec) 5)             ; => [6 4], run several fns

Clojure note: Pure functions plus comp, partial, and juxt let you build new functions without naming intermediate values — composition reads right-to-left, the reverse of a Unix pipe.

Try it yourself

Exercise: In Clojure, rewrite a function that mutates a global so it's pure.

Recap

You now understand pure functions and can apply it in Clojure. Mark this lesson complete and continue to the next one.