On 8 February 2010 12:45, Roman Roelofsen
<roman.roelof...@googlemail.com> wrote:
> Hi,
>
> just for practicing Clojure's macros I wrote the following
> create-java-list macro.
>
> (defmacro create-java-list
>  [& forms]
>  (let [prefixfn (fn [obj form] (cons (symbol ".") (cons obj form)))
>        lname (gensym)]
>    `(let [~lname (java.util.ArrayList.)]
>       ~@(map (partial prefixfn lname) forms)
>       ~lname)))
>
> The idea is that you can write
>
> (let [javalist (create-java-list
>                (add "1")
>                (add "2"))]
>  (prn javalist))
>
> instead of
>
> (let [javalist (java.util.ArrayList.)]
>  (.add javalist "1")
>  (.add javalist "2")
>  (prn javalist))

Try this:

user=> (doto (java.util.ArrayList.)
(.add "1")
(.add "2"))
#<ArrayList [1, 2]>

> Is it possible to create the macro without the (gensym) call? I wasn't
> able to use something like lname#.

Sorry, I'm not sure why that doesn't work.

-- 
Michael Wood <esiot...@gmail.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

Reply via email to