Reb.Cabin
Reb.Cabin

Reputation: 5567

how to convert a string to a clojure expression?

I'd like to send a clojure expression in a string to be evaluated at the receiver, which might be a web service written in compojure. For example, suppose I have the string "(* 7 6)", which I'd like to turn into '(* 7 6), which I can then pass to eval and get 42. This operation is trivial in JavaScript, but not quite sure how to do it in clojure. Hints?

Upvotes: 3

Views: 318

Answers (1)

leonardoborges
leonardoborges

Reputation: 5619

This should do the trick:

(eval (read-string "(* 7 6)")) ;; 42

Or, for short:

(load-string "(* 7 6)") ;; 42

Upvotes: 5

Related Questions