Skip to content

Instantly share code, notes, and snippets.

View nchapon's full-sized avatar

Nicolas Chapon nchapon

View GitHub Profile
@john2x
john2x / 00_destructuring.md
Last active June 6, 2024 13:40
Clojure Destructuring Tutorial and Cheat Sheet

Clojure Destructuring Tutorial and Cheat Sheet

(Related blog post)

Simply put, destructuring in Clojure is a way extract values from a datastructure and bind them to symbols, without having to explicitly traverse the datstructure. It allows for elegant and concise Clojure code.

Vectors and Sequences

;; nchapon's solution to A nil key
;; https://4clojure.com/problem/134
#(nil? (%1 %2 "empty"))
;; nchapon's solution to Count a Sequence
;; https://4clojure.com/problem/22
#(loop [coll % acc 0] (if (nil? (first coll)) acc (recur (rest coll) (inc acc))))