使用函数’;从字符串调用函数;Clojure的名字
如何使用字符串调用函数?e、 g.类似这样的情况:使用函数’;从字符串调用函数;Clojure的名字,clojure,Clojure,如何使用字符串调用函数?e、 g.类似这样的情况: (call "zero?" 1) ;=> false 比如: (defn call [^String nm & args] (when-let [fun (ns-resolve *ns* (symbol nm))] (apply fun args))) 简单的回答是: (defn call [this & that] (apply (resolve (symbol this)) that))
(call "zero?" 1) ;=> false
比如:
(defn call [^String nm & args]
(when-let [fun (ns-resolve *ns* (symbol nm))]
(apply fun args)))
简单的回答是:
(defn call [this & that]
(apply (resolve (symbol this)) that))
(call "zero?" 1)
;=> false
只是为了好玩:
(defn call [this & that]
(cond
(string? this) (apply (resolve (symbol this)) that)
(fn? this) (apply this that)
:else (conj that this)))
(call "+" 1 2 3) ;=> 6
(call + 1 2 3) ;=> 6
(call 1 2 3) ;=> (1 2 3)
谢谢,
ns resolve
是我特别想要的。把ns resolve和fn结合起来更好?要检查,该符号是function,但在clojurescript中不起作用。