Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think you are hit by reflection. <code>*warn-on-reflection*</code> is your friend. Here some tests with <a href="http://hugoduncan.org/post/2010/benchmarking_clojure_code_with_criterium.xhtml" rel="nofollow">criterium</a>.</p> <pre><code>replace-templates-original: 56.4us replace-templates-original-hinted: 9.4us replace-templates-new: 131.4us replace-templates-new-hinted: 6.3us replace-templates-very-new: 7.3us </code></pre> <p>Here is the <code>replace-templates-very-new</code>, a version I did myself for golf. :)</p> <pre><code>(defn replace-templates-very-new [^String text m] (let [builder (StringBuilder.)] (loop [text text] (cond (zero? (count text)) (.toString builder) (.startsWith text "{") (let [brace (.indexOf text "}")] (if (neg? brace) (.toString (.append builder text)) (do (.append builder (get m (keyword (subs text 1 brace)))) (recur (subs text (inc brace)))))) :else (let [brace (.indexOf text "{")] (if (neg? brace) (.toString (.append builder text)) (do (.append builder (subs text 0 brace)) (recur (subs text brace))))))))) </code></pre> <p>It passes all tests, so it should work.</p> <p><strong>UPDATE</strong>: Support non-key brace enclosed values (<code>"this is a {not-a-key-{foo}-in-the-map} test" =&gt; "this is a {not-a-key-FOO-in-the-map} test"</code>), allowing it to be used in a Java code generator where non-key brace-enclosed things are significant :-).</p> <pre><code>(defn replace-templates-even-newer "Return a String with each occurrence of a substring of the form {key} replaced with the corresponding value from a map parameter. @param str the String in which to do the replacements @param m a map of keyword-&gt;value @thanks kotarak http://stackoverflow.com/questions/6112534/ follow-up-to-simple-string-template-replacement-in-scala-and-clojure" [^String text m] (let [builder (StringBuilder.)] (loop [text text] (cond (zero? (count text)) (.toString builder) (.startsWith text "{") (let [brace (.indexOf text "}")] (if (neg? brace) (.toString (.append builder text)) (if-let [[_ replacement] (find m (keyword (subs text 1 brace)))] (do (.append builder replacement) (recur (subs text (inc brace)))) (do (.append builder "{") (recur (subs text 1)))))) :else (let [brace (.indexOf text "{")] (if (neg? brace) (.toString (.append builder text)) (do (.append builder (subs text 0 brace)) (recur (subs text brace))))))))) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload