How to convert a string to a function in Clojure?

前端 未结 2 435
庸人自扰
庸人自扰 2021-02-14 07:48

I\'ve been working on Clojure question 135 Infix Calculator, basically a simplified infix to prefix arithmetic calculator:

(= 7  (__ 2 + 5))

I

2条回答
  •  渐次进展
    2021-02-14 08:29

    you can use load-string or read-string. That's something like READ-FROM-STRING in common lisp, which adhere to the principle of 'Code is Data', treats the string as codes - evaluate and return the value.

    user=> (class (load-string "+"))
    clojure.core$_PLUS_
    user=> (read-string "+")
    +
    user=>
    

    then you can simply use it.

提交回复
热议问题