How do you evaluate a string as a clojure expression?

后端 未结 3 1720
南笙
南笙 2020-12-10 10:55

How would I get something similar to the following?:

(evaluate-text \"(+ 1 2)\")  ; resolves to 3
相关标签:
3条回答
  • 2020-12-10 11:05

    How similar does it have to be? Clojure's eval works on lists, so:

    (eval (list + 1 2)) #=> 3
    
    0 讨论(0)
  • 2020-12-10 11:18
    (load-string "(+ 1 2)")
    
    0 讨论(0)
  • 2020-12-10 11:27
    user> (eval (read-string "(+ 1 2)"))
    3
    

    You probably shouldn't ever need to do this. Macros and fns make this kind of thing unnecessary 99% of the time. This is quite brittle, and can be unsafe if these strings are coming from user input, and so on.

    0 讨论(0)
提交回复
热议问题