The First Production Clojure Macro

What kind of legacy will I leave my shop when I have gone? We have custom software written in 4GL, C, Perl, Python, and Clojure. Not taking into account any generational/age differences, to a new chief technology officer it might seem bad enough to have any custom written software at all, let alone written in a Lisp dialect. Who knows, maybe Clojure will not be frightening by then.

After working on a finite state machine parser written in Bliss-32 macro, I vowed not to write any Clojure macro just to say I could. Unlike Clojure’s macros, Bliss macros were a language within a language, perhaps analogous to Scheme’s macros, so I achieved specialized knowledge never to be used again. Clojure’s language facilities are so good that up until today (nearly two years of use), I had no reason to write a macro. That is, until today.

One of the things I like about Clojure is that with a little bit of work, a programmer can control things at compile time, that is not possible in other languages. I started to write the following macro as a function to check to see the value of dbg. Then I asked, what if any var is not defined? How do I get around that? The result is a simple wrapper. I’ll write something more glorious and complicated when the need arises.


(def dbg 1)

(defmacro chk-flagM
 "Throws an exception if flag does not resolve; else returns flag's value."
 [flag]
 (if (not (resolve flag))
  (throw (Exception. (str 'flag " is not a valid var.")))
  flag))

(defn write-csv-file
 "Writes a csv file using a key and an s-o-s"
 [out-sos out-file]

 (if (>= (chk-flagM dbg) 2)
  (println (first out-sos), "n", out-file))

 (spit out-file "" :append false)
 (with-open [out-data (io/writer out-file)]
   (csv/write-csv out-data (map #(concat % [""]) out-sos))))
Advertisements

Leave a comment

Filed under Clojure

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s