On Thu, Sep 30, 2010 at 04:48, HiHeelHottie <hiheelhot...@gmail.com> wrote:

>
> Is there an idiomatic way to build up a string over different lines of
> code?  Or, should one simply use StringBuilder.
>
>
I recently wrote a program that generates complex java enums (as source)
from input data recorded in clojure syntax (by a VBA macro running in excel
...).

As java is syntactically correct, I quickly found that it was smart of break
up the algorithm into various functions that called each other.

A few examples (sketeched out from memory):

(defn enum-item-init [name args]
  [name "(" (interpose ", " (map enum-format-arg)) ")"])

(defn enum-items [item-map]
  [(interpose "," (map (fn [[name args]] (enum-item-init name args))
item-map)) ";"])

(defn enum-class [name item-map]
  ["public enum " name " {\n" (enum-items item-map) "\n}\n"])

I also used Clojure's multi-line string literals to good effect, though
that's now shown here.

So, really I'd invented a sort of poor-man's templating language within
Clojure.

So, I have each function return a sequence of strings. Some return vectors,
some return lazy sequences resulting from list comprehensions (for [...]
...). In the end, the top-level function returns a sort of seq of seq of seq
... of strings. So, a tree of strings really.

This worked well for me as a way of decomposing my program. Here's the
punch-line:

(apply str (flatten (enum-class name item-map)))

// Ben

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