On Mon, Oct 27, 2008 at 11:38 PM, Islon <[EMAIL PROTECTED]> wrote: > Is there any chance closure will get string interpolation? > > Do things like (prn "Hi ${someone}, my name is ${myname}") is nice, not > crucial of course, but nice.
I'm personally not fond of string interpolation either. But for fun, here's an (i ...) macro, that will give you ${} interpolation in strings (if it works at all, I test it very thorougly!). (defn my-interleave [a b] "Like interleave, but uses all elements from both lists." (loop [acc [] a a b b] (if (and (nil? a) (nil? b)) acc (let [acc2 (if (nil? a) acc (conj acc (first a))) acc3 (if (nil? b) acc2 (conj acc2 (first b)))] (recur acc3 (rest a) (rest b)))))) (defn read-from-string [s] (read (java.io.PushbackReader. (java.io.StringReader. s)))) (defn tokenize [s] ;; not pretty but it works. (let [positions (let [mm (re-matcher #"\\$\\{.*?\\}" s)] (loop [acc []] (if (.find mm) (recur (conj acc [(.start mm) (.end mm)])) acc))) intermed (conj (apply vector 0 (apply concat positions)) (.length s)) textposns (partition 2 intermed)] (my-interleave (map (fn [[a b]] [:text (.substring s a b)]) textposns) (map (fn [[a b]] [:pat (.substring s (+ a 2) (- b 1))]) positions)))) (defmacro i [s] (apply list 'str (map (fn [[type value]] (if (= type :text) value (read-from-string value))) (tokenize s)))) ;; test (let [greeting "Hello" name "Fred" age 33] (prn (i "${greeting}, my name is ${name} and my age is ${age}."))) -- Graham --~--~---------~--~----~------------~-------~--~----~ 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 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 -~----------~----~----~----~------~----~------~--~---