Re: Applying Java functions
Hi, user (definline sqrt [x] `(Math/sqrt ~x)) #'user/sqrt user (map sqrt (range 1 10)) (1.0 1.4142135623730951 1.7320508075688772 2.0 2.23606797749979 2.449489742783178 2.6457513110645907 2.8284271247461903 3.0) -- http://www.nostdal.org/ -- 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 Note that posts from new members are moderated - please be patient with your first post. 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
Applying Java functions
Hi everyone. Ran into an interesting case here when trying stuff out in the REPL. user= (Math/sqrt 4) 2.0 user= (map #(Math/sqrt %) (range 1 10)) (1.0 1.4142135623730951 ..) user= (map Math/sqrt (range 1 10)) java.lang.Exception: Unable to find static field: sqrt in class java.lang.Math (NO_SOURCE_FILE: 2) Shouldn't it be possible to apply Math/sqrt directly? If I use a function from the clojure.core, I can do it: user= (map str (range 1 10)) (1 2 3 4 5 6 7 8 9) What am I missing? Thanks. -- 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 Note that posts from new members are moderated - please be patient with your first post. 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
Re: Applying Java functions
On Fri, Jun 17, 2011 at 2:59 AM, de1976 davidescobar1...@gmail.com wrote: Hi everyone. Ran into an interesting case here when trying stuff out in the REPL. user= (Math/sqrt 4) 2.0 user= (map #(Math/sqrt %) (range 1 10)) (1.0 1.4142135623730951 ..) user= (map Math/sqrt (range 1 10)) java.lang.Exception: Unable to find static field: sqrt in class java.lang.Math (NO_SOURCE_FILE: 2) Shouldn't it be possible to apply Math/sqrt directly? If I use a function from the clojure.core, I can do it: user= (map str (range 1 10)) (1 2 3 4 5 6 7 8 9) Java methods aren't first-class functions, so they can't be mapped, or used directly in filter or partition-by or similarly. But as you've found you can wrap one in a closure to use it. -- Protege: What is this seething mass of parentheses?! Master: Your father's Lisp REPL. This is the language of a true hacker. Not as clumsy or random as C++; a language for a more civilized age. -- 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 Note that posts from new members are moderated - please be patient with your first post. 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
Re: Applying Java functions
On 17 Jun, 2011, at 9:20 , Ken Wesson wrote: Shouldn't it be possible to apply Math/sqrt directly? If I use a function from the clojure.core, I can do it: user= (map str (range 1 10)) (1 2 3 4 5 6 7 8 9) Java methods aren't first-class functions, so they can't be mapped, or used directly in filter or partition-by or similarly. But as you've found you can wrap one in a closure to use it. Java methods aren't even first-class objects (nor, in fact, objects at all) in the Java world. Clojure can hardly do better than Java in unifying things at the JVM level. The one thing that you can do with a method in Java is call it, and the same limitation applies in Clojure. Konrad. -- 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 Note that posts from new members are moderated - please be patient with your first post. 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
Re: Applying Java functions
Thanks. It really is amazing how non functional Java is. Makes me glad there are now languages that do their best to correct that. -- 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 Note that posts from new members are moderated - please be patient with your first post. 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
Re: Applying Java functions
On Jun 17, 2011, at 3:44 AM, Konrad Hinsen wrote: Java methods aren't even first-class objects (nor, in fact, objects at all) in the Java world. Clojure can hardly do better than Java in unifying things at the JVM level. The one thing that you can do with a method in Java is call it, and the same limitation applies in Clojure. Why would it have to be done at the JVM level? Couldn't the Clojure compiler generate an anonymous function whenever it sees a Java method? I presume it currently doesn't do this for performance reasons. -- 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 Note that posts from new members are moderated - please be patient with your first post. 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
Re: Applying Java functions
On 18 Jun, 2011, at 1:12 , Michael Gardner wrote: On Jun 17, 2011, at 3:44 AM, Konrad Hinsen wrote: Java methods aren't even first-class objects (nor, in fact, objects at all) in the Java world. Clojure can hardly do better than Java in unifying things at the JVM level. The one thing that you can do with a method in Java is call it, and the same limitation applies in Clojure. Why would it have to be done at the JVM level? Couldn't the Clojure compiler generate an anonymous function whenever it sees a Java method? I presume it currently doesn't do this for performance reasons. How would it see a Java method in the first place? It's not a value, so Clojure would have to add a special language rule for making sense of it: a symbol that does not refer to a var but that does name an existing Java method is replaced by an anonymous function calling that Java method. I doubt the benefit (syntactic sugar) would justify the added complexity. The explicit creation of the anonymous function by the programmer also takes care of the arity problem; an automatically generated one would have to cover all possible arities. Konrad. -- 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 Note that posts from new members are moderated - please be patient with your first post. 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