clojure - 如何获取 clojure 函数参数的元数据?

标签 clojure metadata

有没有办法在clojure中为函数的参数获取元数据?答案发布in this question实际上,通常不起作用:

user> (defn foo "informative dox!" [] 1)
#'user/foo
user> (defmacro get-docs [func] `(:doc (meta (var ~func))))
#'user/get-docs
user> (get-docs foo)
"informative dox!"
user> (get-docs (identity foo))
; Evaluation aborted.
user> (defn process-docs [f] (let [docs (get-docs f)] (reverse docs)))
; Evaluation aborted.

倒数第二行不起作用,因为您无法调用 var在名单上(identity foo) ,最后一行甚至没有编译,因为编译器提示无法解析 f .

我发现这个问题的大多数解决方案都依赖于您可以访问函数定义中的符号或类似的想法,以便您可以执行类似 (resolve 'f) 的操作。或 (var f) .但是我想要一些我可以在函数的参数上使用的东西,你不知道这些信息。

本质上,我想要一个表达式来代替下面的问号,以获取 #'map 的元数据。 :
(let [x map] (??? x))

最佳答案

尽管可能,但它是满口的:

(let [x map]
   (:doc (meta (second (first (filter #(and (var? (second %))
                                            (= x (var-get (second %))))
                                      (ns-map *ns*)))))))

产生所需的结果:
"Returns a lazy sequence consisting of the result of applying f to the
 set of first items of each coll, followed by applying f to the set
 of second items in each coll, until any one of the colls is\n  exhausted.  Any remaining items in other colls are ignored. Function
 f should accept number-of-colls arguments."

在引擎盖下命名空间本质上是名称到变量的映射,变量包含函数。您可以在这些 var 的内容中搜索与您正在寻找的函数匹配的内容,然后查看它的关联 var 并从该 var 中获取元数据。

关于clojure - 如何获取 clojure 函数参数的元数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12432561/

相关文章:

clojure - 这个 `doseq`语句和 `for`语句有什么区别;在 Clojure 中读取文件?

clojure - 惯用的 Clojure 中的上周编号和对应年份?

ios - 如何在 iOS 中访问 JPEG COM 段?

jquery - 使用 jQuery 元数据 - 无法读取 HTML5 数据属性

clojure - 如何在 Clojure 中正确读取 RandomAccessFile?

r - Clojure/Incanter 中的快速向量数学

clojure - 在Clojure中,为什么要使用:only []

android - FFmpeg 去除视频旋转信息

android - 如何从 android.intent.action.SEND 获取图像细节?

file - 文件系统存储其文件元数据的位置