81

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?

StackedCrooked
  • 34,653
  • 44
  • 154
  • 278

4 Answers4

104

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.

Michał Marczyk
  • 83,634
  • 13
  • 201
  • 212
  • Is the availability of these functions and the `doc` function a reason why providing documentation for old libraries and other packages is not a particularly high priority? – Kenny Evitt Oct 26 '13 at 16:36
72

You can use dir. (Perhaps this wasn't available when the question was first asked.)

user=> (dir clojure.string)
blank?
capitalize
escape
join
lower-case
re-quote-replacement
replace
replace-first
reverse
split
split-lines
trim
trim-newline
triml
trimr
upper-case
nil
Mars
  • 8,689
  • 2
  • 42
  • 70
  • 1
    to notice: somehow in clojure 1.4 `(dir *ns*)` does not work, `Exception No namespace: *ns* found clojure.core/the-ns (core.clj:3691)` – xealits Mar 17 '16 at 15:20
  • 1
    @xeslits, I'm sure you're right. If you follow the link to the `dir` documentation in my answer, there's notice in one corner indicating that `dir` was introduced in Clojure 1.6. At the time that I posted the answer, maybe I should have included that information. At this point, v. 1.5 is already three versions behind the current release. Although there may be people who have good reasons to use 1.5 or even 1.4, I think those cases are rare. – Mars Mar 18 '16 at 04:25
  • 3
    @xealits @Mars This has nothing to do with the version. You just need to provide the actual symbol of the namespace: `(dir my-actual-namespace)` works fine in Clojure 1.8 while I get the `No namespace: *ns*` error if I try `(dir *ns*)`. – Resigned June 2023 Jul 28 '16 at 15:48
  • 3
    returning to this question and answer again: notice, [`dir` returns only "public" vars](https://clojuredocs.org/clojure.repl/dir), `ns-map` is for everything. – xealits Sep 18 '16 at 21:25
6

Have a look here. More specifically:

;; Sometimes I like to ask which public functions a namespace provides.
(defn- ns-publics-list [ns] (#(list (ns-name %) (map first (ns-publics %))) ns))
Bozhidar Batsov
  • 55,802
  • 13
  • 100
  • 117
0

For those who use of Counterclockwise, there is a "Namespace Browser" that is shown by default.

If not visible, it can be shown via menu option:

Window > Show View > Namespace Browser

Official documentation: http://doc.ccw-ide.org/documentation.html#_namespace_browser_view

Excerpt from official documentation:

The Namespace Browser View displays all symbols of all namespaces of the active REPL.[2]. It allows you to jump to the definition of symbols in the relevant files (including inside jars): just double-click on the symbol name in the Namespace Browser View.