Displaying source of function typed into the REPL?

211 views
Skip to first unread message

Sean Corfield

unread,
Sep 18, 2010, 5:39:19 PM9/18/10
to clo...@googlegroups.com
OK, this feels like a really dumb question...

I'm playing around in the REPL, I type in a function, I use it and
continue to work on other stuff... I can't remember what the function
looked like and I want to display the source of it again...

I know I can go back through the REPL history but maybe I typed it in
ages ago or maybe I typed it on multiple lines so it's hard to piece
together from the history. That seems like hard work.

I know I can go directly to the .jline-clojure.main.history file in my
home directory. That seems like cheating (and it means I have to jump
out of the REPL and hunt thru the file).

I know I can use (source sym) to get the source of something whose
.clj is on the classpath - that doesn't work for stuff typed directly
into the REPL.

Is there something easy within the REPL to show the source of
something you defined earlier?
--
Sean A Corfield -- (904) 302-SEAN
Railo Technologies, Inc. -- http://getrailo.com/
An Architect's View -- http://corfield.org/

"If you're not annoying somebody, you're not really alive."
-- Margaret Atwood

Stuart Sierra

unread,
Sep 19, 2010, 9:54:54 AM9/19/10
to Clojure
On Sep 18, 5:39 pm, Sean Corfield <seancorfi...@gmail.com> wrote:
> Is there something easy within the REPL to show the source of
> something you defined earlier?

No. The REPL does not preserve what you type in.

-S

Moritz Ulrich

unread,
Sep 19, 2010, 10:48:29 AM9/19/10
to clo...@googlegroups.com
I think this would be a very nice extension for the repl - Saving the
forms for later-retrieval.

> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@googlegroups.com
> Note that posts from new members are moderated - please be patient with your first post.
> To unsubscribe from this group, send email to
> clojure+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en

--
Moritz Ulrich
Programmer, Student, Almost normal Guy

http://www.google.com/profiles/ulrich.moritz
BB5F086F-C798-41D5-B742-494C1E9677E8

Steven E. Harris

unread,
Sep 19, 2010, 11:45:00 AM9/19/10
to clo...@googlegroups.com
Moritz Ulrich <ulrich...@googlemail.com> writes:

> I think this would be a very nice extension for the repl - Saving the
> forms for later-retrieval.

As precedent, the Common Lisp reader uses the +, ++, and +++ variables¹
to store the last three forms read.


Footnotes:
¹ http://www.lispworks.com/documentation/HyperSpec/Body/v_pl_plp.htm

--
Steven E. Harris

Stephen C. Gilardi

unread,
Sep 19, 2010, 1:24:39 PM9/19/10
to clo...@googlegroups.com
On Sep 19, 2010, at 11:45 AM, Steven E. Harris wrote:

> Moritz Ulrich <ulrich...@googlemail.com> writes:
>
>> I think this would be a very nice extension for the repl - Saving the
>> forms for later-retrieval.
>
> As precedent, the Common Lisp reader uses the +, ++, and +++ variables¹
> to store the last three forms read.

An early draft of the current clojure repl included a feature like that. At the time it was judged not sufficiently useful to keep.

The clojure repl is very customizable. It's possible to add this feature in a custom repl to experiment with the idea:

(declare r1 r2 r3)

(defn saving-repl-read [request-prompt request-exit]
(let [r (clojure.main/repl-read request-prompt request-exit)]
(cond (#{request-prompt request-exit} r) r
(#{'+ '++ '+++} r) `(quote ~({'+ r1 '++ r2 '+++ r3} r))
:else (do (set! r3 r2) (set! r2 r1) (set! r1 r) r))))

(binding [r1 nil r2 nil r3 nil]
(clojure.main/repl :read saving-repl-read))

-------------

user=> 1
1
user=> (/ 22 7)
22/7
user=> (java.util.UUID/randomUUID)
#<UUID fd91090a-8336-4a32-91e2-2c49e114177a>
user=> ++
(/ 22 7)
user=>

-------------

Following the clojure repl's precedent of *1 *2 and *3, these might be better named +1 +2 and +3 if the idea gets any traction.

--Steve

Sean Corfield

unread,
Sep 19, 2010, 3:36:32 PM9/19/10
to clo...@googlegroups.com
On Sun, Sep 19, 2010 at 10:24 AM, Stephen C. Gilardi <sque...@mac.com> wrote:
> The clojure repl is very customizable. It's possible to add this feature in a custom repl to experiment with the idea:

Awesome. I'll see if I can generalize that to keep a longer history
and a nice easy way to view / retrieve past entries so I can see how
useful I'd actually find it in real life...

Hozumi

unread,
Sep 19, 2010, 8:29:40 PM9/19/10
to Clojure
If you use emacs, incremental back search is handy.
Typing "C-r defn", you may jump to the defnition of a function.
Next time, you need to type only "C-r C-r".

-- Takahiro Hozumi
On 9月19日, 午前6:39, Sean Corfield <seancorfi...@gmail.com> wrote:
> OK, this feels like a really dumb question...
>
> I'm playing around in the REPL, I type in a function, I use it and
> continue to work on other stuff... I can't remember what the function
> looked like and I want to display the source of it again...
>
> I know I can go back through the REPL history but maybe I typed it in
> ages ago or maybe I typed it on multiple lines so it's hard to piece
> together from the history. That seems like hard work.
>
> I know I can go directly to the .jline-clojure.main.history file in my
> home directory. That seems like cheating (and it means I have to jump
> out of the REPL and hunt thru the file).
>
> I know I can use (source sym) to get the source of something whose
> .clj is on the classpath - that doesn't work for stuff typed directly
> into the REPL.
>
> Is there something easy within the REPL to show the source of
> something you defined earlier?
> --
> Sean A Corfield -- (904) 302-SEAN
> Railo Technologies, Inc. --http://getrailo.com/
> An Architect's View --http://corfield.org/

jlk

unread,
Sep 19, 2010, 9:11:47 PM9/19/10
to Clojure
Not sure how practical it is, but a while back I was playing around
with a macro redefining defn so that it stored the function source in
the meta-data of the function. I can't find it now but remember it
being fairly trivial to implement.

- lachlan

Sean Corfield

unread,
Sep 19, 2010, 11:00:07 PM9/19/10
to clo...@googlegroups.com
That also sounds pretty useful for development. I may try that as an exercise...

This weekend has been a journey through The Joy of Clojure. I read
about 100 pages on Friday evening (easy going), 150 pages on Saturday
(harder going - more complex topics). Today I read another 50 pages,
skipped chapters 11 & 12 (brain too full to process - about 80 pages)
and read chapter 13 (about 20 pages). All the time I had a REPL open
and kept copy'n'pasting examples in and playing with them to get a
feel for it. It's been a really fun weekend! Now, unfortunately, I
must get chores (and work!) done before the week starts.

See some of you at the JavaOne Clojure meetup tomorrow (Monday)?

Emeka

unread,
Sep 20, 2010, 2:09:23 PM9/20/10
to clo...@googlegroups.com
Sean,

Try this, http://gist.github.com/193550 . Adjust  it to your taste.


Regards,
Emeka

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en



--
Satajanus  Nig. Ltd



Sean Corfield

unread,
Sep 20, 2010, 2:50:39 PM9/20/10
to clo...@googlegroups.com
On Mon, Sep 20, 2010 at 11:09 AM, Emeka <emeka...@gmail.com> wrote:
> Try this, http://gist.github.com/193550 . Adjust  it to your taste.

So I'd say (file-repl "console.txt") and everything typed in and
printed out would be written to that file for the REPL session?

That would be a bit like the .jline-clojure.main.history file (but
would include all output as well).


--
Sean A Corfield -- (904) 302-SEAN

Railo Technologies, Inc. -- http://getrailo.com/
An Architect's View -- http://corfield.org/

Reply all
Reply to author
Forward
0 new messages