I’ve been using Lisp on and off, and I’m catching up with clojure.
The good thing about clojure is that I can use all the java functions naturally, and the bad thing about clojure is also that I have to know java function naturally.
For example, I had to spend some time (googling) to find square function in Java (Math/sqrt in clojure notation).
Could you recommend me some good information resource for Java functions (libraries) for clojure users that are not so familiar with Java?
It can be anything – good books, webpages, forums or whatever.
I had similar problems when I first started using Clojure. I had done some Java development years ago, but was still pretty unfamiliar with the libraries out there.
Intro
I find the easiest way to use Java is to not really use it. I think a book would be a little bit much to just get started using Java from Clojure. There isn’t that much you really need to know, unless you really start getting down into the JVM/Java libraries. Let me explain.
Spend more time learning how to use Clojure inside and out, and become familiar with Clojure-Contrib. For instance,
sqrtis in generic.math-functions in clojure.contrib.Many of the things you’ll need are in fact already in Clojure–but still plenty are not.
Become familiar with calling conventions and syntactic sugar in Clojure for using Java. e.g.
Math/sqrt, as per your example, is calling the static method (which just a function, basically)sqrtfrom the classMath.Anyway, here’s a guide that should help you get started if you find yourself really needing to use Java. I’m going to assume you’ve done some imperative OO programming, but not much else. And even if you haven’t, you should be okay.
Isaac’s Clojurist’s Guide to Java
Classes
A class is a bundle of methods (functions which act on the class) that
can also be a data type: e.g. to create a new class of the type
Double:(Double. 1.2)which initializes the class Double (the period is the syntactic sugar for calling the class constructor methods, which initialize the class with the values you provide) with the value1.2.Now, look at the
Doubleclass in the Java 6 API:So you can see what happened there. You "built" a new
Doublewith value1.2, which is a double. A little confusing there, but really a Double is a class that represents a Double and can do things relating to doubles.Static Methods
For instance, to parse a Double value out of a string, we can use the static method (meaning we don’t need a particular instance of
Double, we can just call it like we calledsqrt)parseDouble(String s):Not to tricky there.
Nonstatic Methods
Say we want to use a Java class that we initialized to something. Not too difficult:
So now we’ve used a method which is not static, and which requires a real, live
Stringobject to deal with. Let’s look at how the docs say it works:So here we have a method which returns a string (as shown by the "String" after the public in the definition, and takes no parameters. But wait! It does take a parameter. In Python, it’d be the implicit parameter
self: this is calledthisin Java.We could also use the method like this:
(.toUpper (String. "Hey there"))and get the same result.More on Methods
Since you deal with mutable data and classes in Java, you need to be able to apply functions to Classes (instances of Classes, really) and not expect a return value.
For instance, say we’re dealing with a
JFramefrom thejavax.swinglibrary. We might need to do a number of things to it, not with it (you generally operate with values, not on them in functional languages). We can, like this:dotojust passes its first argument to all the other functions you supply it, and passes it as the first argument to them. So here we’re just doing a lot of things to theJFramethat don’t return anything in particular. All these methods are listed as methods of theJFramein the documentation (or its superclasses… don’t worry about those yet).Wrapping up
This should prepare you for now exploring the JavaDocs yourself. Here you’ll find everything that is available to you in a standard Java 1.6 install. There will be new concepts, but a quick Google search should answer most of your questions, and you can always come back here with specific ones.
Be sure to look into the other important Clojure functions like
proxyandreifyas well asextend-typeand its friends. I don’t often use them, but when I need to, they can be invaluable. I still am understanding them myself, in fact.There’s a ton out there, but it’s mostly a problem of volume rather than complexity. It’s not a bad problem to have.
Additional reading: