且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

字符串名称中的clojure解析函数

更新时间:2022-03-11 05:06:40

这对我有效,无需使用eval:

This worked for me without using eval:

user> (defn mycar [x] (first x))
#'user/mycar
user> ((resolve (symbol "mycar")) [1 2 3])
1

之所以可行,是因为resolves在当前名称空间中找到了mycar var,并且var调用了它所绑定的函数.这是第一个示例的简短版本.我只是为了避免使用eval而使用它.

This works because resolves finds the mycar var in the current namespace and the var calls the function it's bound to. This is a shorter version of your first example. I'd use it just so that I could avoid using eval.