Skip to content
Tutorials Center
My learning

Topics

Lesson 17 of 36 ยท Functions and Higher-Order Functions

Higher-Order Functions: map, filter, reduce, partial, comp

Use Clojure's higher-order functions: map, filter, remove, reduce with and without an initial value, apply, partial, comp, juxt, complement, every?, some and sort-by with real examples.

A higher-order function takes a function as an argument, returns a function, or both. They are the reason loops are rare in idiomatic Clojure: instead of writing how to walk a collection, you say what to do with each element and let a library function do the walking. This lesson covers the ones you will use every day.

map

map applies a function to every element and returns a lazy sequence of the results:

CLOJURE
(map inc [1 2 3])                 ; (2 3 4)
(map str/upper-case ["a" "b"])    ; ("A" "B")
(map :name [{:name "Ada"} {:name "Grace"}])   ; ("Ada" "Grace")
(map + [1 2 3] [10 20 30])        ; (11 22 33)   several collections
(map vector [:a :b] [1 2])        ; ([:a 1] [:b 2])
(mapv inc [1 2 3])                ; [2 3 4]      a vector instead

With several collections, map walks them in step and stops at the shortest one. mapcat maps and then concatenates the results, which is a flat-map:

CLOJURE
(mapcat #(repeat % %) [1 2 3])    ; (1 2 2 3 3 3)

filter and remove

filter keeps the elements for which a predicate is truthy; remove keeps those for which it is falsey:

CLOJURE
(filter even? (range 10))         ; (0 2 4 6 8)
(remove even? (range 10))         ; (1 3 5 7 9)
(filter #(> (count %) 2) ["a" "abc" "abcd"])   ; ("abc" "abcd")
(keep #(when (even? %) (* % %)) (range 6))     ; (0 4 16)

keep is map followed by dropping the nil results, which is why the last example returns squares of the even numbers only. Note that keep also drops nil only, while false stays.

reduce

reduce folds a collection into a single value by repeatedly applying a function of two arguments: the accumulator and the next element.

CLOJURE
(reduce + [1 2 3 4])          ; 10
(reduce + 100 [1 2 3 4])      ; 110   with an initial value
(reduce max [3 9 4])          ; 9
(reduce conj [] (range 3))    ; [0 1 2]
(reduce (fn [acc x] (assoc acc x (* x x))) {} [1 2 3])
;; => {1 1, 2 4, 3 9}

Follow it step by step: (reduce + [1 2 3]) computes (+ 1 2) which is 3, then (+ 3 3) which is 6. With an initial value, the first call is (+ init first-element). If the collection is empty and there is no initial value, reduce calls the function with no arguments: (reduce + []) is 0 because (+) is 0.

reductions shows every intermediate step, and reduced stops a reduction early:

CLOJURE
(reductions + [1 2 3 4])                ; (1 3 6 10)
(reduce (fn [acc x]
          (if (> acc 10) (reduced acc) (+ acc x)))
        (range 100))                    ; 15
JS Stand-in: map, filter and reduce

Press Run (or Ctrl + Enter) to see the console output here.

No output.

apply

apply calls a function with a collection spread into arguments:

CLOJURE
(apply + [1 2 3])             ; 6
(apply max [3 9 4])           ; 9
(apply str ["a" "b" "c"])     ; "abc"
(apply map vector [[1 2 3] [4 5 6]])   ; ([1 4] [2 5] [3 6])  a transpose

Functions that build functions

partial

partial fixes the first arguments of a function and returns a new function waiting for the rest:

CLOJURE
(def add10 (partial + 10))
(add10 5)                      ; 15
(map (partial * 2) [1 2 3])    ; (2 4 6)

(def greet (partial str "Hello, "))
(greet "Ada")                  ; "Hello, Ada"

comp

comp composes functions, applying them right to left:

CLOJURE
(def shout (comp str/upper-case str/trim))
(shout "  hello ")             ; "HELLO"

(def inc-then-double (comp #(* 2 %) inc))
(inc-then-double 4)            ; 10   (inc first, then double)

juxt, complement, constantly, identity

CLOJURE
((juxt :name :age) {:name "Ada" :age 36})   ; ["Ada" 36]  results of each function
((complement even?) 3)                        ; true       logical opposite
(map (constantly 0) [1 2 3])                  ; (0 0 0)
(map identity [1 2 3])                        ; (1 2 3)    returns its argument
(remove (complement #{:a}) [:a :b])           ; (:a)

juxt turns several functions into one that returns a vector of all their results, which is handy for sorting by several keys and for building key-value pairs.

JS Stand-in: partial, comp and juxt

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Testing a whole collection

CLOJURE
(every? even? [2 4 6])           ; true
(some even? [1 3 4])             ; true   (returns the first truthy result)
(not-any? neg? [1 2 3])          ; true
(not-every? even? [2 3])         ; true
(some #{:b} [:a :b])             ; :b

Sorting and grouping with functions

CLOJURE
(sort [3 1 2])                           ; (1 2 3)
(sort > [3 1 2])                         ; (3 2 1)  with a comparator
(sort-by :age [{:age 30} {:age 20}])     ; ({:age 20} {:age 30})
(sort-by (juxt :last :first) people)     ; by last name, then first
(sort-by count > ["a" "ccc" "bb"])       ; ("ccc" "bb" "a")
(group-by even? (range 6))               ; {true [0 2 4], false [1 3 5]}
(partition-by neg? [-1 -2 3 4 -5])       ; ((-1 -2) (3 4) (-5))
(max-key count "a" "ccc" "bb")           ; "ccc"

Pick the right tool

map One result for each element.
filter / remove Keep or drop by predicate.
reduce Fold everything into one value.
partial / comp Build new functions.

Chain them to describe a whole process.

1 / 2

Putting it together

Find the total price of all expensive items in an order, using only these tools:

CLOJURE
(def items [{:name "pen" :price 2} {:name "desk" :price 150} {:name "lamp" :price 40}])

(reduce + (map :price (filter #(> (:price %) 30) items)))   ; 190

Read it from the inside out: filter expensive items, extract their prices, add them. The same pipeline reads even better with the threading macros from the next lesson.

JS Stand-in: filter, map and reduce pipeline

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Common mistakes

  • Forgetting that map and filter are lazy; side effects inside them may not run until the result is consumed (use doseq, or run!, for effects).
  • Using reduce with a function that takes the arguments in the wrong order: the accumulator comes first.
  • Writing (map #(+ % 1)) with no collection: that returns a transducer, not a result.
  • Using (map f coll) when you want a vector: use mapv.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does (reduce + 100 [1 2 3]) return?

    100 is the initial accumulator: 100 + 1 + 2 + 3 = 106.
  2. 2. In which order does comp apply its functions?

    (comp f g) calls g first and then f on the result.
  3. 3. What is ((partial * 2) 5)?

    partial fixes the first argument 2, so the call is (* 2 5), which is 10.
  4. 4. Which function keeps elements for which the predicate is falsey?

    remove is the opposite of filter.
  5. 5. What does (apply max [3 9 4]) do?

    apply spreads the collection into arguments.

Summary

  • map, filter, remove, keep and reduce replace most loops; map and filter are lazy.
  • reduce takes the accumulator first, an optional initial value, and can stop early with reduced.
  • apply spreads a collection into arguments.
  • partial, comp, juxt, complement and constantly build new functions from old ones.
  • every?, some, sort-by, group-by and partition-by handle testing and arranging collections.

Next: Composition and Threading Macros