Re: Writing code to get the source of a function

2012-08-08 Thread Samuel Lê
Using serializable works fine for me. I find its code very instructive as
well. Thanks!

On Wed, Aug 8, 2012 at 7:18 PM, Alan Malloy  wrote:

> (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).
>
>
> On Wednesday, August 8, 2012 6:25:35 AM UTC-7, Joshua Ballanco wrote:
>
>> On Wed, Aug 08, 2012 at 09:19:15AM +, 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
>>
>  --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Re: Writing code to get the source of a function

2012-08-08 Thread Alan Malloy
(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).

On Wednesday, August 8, 2012 6:25:35 AM UTC-7, Joshua Ballanco wrote:
>
> On Wed, Aug 08, 2012 at 09:19:15AM +, 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 
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Re: Writing code to get the source of a function

2012-08-08 Thread Joshua Ballanco
On Wed, Aug 08, 2012 at 09:19:15AM +, 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
jballa...@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

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Writing code to get the source of a function

2012-08-08 Thread Moritz Ulrich
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.

On Wed, Aug 8, 2012 at 11:19 AM, 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)))
>
> 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
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Writing code to get the source of a function

2012-08-08 Thread Samuel Lê
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

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en