Clojure:判断函数是否存在
我如何知道作为字符串提供的函数名称在当前上下文中是否可调用?类似:
(callable? "asdasd") ;; false
(callable? "filter") ;; true
谢谢
how can i know if a function name provided as string is callable or not in the current context? something like:
(callable? "asdasd") ;; false
(callable? "filter") ;; true
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在寻找resolve,
返回nil
return #'clojure.core/filter
要检查var是否是一个函数(归功于@amalloy):
You are looking for resolve,
returns nil
return #'clojure.core/filter
To check if a var is a function (credit goes to @amalloy):
如果你需要这个,很可能你做错了什么,但是......
Chances are if you need this, you're doing something wrong, but...
UPD。我发现
fn?
仅检查接口Fn
并且不适用于已解析的符号。不过,clojure.test/function?
可以满足需要,所以我更新了一个示例。UPD. I found out that
fn?
checks only for interfaceFn
and doesn't work for resolved symbol. Though,clojure.test/function?
does what is needed, so I updated an example.