Browse Clojure Foundations for Java Developers

Sequence Operations

Use map/filter/reduce and friends to build readable data pipelines; most sequence transforms are lazy.

Sequence operations are the Clojure equivalent of “collection pipelines” (similar in spirit to Java Streams), but they’re just ordinary functions—and they work across lists, vectors, sets, map entries, and more.

The most common shape is:

  • start with a collection
  • transform with map
  • select with filter
  • aggregate with reduce

A Typical Data Pipeline

 1(def orders
 2  [{:id 1 :paid? true  :total-cents 1200}
 3   {:id 2 :paid? false :total-cents 500}
 4   {:id 3 :paid? true  :total-cents 300}])
 5
 6(->> orders
 7     (filter :paid?)
 8     (map :total-cents)
 9     (reduce + 0))
10;; => 1500

Core Sequence Toolbox (Cheat Sheet)

Transform

  • map — apply a function to each element
  • mapcatmap + concat (useful for “expand each item into many”)
  • keep — like map, but drops nil results
  • filter / remove — keep or drop elements matching a predicate
  • some — returns the first truthy value produced by a predicate
  • every? — all elements satisfy a predicate?

Slice / group

  • take / drop — grab the first N / skip the first N
  • partition — chunk into fixed-size groups
  • group-by — build a map from a key-function to a collection of items

Aggregate

  • reduce — fold a sequence into a single value
  • frequencies — counts occurrences of items

Laziness: The One Behavior You Must Remember

Most sequence transforms (map, filter, remove, take, drop, etc.) are lazy: they do work only when someone consumes the result.

Rule of thumb for Java developers:

  • use sequence functions for pure transformations
  • avoid side effects inside map/filter
  • when you truly want “do this for effects,” use doseq or run!

Knowledge Check: Sequences

### What does this evaluate to? ```clojure (->> [1 2 3 4] (filter even?) (map #(* % 10))) ``` - [x] `(20 40)` - [ ] `[20 40]` - [ ] `40` - [ ] It throws because `map` can’t follow `filter`. > **Explanation:** `filter` and `map` return a (lazy) sequence. It prints as a list-like seq: `(20 40)`. ### What’s the biggest difference between `map` and `reduce`? - [x] `map` returns a sequence of results; `reduce` returns a single accumulated value. - [ ] `reduce` is lazy but `map` is eager. - [ ] `map` mutates the collection but `reduce` does not. - [ ] They are synonyms; pick whichever reads better. > **Explanation:** `map` transforms each element. `reduce` combines many elements into one value (sum, map merge, state machine, etc.). ### What does `group-by` return? - [x] A map from key → collection of items that share that key. - [ ] A vector of groups ordered by the key. - [ ] A set of unique keys. - [ ] A single reduced value. > **Explanation:** `group-by` partitions items by a key function and returns a map of groups.
Revised on Friday, April 24, 2026