clojure - Applying a map to a function's rest argument -


in clojure, if have function f,

(defn f [& r] ... ) 

and have seq args arguments want call f with, can use apply:

(apply f args) 

now, have function g, designed take of number of optional, named arguments - is, rest argument destructured map:

(defn g [& {:keys [a b] :as m}] ... ) 

i'd call g doing like

(g :a 1 :b 2) 

but if happen have map my-map value {:a 1 :b 2}, , want "apply" g my-map - in other words, end above call, naturally couldn't use apply, since equivalent to

(g [:a 1] [:b 2]) 

is there nice way handle this? may have gone off track in design end this? best solution can find be

(apply g (flatten (seq my-map))) 

but surely don't it. better solutions?

edit: slight improvement suggested solution might be

(apply g (mapcat seq my-map)) 

which @ least removes 1 function call, may still not clear what's going on.

i have stumbled problem myself , ended defining functions expect 1 map. map can have variable amount of key/value pairs, , if flexible enough, there no need & rest arguments. there no pain apply. makes life lot easier!

(defn g [{:keys [a b] :as m}] ... ) 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -