如何在ClojureScript中使用命名参数?

时间:2012-01-03 10:43:03

标签: clojure clojurescript

在clojure中我可以使用defnk来获取命名参数。如何在ClojureScript中实现相同的功能?

1 个答案:

答案 0 :(得分:10)

ClojureScript中的命名args功能与Clojure中相同:

(defn f [x & {:keys [a b]}] 
  (println (str "a is " a " and b is " b)))

(f 1)
; a is  and b is 

(f 1 :a 42)
; a is 42 and b is 

(f 1 :a 42 :b 108)
; a is 42 and b is 108

如果您需要默认值,请将原件更改为:

(defn f [x & {:keys [a b] :or {a 999 b 9}}]
  (println (str "a is " a " and b is " b)))

(f 1)
; a is 999 and b is 9

这与Clojure - named arguments

的好答案有关