Using functions from Java packages

115 views
Skip to first unread message

Larry Travis

unread,
Dec 16, 2012, 2:33:06 PM12/16/12
to clo...@googlegroups.com, Larry Travis
It almost certainly has something to do with my abysmal ignorance about
things Java, but I don't understand this difference:

(1)
user> (map Math/sqrt [5 6 16])

Unable to find static field: sqrt in class java.lang.Math
[Thrown class java.lang.RuntimeException]

(2)
user> (map #(Math/sqrt %) [5 6 16])
(2.23606797749979 2.449489742783178 4.0)

Thanks for helping me understand.
--Larry

Jim - FooBar();

unread,
Dec 16, 2012, 2:47:03 PM12/16/12
to clo...@googlegroups.com
Java methods are not first-class...you cannot use them like that...you
need an object to call the method on...by wrapping the java call with an
anonymous fn you are able to use Math/sqrt as 1st-class...
Hope it is clearer now...

Jim

Softaddicts

unread,
Dec 16, 2012, 2:53:36 PM12/16/12
to clo...@googlegroups.com
First example tries to access a public static field in the Math class.

Second example calls the static member function of the Math class.

The difference is in the missing set of parenthesis.

A static field or member function is attached to the class, not to a specific object
and can be accessed through the class itself. Hence the / notation.

On the other hand, (.addListener x ...) refers to the member fn addListener of the given object x.

You will rarely find Java object specific public fields directly accessible, most of the
time you need to use a getter to access them, hence the profusion of .getXzzz
when you look at interop code.

Static fields attached to a class are most of the time immutable, they are a way
to make constants public and avoid the getter wrapper syndrom.

Last thing, a class can define classes so you may need to access
Aclass$Bclass/field to get access to a class static field defined within a class.

This does not apply to an object of class B, the usual (.memberFn object ...)
would still apply assuming you are handed an object created from an inner class.


Luc P.
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@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+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
>
--
Softaddicts<lprefo...@softaddicts.ca> sent by ibisMail from my ipad!

Larry Travis

unread,
Dec 16, 2012, 6:59:58 PM12/16/12
to clo...@googlegroups.com, Larry Travis
Thank you, gentlemen. Jim and Luc, your answers are both helpful. Luc's
answer illustrates why a Java tyro often has problems understanding
Clojure. Somebody like me who is trying to master Clojure, having come
to it via a language path that doesn't include Java, needs a
prerequisite crash course in Java concepts. (I hope it isn't necessary
for him actually to learn Java programming skills. Reserving
programming-skill learning for Clojure is a lot more fun!) Anybody have
any recommendations of a book that could be used for such a crash course?
--Larry

Softaddicts

unread,
Dec 16, 2012, 7:28:56 PM12/16/12
to clo...@googlegroups.com
The problem if you dive into Java is that it may bring to your attention a myriad of details
that may not be worth the trouble of learning if you do not expect to
dive into Java and stay on the Clojure side of the fence.

No ready-fit light reading comes to my mind. Maybe a "learn java in 21 days" primer
might be just enough but you would still have to fill the gap from the
perspective of calling java libs from Clojure.

Every Clojure books that I know of has a chapter on interop but that may not be
enough alone depending of your interop needs.

I would suggest narrowing the subject on how to understand Javadoc (most libs APIs are
documented) and how to translate this to Clojure usable forms. The java primer
might be a start to understand the API terminology(class, static, abstract class, ....).

You may be able to pick up only the pieces you
need along the way instead of digesting the whole thing at once.

Luc

greg r

unread,
Dec 17, 2012, 7:25:06 PM12/17/12
to clo...@googlegroups.com, Larry Travis
Another possibility is the macro memfn.  From the documentation:

http://clojure.github.com/clojure/clojure.core-api.html#clojure.core/memfn

Regards,
Greg

Reply all
Reply to author
Forward
Message has been deleted
0 new messages