Yes. The following should also work, without calling into Clojure implementation methods: (defn eval-string [s] (eval (read-string s)))
-Stuart Sierra On Apr 26, 1:50 pm, timc <timgcl...@gmail.com> wrote: > Thanks Stuart. > > I have figured out another way, which is much more general (and uses > the lowest level of how Clojure works). > > (defn evalStr [s] (clojure.lang.Compiler/eval (clojure.lang.RT/ > readString s))) > > will (attempt to) execute any valid form (i.e. the string that is the > source of the form). > > Thus: (evalStr "(+ 1 2)") --> 3 > > On Apr 26, 5:21 pm, Stuart Sierra <the.stuart.sie...@gmail.com> wrote: > > > This will work: > > ((resolve (symbol "+")) 1 2 3) > > > To answer your question, a symbol is just a symbol, it doesn't have a > > value. (In Common Lisp, symbols have values, but in Clojure they do > > not.) In Clojure, values belong to Vars. "resolve" will find the Var > > that is named by the symbol. When you write (+ 1 2 3), the compiler > > automatically resolves the "+" to get the Var #'clojure.core/+ > > > To invoke Java methods by name, use the Java Reflection > > API:http://java.sun.com/docs/books/tutorial/reflect/index.html > > > -Stuart Sierra > > > On Apr 26, 10:46 am, timc <timgcl...@gmail.com> wrote: > > > > Is there a way of invoking functions, or java methods "by name" in > > > Clojure? > > > Or, put another way, why does this work: > > > > (+ 1 2) > > > > but this does not: > > > > ((symbol "+") 1 2) > > > > Similarly, this works > > > > (. javaObj (methodName param)) > > > > but this does not: > > > > (. javaObj ((symbol "methodName") param)) > > > > I suppose this really comes down to the question: what is a symbol? > > > And, is the thing at the head of the list (+ 1 2 3) a symbol? > > > > [In Icon, for example, there is no difference between > > > > "funcName"(1,2,3) and funcName(1,2,3), or indeed: > > > > x := "funcName" > > > x(1,2,3) > > > > which is VERY useful.] --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~----------~----~----~----~------~----~------~--~---