Skip to content

Instantly share code, notes, and snippets.

Last active August 29, 2015 14:15
What would you like to do?
[TIx 6] View source code in a Clojure REPL

This is one of those things that was right in front of my face, but I figure it's worth a mention. I was trying to figure out the best way to view the source code for Clojure's frequencies function — ideally while in a REPL. I found this older post mentioning a source function in the clojure.contrib.repl-utils namespace. I launched a REPL to see if I could use it and, well, here's what I saw:

$ lein repl
nREPL server started on port 58700 on host - nrepl://
REPL-y 0.3.5, nREPL 0.2.6
Clojure 1.6.0
Java HotSpot(TM) 64-Bit Server VM 1.7.0_45-b18
    Docs: (doc function-name-here)
          (find-doc "part-of-name-here")
  Source: (source function-name-here)
 Javadoc: (javadoc java-object-or-class-here)
    Exit: Control+D or (exit) or (quit)
 Results: Stored in vars *1, *2, *3, an exception in *e


It says it right there: Source: (source function-name-here). It can't be any more obvious than that.

user=> (source frequencies)
(defn frequencies
  "Returns a map from distinct items in coll to the number of times
  they appear."
  {:added "1.2"
   :static true}
   (reduce (fn [counts x]
             (assoc! counts x (inc (get counts x 0))))
           (transient {}) coll)))

Haven't figured out a replacement for clojure.contrib.repl-utils/show, although this Stack Overflow thread seems to point in the right direction.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment