Hi all,
I've got a couple questions regarding rest parameters.
1) In function params, is there a way to destructure the rest argument as a
map and bind it to a name at once in the function signature?
Below is an example. f1 takes opts parameter. Now I have f2 with similar
signature (but different default for b) and it may decide to pass the opts
argument to f1, but I need to call flatten & seq to pass it.
Another way is with f3 where I destructure in let, but my fn signature no
longer showed me what I could have in opts.
Is there a way to combine f2 & f3, say like f4? It seems like if I
destructure a rest argument, I cannot bind a name to it anymore?
(let [f1 (fn [a & {:keys [b] :or {b 'f1-def} :as opts}] [a b opts])
f2 (fn [a & {:keys [b] :or {b 'f2-def} :as opts}] (apply f1 a
(flatten (seq opts))))
f3 (fn [a & opts] (let [{:keys [b] :or {b 'f3-def}} opts] (apply f1 a
opts)))
#_(f4 (fn [a & {:keys [b] :or {b 'f4-def}} opts] (apply f1 a opts)))]
(println (f2 1 :b 'hi :c 'f2))
(println (f2 1))
(println (f3 2 :b 'hi :c 'f3))
(println (f3 2)))
This gives:
[1 hi {:c f2, :b hi}]
[1 f1-def nil]
[2 hi {:c f3, :b hi}]
[2 f1-def nil]
2) My f2 & f3 above is incorrect (wrong default is applied). Calling (f2 1)
gave [1 f1-def nil] instead of [1 f2-def nil]. So does this mean the opts
binding is applied before the default is assoced-in? What's the idiomatic &
correct way to handle this?
Thanks all in advance,
Sindu
--
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.