我正在将此 Java 函数复制到 Clojure 中。
Config createConfig(Map<String, String> options) {
Config conf = new Config();
String foo = options.get("foo");
if (foo != null) { conf.setFoo(foo); }
String bar = options.get("bar");
if (bar != null) { conf.setBar(bar); }
// many other configs
return conf;
}
我想出了这个,
(defn create-config [options]
(let [conf (Config.)]
(when-let [a (:foo options)] (.setFoo a))
(when-let [a (:bar options)] (.setBar a))
conf))
有更好的方法吗?
最佳答案
这个怎么样?
(defn create-config [{:keys (foo bar)}]
(let [config (Config.)]
(when foo (.setFoo config foo))
(when bar (.setBar config bar))))
可能更好的是创建一个通用宏,它可以基于 Clojure 映射在对象上设置任意数量的字段。这可能更漂亮,但它有效:
(defmacro set-fields
[options obj]
(let [o (gensym)
m (gensym)]
`(let [~m ~options
~o ~obj]
~@(map (fn [[field value]]
(let [setter (symbol (str "set" (clojure.string/capitalize (name field))))]
`(. ~o (~setter ~value))))
options)
~o)))
;; call like:
(set-fields {:daemon true :name "my-thread"} (Thread.))
;; translates to:
;; (let [G__1275 {:daemon true, :name "my-thread"}
;; G__1274 (Thread.)]
;; (. G__1274 (setDaemon true))
;; (. G__1274 (setName "my-thread"))
;; G__1274)
我不检查 null ,因为这仅查看选项中传递的内容。
关于java - 在 Clojure 中执行此 Java 函数的惯用方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27308061/