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

Closures and Higher-Order Functions

Closures and Higher-Order Functions

In this lesson — part of Advanced Features — you'll learn closures and higher-order functions in Clojure and why it matters in real work.

Why it matters

Functions that take or return functions unlock concise, composable code.

Key ideas

  • Functions as values
  • map / filter / reduce
  • Callbacks
  • Composition

In practice

Here's how it looks in idiomatic Clojure:

(map inc [1 2 3])              ; => (2 3 4)
(filter even? (range 10))     ; => (0 2 4 6 8)
(reduce + 0 [1 2 3 4])        ; => 10

;; functions returning functions
(defn adder [n] (fn [x] (+ x n)))
(map (adder 10) [1 2 3])      ; => (11 12 13)

Clojure note: map/filter return lazy sequences (nothing computes until consumed), so side effects inside them may not run — use doseq/run! or mapv when you need eager evaluation.

Try it yourself

Exercise: In Clojure, use map and filter to get the squares of the even numbers.

Recap

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