I've been working on Clojure question 135 Infix Calculator, basically a simplified infix to prefix arithmetic calculator:
(= 7 (__ 2 + 5))
I solved the problem as specified. But that got me wondering - what if the argument had been supplied as a string - how would I get it to work?
(= 7 (__ "2 + 5"))
Obviously I'd start with a split, then munch through the result:
(clojure.string/split "2 + 5" #"\s")
user=> ["2" "+" "5"]
But how would I convert the "+" to a function call? This isn't going to work:
("+" 2 5)
user=> java.lang.ClassCastException: java.lang.String cannot be
cast to clojure.lang.IFn <snip>
Enlightenment sought....
Look at the resolve function
((resolve (symbol "+")) 1 2)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With