Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easy way to work with text without having to escape quotation marks in Clojure?

I'm playing around with text parsing in the REPL, and sometimes want to dump in a bunch of data into a string, whether it's a bibtex entry or some EBNF notation etc. Typically there might be quotation marks in the string, and it's very tedious and error-prone to have to manually escape them..

Is there an alternative way of doing this, such as Ruby's %Q|I can use "Quotation Marks"| or heredocs etc? Or would it be possible to write a macro or modification of the reader to enable this?

like image 841
Stian Håklev Avatar asked Nov 24 '22 00:11

Stian Håklev


1 Answers

There has been some discussion about a more robust quoting syntax, but no changes to support this seem imminent.

In the meantime, to specifically handle the REPL interaction you mention, you might find this useful. Note it probably doesn't work for every REPL out there -- they don't all support read-line terribly well:

(defn read-lines []
  (->> (repeatedly read-line)
       (take-while #(not= % "."))
       (mapcat #(list % "\n"))
       (apply str)))

Use it by running (read-lines) at the REPL, pasting your content, and then adding a line with a . by itself:

user=> (read-lines)
  #_=> This "works"
  #_=> sometimes...
  #_=> .
"This \"works\"\nsometimes...\n"
user=> (print *1)
This "works"
sometimes...
nil
like image 140
Chouser Avatar answered Dec 01 '22 01:12

Chouser