using clojure symbol function to make indirect function call

橙三吉。 提交于 2019-12-18 03:03:15

问题


I'm expecting below that I should be able to call my function squared indirectly via the symbol function, but its not working. What am I doing wrong here:

user=> (defn squared [x] (* x x))
#'user/squared
user=> (squared 2)
4
user=> ((symbol "squared") 2)
nil
user=> ((symbol "user" "squared") 2)
nil
user=> 

回答1:


The symbol itself does not hold your function, the Var it names does.

This says "take the contents (@ = deref) of the Var (resolve) named by the symbol (symbol) whose name is "squared" and apply it (as a function) to the argument 2:

(@(resolve (symbol "squared")) 2)

This says "take the Var named by the symbol etc.":

((resolve (symbol "squared")) 2)

Both will work, since Vars, when asked to act as a function, defer to the functions stored in them. (If you try to use a Var as a function when it is not bound to a function, an error will result.)

This says "take the symbol named "squared" and apply it as a function to the argument 2" -- note that the symbol itself is used as the function:

((symbol "squared") 2)

Now symbols can be used as functions (they implement the clojure.lang.IFn interface), but the way they act when used in this way is that they look themselves up in their argument, i.e. treat their argument as a map and perform a lookup inside it:

('foo {'foo 2})
; is equivalent to
(get {'foo 2} 'foo)

If the argument passed to a symbol is not something it makes sense to do lookups in, nil is returned.



来源:https://stackoverflow.com/questions/7354814/using-clojure-symbol-function-to-make-indirect-function-call

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!