这是可行的,但我很好奇动态传入名称空间并使用ns-resolve解析它的性能代价是什么……
(ns bulbs.vertices)
(defn create
[config data]
((ns-resolve (:ns config) 'create-vertex) config data))然后这样叫它。
(ns bulbs.neo4jserver.graph
(:require [bulbs.vertices :as vertices])
(:require [bulbs.neo4jserver.client :as client]))
(defn graph
[& [config]]
(let [config (client/build-config config {:ns 'bulbs.neo4jserver.client})]
(fn [func & args]
(apply func config args))))
(def g (graph))
(g vertices/create {:name "James"})发布于 2012-05-14 07:32:38
除非您的ns-resolve是循环的一部分(除非您需要在每次迭代中动态解析不同的函数,否则绝对不需要这样做),否则我不会担心性能损失。
但是,确实存在性能损失:
user=> (time (dotimes [n 1000000] (let [f (ns-resolve 'clojure.core 'inc)] (f n))))
"Elapsed time: 175.386 msecs"
nil
user=> (time (dotimes [n 1000000] (let [f inc] (f n))))
"Elapsed time: 27.022 msecs"
nil如果你真的需要ns-resolve的魔力(但是在你的特殊情况下,你的other question回复不鼓励你这么做),并且如果你正在解析的函数是在循环中使用的,那么就把解析从这个循环中去掉:
user=> (time (let [f (ns-resolve 'clojure.core 'inc)] (dotimes [n 1000000] (f n))))
"Elapsed time: 48.538 msecs"
nilhttps://stackoverflow.com/questions/10575228
复制相似问题