我想创建一个工厂,在clojure
中,创建者的参数数量在运行时会发生变化。在Clojure中,如何编写具有可变数量参数的工厂
例如:
(defn create-long-document [direction]
(str "long document " direction))
(defn create-short-document[]
"short document")
(def creator-map {
:english create-short-document
:hebrew create-long-document
})
(def additional-arg-map {
:english nil
:hebrew "rtl"
})
(defn create-document [language]
(let [creator (language creator-map) arg (language additional-arg-map)]
(if arg (creator arg) (creator))))
(println (create-document :hebrew)); long document rtl
(println (create-document :english)); short document
我找改写的create-document
身体的优雅方式。我想摆脱if
。也许通过引入智能宏?
请分享你的想法。
没有必要允许呼叫者提供特定的额外的参数。在我的情况下,所有额外的参数必须从调用者隐藏。 – viebel 2012-02-13 10:44:34