gpt4 book ai didi

Clojure:如何获取函数的元数据?

转载 作者:行者123 更新时间:2023-12-03 00:40:23 25 4
gpt4 key购买 nike

我正在尝试获取所有内置 Clojure 函数的元数据。

previous question我了解到这可以使用类似 ^#'func_name (获取 var 对象的元数据)来实现。但我没能以编程方式做到这一点,因为事先不知道 func-name 。

例如尝试获取 clojure.core 中最后一个函数的元数据:

user=> (use 'clojure.contrib.ns-utils)
nil
user=> (def last-func (last (vars clojure.core)))

user=> last-func
zipmap

;The real metadata (zipmap is hardcoded)
user=> ^#'zipmap
{:ns #<Namespace clojure.core>, :name zipmap, :file "clojure/core.clj", :line 1661, :arglists ([keys vals]), :doc "Returns a map .."}

;Try to get programmatically, but get shit
user=> ^#'last-func
{:ns #<Namespace user>, :name last-func, :file "NO_SOURCE_PATH", :line 282}

如何才能做到呢?我已经尝试了多种变体,但没有任何效果。

最佳答案

您正在寻找metans-resolve

user=> (let [fun "map"] (meta (ns-resolve 'clojure.core (symbol fun))))
{:ns #<Namespace clojure.core>, :name map, :file "clojure/core.clj", :line 1705, :arglists ([f coll] [f c1 c2] [f c1 c2 c3] [f c1 c2 c3 & colls]), :doc "Returns a lazy sequence consisting of the result of applying f to the\n set of first i tems of each coll, followed by applying f to the set\n of second items in each coll, until any one of the colls is\n exhausted. Any remaining items in other colls are ignored. Function\n f should accept number-of-colls arguments."}

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com