Hi,

2009/6/9 Josh  Smith <[email protected]>:
>
> 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

I second Parth concerning the usage of macros (don't worry, we all did
this when first exposed to lisp - aka macros everywhere- :-).

But looking at your code, I think I understand your problem. You
wanted to be able to pass around java method names as if they were
first class functions, but indeed, you can't do that at runtime.
So you resorted to write macros instead of functions + runtime
reflective information.

Still, I'm not sure it's worth to use macros in this case. I would
better have used java reflection. Or maybe just done it a little bit
differently. Indeed, you do not really have independent code / low
coupling between the m_load_properties macro and the load_propfile and
load_xml_propfile functions, because the 2 must know exactly the
method names of java.util.Properties to be called.

So maybe you could try this which provides the same not-so-low
coupling between m_load_properties and the 2 functions (note that I've
convertd the code into something that fits more the clojure/lisp style
by using hyphens instead of underscores) :

(defn load-properties
  [filename load-method]
  (with-open [ifst (new java.io.FileInputStream filename)]
    (let [prop (new java.util.Properties)]
      (load-method prop ifst)
      (let [keysseq (enumeration-seq (. prop propertyNames))]
        (reduce (fn [a b] (assoc a b (. prop getProperty b))) {} keysseq)))))

(defn load-propfile
  "given a filename that contains text formated as a Java properties
   file, this function load that file and returns a map of the
   key/value pairs"
  [filename]
  (load-properties filename (fn [props stream] (.load props stream))))

(defn load-xml-propfile
  "Same as load_propfile, except with an xml formated file instead of
a plain text file"
  [filename]
  (load-properties filename (fn [props stream] (.loadFromXML props stream))))

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

Beware that thread safe is different from immutable. Properties class
is not immutable (you can call the load methods several times on
different files for the same instance), so I think is not a good
candidate as a general purpose "clojure type".

Ah, and BTW, I didn't see anywhere in the javadoc where it is
mentioned that Properties is thread safe ?


HTH,

-- 
Laurent

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to