Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java for Clojure users

Tags:

java

clojure

api

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.

like image 974
prosseek Avatar asked Aug 19 '10 19:08

prosseek


People also ask

Does Clojure run on Java?

Clojure is a functional programming language that runs entirely on the Java Virtual Machine, in a similar way to Scala and Kotlin.

Does Clojure need JVM?

Clojure is a dynamically-typed, functional programming language that runs on the JVM (Java 5 or greater) and provides interoperability with Java.

Is Clojure interoperable with Java?

Clojure solved this new language library problem by running on the JVM and having interoperability with Java classes. When you use Clojure, you can use Java classes and Java libraries. Clojure builds on the strength of the production-hardened and tested JVM and existing Java libraries.

Why is Clojure not popular?

Clojure may not find much popularity since it lacks sufficient Clojure-based libraries and frameworks, compared to other applications worth consideration. The lisp syntax of Clojure is also more difficult to read, and therefore could stir being unfamiliar with the tool.


1 Answers

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, sqrt is 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) sqrt from the class Math.

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 value 1.2.

Now, look at the Double class in the Java 6 API:

Double

public Double(double value)

Constructs a newly allocated Double object that represents the 
primitive double argument.

Parameters:
value - the value to be represented by the Double.

So you can see what happened there. You "built" a new Double with value 1.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 called sqrt) parseDouble(String s):

(Double/parseDouble "1.2") => 1.2

Not to tricky there.

Nonstatic Methods

Say we want to use a Java class that we initialized to something. Not too difficult:

(-> (String. "Hey there") ;; make a new String object
    (.toUpperCase)) ;; pass it to .toUpperCase (look up -> to see what it does)
                    ;; toUpperCase is a non-static method

=> "HEY THERE"

So now we've used a method which is not static, and which requires a real, live String object to deal with. Let's look at how the docs say it works:

toUpperCase

public String toUpperCase()

Converts all of the characters in this String to upper case using 
the rules of the default locale. This method is equivalent to
toUpperCase(Locale.getDefault()).

Returns:
the String, converted to uppercase.

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 called this in 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 JFrame from the javax.swing library. 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:

(doto (JFrame. "My Frame!");; clever name
   (.setContentPane ... here we'd add a JPanel or something to the JFrame)
   (.pack) ;; this simply arranges the stuff in the frame–don't worry about it
   (.setVisibleTrue)) ;; this makes the Frame visible 

doto just 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 the JFrame that don't return anything in particular. All these methods are listed as methods of the JFrame in 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 proxy and reify as well as extend-type and 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:

  • Static or Nonstatic? ;; a guide to statis vs. nonstatic methods
  • The Java Class Library ;; an overview of what's out there, with a nice picture
  • The JavaDocs ;; linked above
  • Clojure Java Interop Docs ;; from the Clojure website
  • Best Java Books ;; as per clartaq's answer
like image 187
Isaac Avatar answered Sep 20 '22 14:09

Isaac