Towards a better dir function in Clojure

Jason Whitlark —  March 18, 2015 — Leave a comment

This post originally appeared on Jason’s blog, Codeabout

Coming from Python, for the most part, I felt right at home in the Clojure REPL. However, one of my trustiest old tricks in the Python interpreter didn’t work nearly as well in Clojure.

I’m referring, of course, to the “what the hell was that called again?” fixer: dir.

In Python, dir will get you the fns/classes in a namespace, but the first major obstacle in clojure is the lack of support for namespace aliases.

Let’s fix that, shall we?

Looking at the source for the dir macro, we find:

Ok, this is really no more than a bit of sugar, to save us having to quote the namespace and to print the vars on their own line. We’ll have to dig deeper for some meat.

One thing I really love about Lisps: many times when I look under the hood, I’m expecting a monstrous Rube Goldberg machine, but what I find is some beautifully simple thing, that will still be pretty much the same thing in 50 years. (Go back and read some of the early algorithmic lisp code, it comes very easy.)

The first part of dir that bugs me is you must specify the full namespace, even if you’ve aliased it to something much simpler.

You can get aliases via ns-aliases, so…

Now we can…

But I’m not done with dir yet. Stay tuned for part 2.

Edit: I’ve filed a ticket for this functionality to be added to Clojure

Jason Whitlark

Jason Whitlark


Jason Whitlark is a software engineer on the BitTorrent DevApps team.