Skip to content

Instantly share code, notes, and snippets.

@liquidz
Created April 16, 2012 14:54
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liquidz/2399254 to your computer and use it in GitHub Desktop.
Save liquidz/2399254 to your computer and use it in GitHub Desktop.
Clojure1.4 Reader Literals Test
(ns myreader.core
"Reader Literals Test"
(:require [clojure.string :as str]))
(defn debug-print
"Gauche debug print"
[x]
`(let [res# ~x]
(println "?=" res#)
res#))
(defn expand-sexp
"Expand S-exp in string"
[s]
(let [ls (map-indexed #(if (even? %) %2 (read-string %2))
(str/split s #"`"))]
`(apply str (list ~@ls))))
(defn -main []
(println (map inc #?=(range 10)))
; ?= (0 1 2 3 4 5 6 7 8 9)
;=> (1 2 3 4 5 6 7 8 9 10)
(let [i 100]
(println #str"i = `i`")
;=> "i = 100"
(println #str"(+ 1 2 3) = `(+ 1 2 3)`")))
;=> "(+ 1 2 3) = 6"
{
?= myreader.core/debug-print
str myreader.core/expand-sexp
}
@ericnormand
Copy link

It should be noted that top-level reader literals are reserved. You should use a namespace for your reader names. For instance #mynamesspace/str

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment