Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can clojure convert string representing a sequence back to a sequence?

I can convert a string to a sequence, and then convert that sequence to a string representing the sequence.

user=> (str (first (list (seq "(xy)z"))))
"(\\( \\x \\y \\) \\z)"

I can also insert apply into the above form to get the original string back

user=> (apply str (first (list (seq "(xy)z"))))
"(xy)z"

but is there a way to convert a string representing a sequence, to the sequence that the string represents? such as:

"(\\( \\x \\y \\) \\z)"
user=> (some-fn2 "(\\( \\x \\y \\) \\z)")
(\( \x \y \) \z \))
like image 265
dansalmo Avatar asked Aug 20 '12 21:08

dansalmo


1 Answers

The read-string function reads a string into a Clojure expression.

(read-string "(\\( \\x \\y \\) \\z)")
(\( \x \y \) \z)  

The read family of functions are a big part of what makes Clojure a lisp and the whole "everything is data" mindset. You can read any form with them:

(read-string "{:a 1 :b 3 :c (1 2 3)}")
{:a 1, :b 3, :c (1 2 3)}
like image 172
Arthur Ulfeldt Avatar answered Sep 22 '22 14:09

Arthur Ulfeldt