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

Attachment: smime.p7s
Description: S/MIME cryptographic signature

Reply via email to