Skip to content


Subversion checkout URL

You can clone with
Download ZIP
73 lines (63 sloc) 2.74 KB
;;; strint.clj -- String interpolation for Clojure
;; originally proposed/published at
;; by Chas Emerick <>
;; December 4, 2009
;; Copyright (c) Chas Emerick, 2009. All rights reserved. The use
;; and distribution terms for this software are covered by the Eclipse
;; Public License 1.0 (
;; which can be found in the file epl-v10.html at the root of this
;; distribution. By using this software in any fashion, you are
;; agreeing to be bound by the terms of this license. You must not
;; remove this notice, or any other, from this software.
^{:author "Chas Emerick",
:doc "String interpolation for Clojure."}
(defn- silent-read
"Attempts to clojure.core/read a single form from the provided String, returning
a vector containing the read form and a String containing the unread remainder
of the provided String. Returns nil if no valid form can be read from the
head of the String."
(let [r (-> s]
[(read r) (slurp r)])
(catch Exception e))) ; this indicates an invalid form -- the head of s is just string data
(defn- interpolate
"Yields a seq of Strings and read forms."
([s atom?]
(if-let [[form rest] (silent-read (subs s (if atom? 2 1)))]
(cons form (interpolate (if atom? (subs rest 1) rest)))
(cons (subs s 0 2) (interpolate (subs s 2))))))
([^String s]
(if-let [start (->> ["~{" "~("]
(map #(.indexOf s %))
(remove #(== -1 %))
(lazy-seq (cons
(subs s 0 start)
(interpolate (subs s start) (= \{ (.charAt s (inc start))))))
(defmacro <<
"Takes a single string argument and emits a str invocation that concatenates
the string data and evaluated expressions contained within that argument.
Evaluation is controlled using ~{} and ~() forms. The former is used for
simple value replacement using clojure.core/str; the latter can be used to
embed the results of arbitrary function invocation into the produced string.
user=> (def v 30.5)
user=> (<< \"This trial required ~{v}ml of solution.\")
\"This trial required 30.5ml of solution.\"
user=> (<< \"There are ~(int v) days in November.\")
\"There are 30 days in November.\"
user=> (def m {:a [1 2 3]})
user=> (<< \"The total for your order is $~(->> m :a (apply +)).\")
\"The total for your order is $6.\"
Note that quotes surrounding string literals within ~() forms must be
`(str ~@(interpolate string)))
Jump to Line
Something went wrong with that request. Please try again.