Avi Drucker / Apr 12 2021 / Published
Remix of Clojure by Nextjournal
Clojure Koans 5 Sets Notebook
Today's notebook is on Clojure sets :) So, get ready, get set! Clojure Koan Spoilers ahead... Go!
{:deps {org.clojure/clojure {:mvn/version "1.10.1"}
;; complient is used for autocompletion
;; add your libs here (and restart the runtime to pick up changes)
compliment/compliment {:mvn/version "0.3.9"}}}
deps.edn
Extensible Data Notation
{:hello (clojure-version)}
0.1s
Clojure
;; "You can create a set by converting another collection"
(= {3} (set (3)))
;; skill 5001: convert a list or vector into a set using the `set` function
Clojure
;; "Counting them is like counting other collections"
(= 3 (count {1 2 3}))
;; skill 5002: Find the quantity of items in a collection by using the `count` function
;; skill 5003: Find the quantity of items in a set by using the `count` function
Clojure
;; "Remember that a set is a *mathematical* set"
(= {1 2 3 4 5} (set (1 1 2 2 3 3 4 4 5 5)))
;; skill 5004: Remove duplicates from a collection by converting it to a set
Clojure
;; "You can ask clojure for the union of two sets"
(= {1 2 3 4 5} (set/union {1 2 3 4} {2 3 5}))
;; skill 5005: Combine (join? unite?) two sets into one using the `clojure.set/union` function
Clojure
;; "And also the intersection"
(= {2 3} (set/intersection {1 2 3 4} {2 3 5}))
;; skill 5006: Intersect two sets using the `clojure.set/intersection` function
Clojure
;; "But don't forget about the difference"
(= {1 4} (set/difference {1 2 3 4 5} {2 3 5})))
;; skill 5007: Get the difference of two sets using the `clojure.set/difference` function
Clojure
(wrap-up)
What are your thoughts on sets in Clojure? Can you spot any skills that I didn't list?
I see that I've not listed any questions here. I'd be interested to see some useful set-related exercises, quiz questions, or puzzles.
Till tomorrow ~