Clojure Seq Abstraction — One Interface for Every Collection | Episode 11
0views
C
CelesteAI
Description
You've learned vectors, lists, maps, and sets. Now one abstraction ties them all together — the sequence. In Clojure, seq converts any collection into a uniform interface. Learn first and rest once, and they work on everything.
In this episode we call seq on all five types, use first and rest across collections, and write a single function that handles vectors, lists, and strings with the same code.
Student code: https://github.com/GoCelesteAI/clojure-for-beginners/tree/main/episode11
Every keystroke is shown on screen with generous pauses so you can follow along at your own pace.
What You'll Learn:
- seq converts any collection to a sequence (or nil if empty)
- first and rest work on vectors, lists, maps, sets, and strings
- Maps become sequences of key-value pairs
- Strings become sequences of characters
- seq on empty returns nil — the idiomatic emptiness test
- Writing polymorphic functions that work on any seqable type
Timestamps:
0:00 - Intro
0:12 - Preview: one interface for everything
0:32 - Start the REPL
0:40 - seq on vectors, lists, maps, sets, strings
1:11 - seq on empty returns nil
1:17 - first and rest on different types
1:45 - Exit REPL
1:50 - Write seqs.clj in Neovim
2:12 - seq section — all five types
3:25 - first and rest section
4:20 - describe-coll — one function, every type
5:42 - Run with :!clj -M %
5:46 - Output explained line by line
6:42 - Review
6:52 - Recap
7:27 - What's next: Episode 12
Key Takeaways:
1. seq converts any collection to a sequence. first and rest work on all of them.
2. Maps become pairs, strings become characters — everything is seqable.
3. seq on empty returns nil — use (when (seq coll) ...) as the idiomatic emptiness test.
4. Write one function and it handles every collection type — that's the power of the seq abstraction.
Next up — Episode 12: Lazy Sequences.
Taught by CelesteAI. Like and subscribe for more Clojure tutorials!