On Jun 9, 11:50 am, Josh  Smith <kogn...@gmail.com> wrote:
> I've been trying to understand the macro syntax in clojure.  I think
> macro's are closer to the kind of functionality I'm looking for (as
> opposed to multi-methods) but I'm not sure.
>
> http://gist.github.com/126315
>

Hi Josh,

As a general recommendation, its better to use functions
instead of macros as functions provide a lot of flexibility
(e.g. you can apply them). The thumb rule I use to use a
macro only if I can't do something with a function.

user=> (defmacro double-mac [x] `(* ~x ~x))
#'user/double-mac
user=> (double-mac 2)
4
user=> (defn double-fn [x] (* x x))
#'user/double-fn
user=> (map double-fn [1 2 3])
(1 4 9)
user=> (map double-mac [1 2 3])
java.lang.Exception: Can't take value of a macro: #'user/double-mac
(NO_SOURCE_FILE:8)
user=>

Another minor comment, Lisp style programming
usually stacks up parenthesis at the end rather than
put them in separate lines like C or Java.
E.g.
(defn foo [coll]
   (map inc  coll))

rather than
(defn foo [coll]
  (map inc coll)
)


> I wrote the above code, which simply processes Java-style Properties
> files (using the java interfaces) as
> a learning exercise.  It works, but I'm still hazy about exactly why
> it works.
>

I don't know java too well so I can't really talk much about
properties file. :(

There are some functions dealing with properties in
clojure.contrib.java-utils. They might be good for use/reference
in case you haven't seen them already.

http://code.google.com/p/clojure-contrib/wiki/JavaUtilsApiDoc


> I spent a lot of time figuring out the escape/not escape syntax.  What
> do you recommend as a starting place for understanding what needs to
> be escaped, and what doesn't?  Is that even the right question?
> What's the best way to learn the proper care and feeding of macros?
> Is there another/better way of passing a method name to a function as
> an argument than via a macro?
>

I found PCL and OnLisp very useful to understand macros.

http://gigamonkeys.com/book/
http://www.paulgraham.com/onlisp.html

Both are available online. You could just look at the chapters
relevant to macros. Though these books deal with Common Lisp
the same principles hold and the macro system is similar
(in clojure you don't need gensym as often).

> Another question I have is that the Properties class is listed as
> being thread safe.  Does that mean
> it can safely be used in the multi-threaded Clojure app?
>
> Lastly,  is there any way to turn a string into a :thing?  For
> example,  I've got "stow" and I want a function
> that returns :stow.
>

The :thing are called keywords in Lisp/Clojure and are very
useful. I think what you are looking for is:

user=> (keyword "stow")
:stow

Regards,
Parth

> Thank you


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