Will Roe’s blog

A variety of geekery » Archives

Fast String Interpolation in ClojureScript

In my previous post on string interpolation in Clojure, I benchmarked << from core.incubator and it proved both useful and performant. But can this useful macro be used from ClojureScript? Yes.

Add core.incubator to your dependencies in project.clj of your ClojureScript project:

1
2
3
4
(defproject some-project "1.0.0-SNAPSHOT"
  :dependencies [...
                 [org.clojure/core.incubator "0.1.3"]
                 ...]

Now you can require the strint macros in your ClojureScript source (e.g. in src/cljs/app/core.cljs):

1
2
(ns app.core
  (:require-macros [clojure.core.strint :as strint]))

Now you can use the fast string interpolator thus:

1
2
3
4
5
6
7
8
(enable-console-print!)

(let [name "Ethel Smyth"
      profession "Composer"
      born 1858]
  (println
   (strint/<< "The person named ~{name} works as a ~{profession}
and was born in ~{born}")))

That’s it!