Ah! Thank you, Francis! And sorry for the belated reply!

I ran into a terse example of it at 
<https://clojure.org/guides/destructuring#_associative_destructuring> (see 
"(def options '(:debug true))") and was scratching my head. Now that you 
explained it though, I remembered seeing an explanation of it somewhere. 
Turns out I'd forgotten reading about that bit of magic in the "Clojure 
Programming" book (p.35).

-- John



On Friday, January 6, 2017 at 7:36:51 PM UTC-5, Francis Avila wrote:

> A list/seq (not a vector) which is destructured as a map will be first 
> read into a map as by (apply hash-map the-seq)
>
> This curiosity exists so the "keyword argument" idiom works:
>
> (defn my-options [_ & {:keys [a b] :as options}]
>   options)
> => #'user/my-options
> (my-options nil :a 1 :b 2 :c 3)
> => {:c 3, :b 2, :a 1}
>
> (my-options nil :a 1 :b 2 :c)
> IllegalArgumentException No value supplied for key: :c
>
> Keyword arguments have in general fallen out of favor (they were more 
> idiomatic in Clojure's early days), so you may not see this as often. 
> Generally people just pass an option map unless the function is meant 
> purely for "easy" interactive use (vs "simple" programmatic use). This is 
> because it's more difficult to call a keyword-argumented function correctly 
> when the keyword arguments are not literals.
>
> On Friday, January 6, 2017 at 4:54:52 PM UTC-6, John Gabriele wrote:
>>
>> I've used associative destructing in the usual fashion:
>>
>>     some-app.core=> (def m {:a 1 :b 2})
>>     #'some-app.core/m
>>
>>     some-app.core=> (let [{:keys [a b]} m] (str a "-" b))
>>     "1-2"
>>
>> but what is going on here:
>>
>>     some-app.core=> (def li '(:a 1 :b 2))
>>     #'some-app.core/li
>>
>>     ;; Wat?
>>     some-app.core=> (let [{:keys [a b]} li] (str a "-" b))
>>     "1-2"
>>
>> I didn't think lists were associative... As in:
>>
>>     some-app.core=> li
>>     (:a 1 :b 2)
>>
>>     some-app.core=> (associative? li)
>>     false
>>
>>     ;; So this is expected:
>>     some-app.core=> (let [{a 1 b 3} li] (str a "-" b))
>>     "-"
>>
>>     some-app.core=> (def v [:a 1 :b 2])
>>     #'some-app.core/v
>>
>>     ;; ...as is this (since vectors *are* associative):
>>     some-app.core=> (let [{a 1 b 3} v] (str a "-" b))
>>     "1-2"
>>
>>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to