Hi,

On Feb 8, 11:45 am, Roman Roelofsen <roman.roelof...@googlemail.com>
wrote:

> 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))
>
> Is it possible to create the macro without the (gensym) call? I wasn't
> able to use something like lname#.

You can use ->.

(defmacro create-java-list
  [& forms]
  (let [predot (fn [form] (cons (symbol ".") form))]
    `(let [lname# (java.util.ArrayList.)]
       (-> lname#
         ~@(map predot forms)))))

which is maybe better written as

(let [jl (-> (java.util.ArrayList.)
           (.add "1")
           (.add "2"))]
  (prn jl))

I don't see a compelling reason for such a macro. At least not in this
simple case.

Sincerely
Meikel

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