Writing code to get the source of a function

93 views
Skip to first unread message

Samuel Lê

unread,
Aug 8, 2012, 5:19:15 AM8/8/12
to clo...@googlegroups.com
Dear all,

I am trying to write some code that would take a function name, get its source code, and create a new function based on the source code. Unfortunately, the function 'source' from clojure.repl doesn't seem to be working for the functions I define.
Here is my code:

(ns test-src.core
  (:require [clojure.repl]))

(defn my-function [x]
  (+ x 1))

(defn print-src []
  (println (clojure.repl/source my-function)))

When I try print-src on the repl, I get: 
test-src.core> (print-src)
Source not found
nil
nil

So my question is: how can I access to the source code of the functions I write?

Many thanks,
Sam

Moritz Ulrich

unread,
Aug 8, 2012, 8:54:17 AM8/8/12
to clo...@googlegroups.com
The source function only works for function where the .clj where the
function is defined is in the classpath. If you have control over all
functions, I'd suggest using
https://github.com/technomancy/serializable-fn when defining them.
> --
> 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

Joshua Ballanco

unread,
Aug 8, 2012, 9:25:35 AM8/8/12
to clo...@googlegroups.com
On Wed, Aug 08, 2012 at 09:19:15AM +0000, Samuel Lê wrote:
> Dear all,
>
> I am trying to write some code that would take a function name, get its
> source code, and create a new function based on the source code.
> Unfortunately, the function 'source' from clojure.repl doesn't seem to be
> working for the functions I define.
> Here is my code:
>
> (ns test-src.core
> (:require [clojure.repl]))
>
> (defn my-function [x]
> (+ x 1))
>
> (defn print-src []
> (println (clojure.repl/source my-function)))
>

Try:

(defn print-src []
(println (with-out-str (clojure.repl/source my-function))))

The "source" method is designed for the REPL, and so dumps to *out* by
default (you can confirm this yourself, appropriately enough, by doing
"(source source)")

Cheers,

Josh



--
Joshua Ballanco

ELC Technologies™
1771 NW Pettygrove Street, Suite 140
Portland, OR, 97209
jbal...@elctech.com

P +1 866.863.7365
F +1 877.658.6313
M +1 646.463.2673
T +90 533.085.5773

http://www.elctech.com

Alan Malloy

unread,
Aug 8, 2012, 3:18:19 PM8/8/12
to clo...@googlegroups.com
(println (with-out-str (foo))) is silly - it's the same as (do (foo) nil), which in many cases (eg, in this one) is the same as just (foo).

Samuel Lê

unread,
Aug 8, 2012, 6:26:40 PM8/8/12
to clo...@googlegroups.com
Using serializable works fine for me. I find its code very instructive as well. Thanks!

--
Reply all
Reply to author
Forward
0 new messages