Skip to content

Instantly share code, notes, and snippets.

@yangsu
Forked from sudowork/clojure-more-destructuring.clj
Last active December 16, 2015 07:28
Show Gist options
  • Save yangsu/5398407 to your computer and use it in GitHub Desktop.
Save yangsu/5398407 to your computer and use it in GitHub Desktop.
Clojure Destructuring example
; First let's show two alternative ways to destructure a vector
user=> (defn first-three [a & [b & [c]]] (println a b c))
#'user/first-three
user=> (defn first-three [a b c & _] (println a b c))
#'user/first-three
user=> (first-three 1 2 3 4)
1 2 3
nil
user=> (first-three 1 2)
1 2 nil
nil
; Let's destructure a tic-tac-toe board
user=> (def board [[:x :o :x] [:o :x :o] [:x :o :x]])
#'user/board
user=> (defn center "Get center mark" [[_ [_ c _] _]] c)
#'user/center
user=> (defn center "Get center mark" [[_ [_ c]]] c) ; we don't need to specify the entire structure
#'user/center
user=> (center board)
:x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment