Skip to content

Latest commit

 

History

History
42 lines (33 loc) · 834 Bytes

advanced-strings.md

File metadata and controls

42 lines (33 loc) · 834 Bytes

Advanced String features

Text blocks

(do
   (def s1 """{ "fruit": "apple", "color": "red" }""")

   (def s2 """
           { 
             "fruit": "apple",
             "color": "red" 
           }
           """)

   (println s1)
   (println s2))

Interpolation

Interpolation is controlled using ~{} and ~() forms. The former is used for simple value replacement while the latter can be used to embed the results of arbitrary function invocation into the produced string.

Interpolation is implemented as a reader macro. It's parsed at read time and turned into a (str args) expression.

(do
   (let [x 100] 
      (println "x: ~{x}")
      (println "f(x): ~(inc x)")))
(do
   (let [x 100] 
      (println """x: ~{x}""")
      (println """f(x): ~(inc x)""")))