Clojure Threading Macros — thread-first and thread-last | Episode 15
0views
C
CelesteAI
Description
Nested function calls read inside-out. That's backwards. Threading macros flip the flow so you read left-to-right, top-to-bottom — the way you think about the data moving through each step.
In this episode we cover the nested mess, thread-last for sequence pipelines, thread-first for map updates, and the rule of thumb for picking between them based on API shape.
Student code: https://github.com/GoCelesteAI/clojure-for-beginners/tree/main/episode15
Every keystroke is shown on screen with generous pauses so you can follow along at your own pace.
What You'll Learn:
- Why nested calls are hard to read
- Thread-last — perfect for map, filter, reduce
- Thread-first — perfect for assoc, update, get
- Picking between them by API shape (coll-last vs coll-first)
- Rewriting Episode 14's nested pipeline as a single threaded expression
Timestamps:
0:00 - Intro
0:12 - Preview: pipelines, not nesting
0:32 - Start the REPL
0:40 - Nested vs threaded: reverse, drop, take
0:48 - Thread-last
1:06 - Thread-first
1:18 - Exit REPL
1:23 - Write threading.clj in Neovim
1:46 - Nested mess section
2:23 - Thread-last section
4:22 - Thread-first section
5:38 - Ep14 pipeline, threaded
7:32 - Run with clj -M
7:36 - Output walked through
8:32 - Review
8:36 - Recap
9:11 - What's next: Episode 16
Key Takeaways:
1. Threading macros turn nested calls into readable pipelines.
2. Thread-last passes the value as the LAST argument — use it for sequence transforms.
3. Thread-first passes the value as the FIRST argument — use it for map and string transforms.
4. Pick by API shape: if the function takes the collection last, reach for thread-last.
Phase 3 continues. Next up, Episode 16 — a deep dive on `reduce`.
Taught by CelesteAI. Like and subscribe for more Clojure tutorials!