Clojure for Beginners: Clojure Sets — Unique Collections & Set Math
Video: Clojure Sets — Unique Collections & Set Math | Episode 10 by CelesteAI
Take the quiz on the full lesson page
Test what you've read · interactive walkthrough
Clojure Sets — Unique Collections & Set Math
Sets guarantee every element is unique. They double as membership functions. And clojure.set gives you real set math — union, intersection, and difference — built right into the standard library.
Code
;; Episode 10: Sets
;; Clojure for Beginners in Neovim
;; Create a set — unique elements only
(def colors #{:red :green :blue})
(println "Colors:" colors)
;; conj adds, disj removes
(println "Add yellow:" (conj colors :yellow))
(println "Remove red:" (disj colors :red))
;; Duplicates are ignored
(println "Add red again:" (conj colors :red))
;; Membership — sets are functions of their elements
(println "Has blue?" (colors :blue))
(println "Has pink?" (colors :pink))
(println "Contains?" (contains? colors :green))
;; Set operations with clojure.set
(require '[clojure.set :as set])
(def frontend #{:html :css :js :react})
(def backend #{:java :python :js :sql})
(println "Union:" (set/union frontend backend))
(println "Intersection:" (set/intersection frontend backend))
(println "Frontend only:" (set/difference frontend backend))
Key Points
Watch the video above for a full walkthrough — every keystroke is shown so you can code along.
Student code: GitHub
Ready? Take the quiz on the full lesson page →
Test what you've learned. Watch the lesson and try the interactive quiz on the same page.