tags:

views:

223

answers:

2

Is there some way to see what has already been defined in a clojure session (equivalent to calling ls())? Let's say that I create a few objects:

(def x 1)
(def y 2.2)
(def plus-one (fn [x] (+ x 1)))

Is there a command that can be run to show me that these now exist in the user namespace?

+5  A: 

Maybe ns-publics ?

Returns a map of the public intern mappings for the namespace.

or ns-map ?

Returns a map of all the mappings for the namespace.


As I understand it, there is no "global environment," there are only namespaces. Of course whichever one you are currently "in" looks like a "global environment" for practical purposes.

Carl Smotricz
+1 Thanks! That's very helpful.
Shane
+12  A: 

I am doing all the assignments in user namespace.

user> (def *foo 10) 
#'user/*foo

;; To see all the public intern mappings in the user namespace.

user> (ns-publics 'user)
{*foo #'user/*foo}

Now let's define a function which is not public

user> (defn- foobar[x]
        (println x)
#'user/foobar

When you call ns-publics function. It will not show foobar function in the mappings.

user> (ns-publics 'user)
{*foo #'user/*foo}

To see the intern mappings for the namespace. Use (ns-interns 'YOUR-NAMESPACE)

user> (ns-interns 'user)
{foobar #'user/foobar, *foo #'user/*foo}
aatifh
Oh, so that's what `ns-interns` is for! +1 but I'm out of votes ;)
Carl Smotricz