How to convert a string to a function in Clojure?

前端 未结 2 434
庸人自扰
庸人自扰 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.

    0 讨论(0)
  • 2021-02-14 08:40

    Look at the resolve function

    ((resolve (symbol "+")) 1 2)
    
    0 讨论(0)
提交回复
热议问题