Thanks Steve! That's very neat. Pretty much a "canonical" macro example.

Adrian.

On Tue, Jun 2, 2009 at 5:50 AM, Stephen C. Gilardi <squee...@mac.com> wrote:
> Here's a macro I've found useful for loading and running Clojure programs
> from the REPL:
>
>  (defmacro run
>    "Loads the specified namespace and invokes its \"main\"
>    function with optional args. ns-name is not evaluated."
>    [ns-name & args]
>    `(do
>       (require '~ns-name :reload-all)
>       ((ns-resolve '~ns-name '~'main) ~...@args)))
>
> An example namespace that works with it (in hello.clj at a classpath root):
>
>  (ns hello)
>
>  (defn main
>    [& args]
>    (apply println "hi" args))
>
> and a REPL session:
>
>  user=> (run hello)
>  hi
>  nil
>  user=> (run hello :clojure "is" \#
>           (.gcd (bigint 1169687) (bigint 311791)))
>  hi :clojure is # 1
>  nil
>  user=>
>
> I think the quoting on "main" in the ns-resolve call is quite interesting.
> macroexpand-ing variations on it is a heap of educational Clojure macro-fu
> fun!
>
> --Steve
>
>

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to