2
votes

Is there a way to get a dump of all the source code I have entered into a repl session. I have created a bunch of functions using (defn ...) but did it 'on the fly' without entering them in a text file (IDE) first.

Is there a convenience way to get the source back out of the repl session?

I note that:

(dir user)

will give me a printed list of type:

user.proxy$java.lang.Object

so I can't appear to get that printed list into a Seq for mapping a function like 'source' over. And even if I could then:

(source my-defined-fn)

returns "source not found"...even though I personally entered it in to the repl session.

Any way of doing this? Thanks.

1
Possible duplicate of stackoverflow.com/questions/3782970/…dsm
not a duplicate, that function is asking about using the REPL to print the source of a function defined in a .clj file. This question is about printing the source of a function not defined in any file (at least if I'm reading this right)Arthur Ulfeldt
@Arthur Ulfeldt, Fair enoughdsm

1 Answers

0
votes

Sorry, but I suspect the answer is no :-/

The best you get is scrolling up in the repl buffer to where you defined it. The source function works by looking in the var's metadata for the file and line number where the functions code is (or was last time it was evaluated), opening the file, and printing the lines. It looks like this:

...
(when-let [filepath (:file (meta v))]
  (when-let [strm (.getResourceAsStream (RT/baseLoader) filepath)]
    (with-open [rdr (LineNumberReader. (InputStreamReader. strm))]
      (dotimes [_ (dec (:line (meta v)))] (.readLine rdr))
...

Not including the full source in the metadata was done on purpose to save memory in the normal case, though it does make it less convenient here.