Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure namespace: method to see defined objects?

Tags:

clojure

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?

like image 614
Shane Avatar asked Jan 06 '10 11:01

Shane


2 Answers

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}
like image 94
aatifh Avatar answered Sep 22 '22 07:09

aatifh


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.

like image 23
Carl Smotricz Avatar answered Sep 23 '22 07:09

Carl Smotricz