Created
December 9, 2015 08:23
-
-
Save dainiusjocas/d18d636334d2fa97a18b to your computer and use it in GitHub Desktop.
Multi-level clojure map destructuring
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
(def contact | |
{:firstname "John" | |
:lastname "Smith" | |
:age 25 | |
:contacts {:phone "+44.123.456.789" | |
:emails {:work "jsmith@company.com" | |
:personal "jsmith@some-email.com"}}}) | |
;; Just the top level | |
(let [{lastname :lastname} contact] | |
(println lastname )) | |
;; Smith | |
;; One nested level | |
(let [{lastname :lastname | |
{phone :phone} :contacts} contact] | |
(println lastname phone)) | |
;; Smith +44.123.456.789 | |
(let [{:keys [firstname lastname] | |
{:keys [phone] } :contacts} contact] | |
(println firstname lastname phone )) | |
;; John Smith +44.123.456.789 | |
(let [{:keys [firstname lastname] | |
{:keys [phone] | |
{:keys [work personal]} :emails } :contacts} contact] | |
(println firstname lastname phone work personal)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment