Chuyển đến nội dung

Bài 22 · Sequences and Laziness

Sequence Functions in Practice

A practical tour of Clojure's sequence library: map, filter, reduce, for, partition, group-by, frequencies, sort-by, mapcat, into and threading pipelines with real examples.

Clojure's standard library has well over a hundred functions for working with sequences. You do not need to memorize them, but you do need to know the families: transform, select, combine, group, order and generate. Once you can name the family you want, finding the function is easy. This lesson walks through the ones you will use daily, with a running example of order data.

The examples are read-only Clojure; the runnable blocks mirror the same ideas in JavaScript. Everything here is built on the sequence abstraction and is lazy unless noted.

Sample data

CLOJURE
(def orders
  [{:id 1 :customer "ana"  :total 40  :items [:pen :book]}
   {:id 2 :customer "ben"  :total 15  :items [:pen]}
   {:id 3 :customer "ana"  :total 100 :items [:lamp]}
   {:id 4 :customer "cleo" :total 60  :items [:book :lamp]}])

Transform: map, mapcat, keep, for

map applies a function to each element, and with several collections it walks them in step:

CLOJURE
(map :total orders)              ;=> (40 15 100 60)
(map + [1 2 3] [10 20 30])       ;=> (11 22 33)
(mapcat :items orders)           ;=> (:pen :book :pen :lamp :book :lamp)
(keep #(when (> (:total %) 50) (:id %)) orders)   ;=> (3 4)

mapcat is "map then flatten one level". keep is "map, then drop the nil results". Keywords act as functions, which is why (map :total orders) works.

The for macro is a list comprehension: it takes bindings, optional :when, :let and :while modifiers, and returns a lazy sequence. Nested bindings give a cross product, with the rightmost binding changing fastest.

CLOJURE
(for [x [1 2 3] y [:a :b]] [x y])
;=> ([1 :a] [1 :b] [2 :a] [2 :b] [3 :a] [3 :b])

(for [o orders :when (> (:total o) 30) :let [t (:total o)]]
  (str (:customer o) ":" t))
;=> ("ana:40" "ana:100" "cleo:60")

Select: filter, remove, take, drop, some

CLOJURE
(filter #(> (:total %) 30) orders)   ; keep matching
(remove #(> (:total %) 30) orders)   ; drop matching
(take 2 orders) (drop 2 orders)
(take-while pos? [3 2 0 5])          ;=> (3 2)
(drop-while pos? [3 2 0 5])          ;=> (0 5)
(some #(when (= (:customer %) "cleo") %) orders)  ; first truthy result
(every? :id orders)                  ;=> true

some returns the first truthy value its predicate produces (not just true), which makes it a "find" operation. To get the first matching item use (first (filter pred coll)).

Combine: reduce and its relatives

reduce folds a collection into one value using a two-argument function and an optional starting value:

CLOJURE
(reduce + (map :total orders))              ;=> 215
(reduce (fn [acc o] (assoc acc (:id o) (:total o))) {} orders)
;=> {1 40, 2 15, 3 100, 4 60}
(reductions + [1 2 3 4])                    ;=> (1 3 6 10)   running totals
(apply max (map :total orders))             ;=> 100

reductions returns every intermediate step, which is perfect for running totals. apply spreads a collection as arguments, so (apply + [1 2 3]) is (+ 1 2 3). Prefer reduce with a start value when the collection might be empty: (reduce + 0 []) gives 0.

Group: group-by, frequencies, partition

CLOJURE
(group-by :customer orders)
;=> {"ana" [{...order 1} {...order 3}], "ben" [{...}], "cleo" [{...}]}

(frequencies (mapcat :items orders))
;=> {:pen 2, :book 2, :lamp 2}

(partition 2 [1 2 3 4 5])        ;=> ((1 2) (3 4))        drops the leftover
(partition-all 2 [1 2 3 4 5])    ;=> ((1 2) (3 4) (5))
(partition-by even? [2 4 1 3 6]) ;=> ((2 4) (1 3) (6))

group-by returns a map from the key function's result to a vector of items. Combine it with update-vals to summarize each group:

CLOJURE
(update-vals (group-by :customer orders)
             #(reduce + (map :total %)))
;=> {"ana" 140, "ben" 15, "cleo" 60}

Order: sort, sort-by, reverse

CLOJURE
(sort [3 1 2])                         ;=> (1 2 3)
(sort > [3 1 2])                       ;=> (3 2 1)
(sort-by :total orders)                ; ascending by total
(sort-by :total > orders)              ; descending
(sort-by (juxt :customer :total) orders) ; by customer, then total

sort is stable and works on anything comparable. juxt builds a function that returns a vector of several results, so vectors compare element by element and give you multi-key sorting for free.

Build and convert: into, vec, set, zipmap, distinct

CLOJURE
(into [] (map :id) orders)             ;=> [1 2 3 4]  (transducer form)
(set (map :customer orders))           ;=> #{"ana" "ben" "cleo"}
(zipmap [:a :b :c] [1 2 3])            ;=> {:a 1, :b 2, :c 3}
(distinct [1 2 1 3 2])                 ;=> (1 2 3)
(interleave [1 2 3] [:a :b :c])        ;=> (1 :a 2 :b 3 :c)
(concat [1 2] [3] [4 5])               ;=> (1 2 3 4 5)
(flatten [1 [2 [3]]])                  ;=> (1 2 3)

Use into when you need a particular collection type at the end. Use flatten sparingly, since it flattens everything, including nested data you wanted to keep; mapcat is usually more precise.

Putting it together with ->>

The thread-last macro ->> feeds the result of each step in as the last argument of the next, so pipelines read top to bottom:

CLOJURE
(->> orders
     (filter #(> (:total %) 20))
     (group-by :customer)
     (map (fn [[name os]] [name (count os)]))
     (sort-by second >)
     (into []))
;=> [["ana" 2] ["cleo" 1]]

If you need the value in a different position, use as->, or restructure so each function takes the collection last (the convention for sequence functions).

Try it: the same pipeline in JavaScript

JS Group, total and rank

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

No output.

Try it: reductions, partition and for

JS Running totals, chunks, comprehensions

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

No output.

Common mistakes

  • Forgetting that results are sequences, not vectors: use vec, into or mapv/filterv when you need indexing.
  • reduce without an initial value on an empty collection calls the function with no arguments; give it a start value.
  • Using map for side effects; use doseq or run! (see laziness).
  • partition silently dropping the tail; use partition-all.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. What is the difference between partition and partition-all?

    (partition 2 [1 2 3]) gives ((1 2)); (partition-all 2 [1 2 3]) gives ((1 2) (3)).
  2. 2. Which returns running totals?

    reductions returns every intermediate accumulator value, such as (1 3 6 10) for + over [1 2 3 4].
  3. 3. What does (group-by :customer orders) return?

    group-by builds a map whose keys are the results of the key function and whose values are vectors of the matching items.
  4. 4. How do you sort descending by :total?

    sort-by takes an optional comparator before the collection: (sort-by key-fn comparator coll).

Summary

  • Think in families: transform (map, mapcat, keep, for), select (filter, take-while, some), combine (reduce, reductions, apply), group (group-by, frequencies, partition-all), order (sort-by), build (into, zipmap).
  • ->> threads the collection through a readable top-to-bottom pipeline.
  • Results are lazy sequences; convert with into, vec or set when you need a concrete type.
  • Prefer partition-all, mapcat and reduce with an initial value to the surprising alternatives.

Next: Identity, State and Atoms

Theo dõi tiến độ của bạn

Tạo tài khoản miễn phí để đánh dấu bài đã học và tiếp tục từ chỗ bạn dừng lại trên mọi thiết bị.