KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
In Clojure, if I have a function f , (defn f [& r] ... ) and I have a seq args with the arguments I want to call f with, I can easily use apply : (apply f args) Now, say I have another function g , which is designed to take any of a number of optional, named arguments - that is, where the rest argument is destructured as a map: (defn g [& {:keys [a b] :as m}] ... ) I'd normally call g by doing something like (g :a 1 :b 2) but if I happen to have a map my-map with the value {:a 1 :b 2}, and I want to "apply" g to my-map - in other words, get something that would end up as the above call, then I naturally couldn't use apply, since it would be equivalent to (g [:a 1] [:b 2]) Is there a nice way to handle this? May I have gone off track in my design to end up with this? The best solution I can find would be (apply g (flatten (seq my-map))) but I surely don't like it. Any better solutions? EDIT: A slight improvement to the suggested solution might be (apply g (mapcat seq my-map)) which at least removes one function call, but it may still not be very clear what's going on.
Tags (comma-separated)
Save Edits
Cancel