Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

clojure - How to list the functions of a namespace?

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)

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

(show 'clojure.contrib.repl-utils)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I normally call

(keys (ns-publics 'foo))

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 ...)

(the ... stands for quite a lot more).

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 -- 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.

  2. 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.

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

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...