These inconsistencies are bugging me, I just wondered if I was missing some
standard language thing about argument passing or destructuring, so I'm
looking for comments.
At issue (for me), keyword arguments and maps that bundle them.
user> (defn foo [a b & {:keys [c d]}]
[a b c d])
#'user/foo
user> (defn bar [a b & {:keys [c d] :as options}]
(println (apply foo a b options)))
#'user/bar
user> (bar 1 2)
[1 2 nil nil]
nil
;; Okay so far
user> (bar 1 2 :c 3)
IllegalArgumentException No value supplied for key: [:c 3]
clojure.lang.PersistentHashMap.create (PersistentHashMap.java:77)
;; I'm wishing the above was smarter, but okay, so I use 'mapply', which
seems to me it ought to be in clojure.core but isn't.
user> (defn mapply
[f & args]
(apply f (apply concat (butlast args) (last args))))
#'user/mapply
user> (defn bar2 [a b & {:keys [c d] :as options}]
(println (mapply foo a b options)))
#'user/bar2
user> (bar2 1 2 :c 3)
[1 2 3 nil]
nil
;; Okay, that works nicely
So maybe this is all fine, though if there's some standard way of doing
this using things shipped with clojure please let me know.
However then 'recur' bucks the trend, which adds to confusion.
user> (defn baz [a b & {:keys [c d] :as options}]
(if (> a 0)
(recur (- a 1) b options)
[a b c d]))
#'user/baz
user> (baz 2 3 :c 4)
[0 3 4 nil]
So recur does this arguably very useful thing, but apply does not (and
probably with good reason, otherwise how would we pass maps as regular
arguments...)
I guess what I'm seeking is the canonical clojure approach to dealing with
keyword arguments and maps in call sites, and I find
the apply/recur difference confusing, and the lack of 'mapply' an oversight.
Thoughts? Comments? Please educate on what I'm missing, thanks.
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
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 [email protected].
For more options, visit https://groups.google.com/d/optout.