Skip to content

Instantly share code, notes, and snippets.

@O-I
Last active August 29, 2015 14:15
Show Gist options
  • Save O-I/96a0baca5a06ef95fa5e to your computer and use it in GitHub Desktop.
Save O-I/96a0baca5a06ef95fa5e to your computer and use it in GitHub Desktop.
[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 127.0.0.1 - nrepl://127.0.0.1:58700
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

user=>

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}
  [coll]
  (persistent!
   (reduce (fn [counts x]
             (assoc! counts x (inc (get counts x 0))))
           (transient {}) coll)))
nil

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