Forked from sudowork/clojure-more-destructuring.clj
Last active
December 16, 2015 07:28
-
-
Save yangsu/5398407 to your computer and use it in GitHub Desktop.
Clojure Destructuring example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 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