如何列出命名空间的功能? [英] How to list the functions of a namespace?

查看:16
本文介绍了如何列出命名空间的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何列出 Clojure 命名空间的所有函数.我已经做了一些研究,但我还没有到那里.我已经找到了如何使用 show 方法列出 Java 类的方法:

I would like to know how to list all functions of a Clojure namespace. I've done some research but I'm not there yet. I already found out how to list the methods of a Java class using the show method:

(show java.awt.Graphics)

为了列出 Clojure 命名空间的功能,我尝试了如下所示的 show 方法:

To list the functions of a Clojure namespace I've tried the show method like this:

(show 'clojure.contrib.repl-utils)

这显示了一些 Java 类方法,但不是我正在寻找的那些,例如 docshow.我怎样才能得到这些?

This shows some Java class methods, but not the ones I'm looking for like doc and show. How can I get those?

推荐答案

我通常打电话

(keys (ns-publics 'foo))

列出命名空间foo导出的Vars;例如对于 clojure.contrib.monads 返回

to list Vars exported by the namespace foo; e.g. for clojure.contrib.monads this returns

(defmonad censor m-when-not m+write+m maybe-m maybe-t ...)

(... 代表更多).

更一般地说,有一堆函数的名称以 ns- 开头,它们按命名空间列出 Vars,并附加了某些附加条件:

More generally, there's a bunch of functions whose names start in ns- which list Vars by namespace, with certain additional criteria attached:

  1. ns-map -- 最通用的函数,返回一个以符号为键的映射(实际上是非命名空间限定的符号),其中每个符号对应的值是符号在给定命名空间中解析为的变量或类.

  1. ns-map -- the most general function of all, returns a map keyed by symbols (non-namespace-qualified symbols actually), where the value corresponding to each symbol is the Var or class that symbol resolves to in the given namespace.

ns-interns -- 与 ns-map 类似,但只包含在给定命名空间中的变量(而不是来自其他命名空间的变量,它们是由于 userefer 调用或来自 clojure.core 的 Vars 的隐式引用,可以从给定的命名空间访问.

ns-interns -- like ns-map, but includes only the Vars interned in the given namespace (as opposed to Vars from other namespaces which are accessible from the given namespace due to a use or refer call or the implicit referral of Vars from clojure.core.

ns-publics -- 类似于 ns-interns,但只包括非私有变量.

ns-publics -- like ns-interns, but includes only the non-private Vars.

ns-imports -- 与 ns-map 类似,但只包括其值对应于 Java 类的条目.

ns-imports -- like ns-map, but includes only the entries whose values correspond to Java classes.

还有 ns-aliases 列出了在从其他命名空间引用 Vars 时可以用作速记别名的符号;例如如果您调用 (require '[clojure.contrib.math :as math])ns-aliases 将包含一个带有 math(符号),其值将是实际的命名空间 clojure.contrib.math.这些映射不包含在 ns-map 返回的映射中.

There's also ns-aliases which lists symbols which can be used as shorthand aliases when referring to Vars from other namespaces; e.g. if you call (require '[clojure.contrib.math :as math]), ns-aliases will include an entry with the key of math (the symbol), whose value will be the actual namespace clojure.contrib.math. These mapping are not included in the map returned by ns-map.

这篇关于如何列出命名空间的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆