Clojure applying a map and keyword arguments destruction

后端 未结 3 1892
生来不讨喜
生来不讨喜 2021-02-13 17:47

Consider a function with the following signature:

(defn make-widget [& {:keys [x y] :or {x 10 y 20}}]
 ...)

What is the best way to pass a

3条回答
  •  无人共我
    2021-02-13 18:27

    There is also a known (not invented by me at least), function "mapply":

    (defn mapply [f & args] (apply f (apply concat (butlast args) (last args))))
    

    which can be applied like

    (mapply your-function {:your "map"})
    

    As to why is this language-specific functionality absent from Clojure core, being implemented more natively and elegantly, no one could ever give me a clear answer.

    UPDATE

    After I have spent much time programming in Clojure, I personally tend to refrain from creating functions that accept a {} as a vararg. Although at first this can seem appealing, in reality experience proves that passing an explicit {} is always better for many reasons.

提交回复
热议问题