Improving the Interoperability of Java and...

42
Outline Introduction Introduction to Clojure Functional Programming in Clojure Java Interop Conclusion & References Improving the Interoperability of Java and Clojure Stephen Adams University of Minnesota: Morris April 14th, 2012 Stephen Adams Improving the Interoperability of Java and Clojure

Transcript of Improving the Interoperability of Java and...

Page 1: Improving the Interoperability of Java and Clojurepersonal.morris.umn.edu/~elenam/publications/stephen... · 2012-05-26 · Outline Introduction Introduction to Clojure Functional

OutlineIntroduction

Introduction to ClojureFunctional Programming in Clojure

Java InteropConclusion & References

Improving the Interoperability of Java andClojure

Stephen Adams

University of Minnesota: Morris

April 14th, 2012

Stephen Adams Improving the Interoperability of Java and Clojure

Page 2: Improving the Interoperability of Java and Clojurepersonal.morris.umn.edu/~elenam/publications/stephen... · 2012-05-26 · Outline Introduction Introduction to Clojure Functional

OutlineIntroduction

Introduction to ClojureFunctional Programming in Clojure

Java InteropConclusion & References

A little reminder about Java

An object-oriented language released by Sun Microsystems in1995.

I Compiled to Java bytecodeI Java bytecode is run on the Java Virtual MachineI Second language in TIOBE programming Community

Index (as of April 2012)

Stephen Adams Improving the Interoperability of Java and Clojure

Page 3: Improving the Interoperability of Java and Clojurepersonal.morris.umn.edu/~elenam/publications/stephen... · 2012-05-26 · Outline Introduction Introduction to Clojure Functional

OutlineIntroduction

Introduction to ClojureFunctional Programming in Clojure

Java InteropConclusion & References

Clojure

The Clojure programming language was released in 2007 bythe software developer Rich Hickey. Clojure was designed withfour features in mind:

The Four Features of Clojure

I A LispI Functional programmingI Symbiosis with an established platform (Java)I Designed for concurrency

Stephen Adams Improving the Interoperability of Java and Clojure

Page 4: Improving the Interoperability of Java and Clojurepersonal.morris.umn.edu/~elenam/publications/stephen... · 2012-05-26 · Outline Introduction Introduction to Clojure Functional

OutlineIntroduction

Introduction to ClojureFunctional Programming in Clojure

Java InteropConclusion & References

Clojure

The Clojure programming language was released in 2007 bythe software developer Rich Hickey. Clojure was designed withfour features in mind:

The Four Features of Clojure

I A LispI Functional programmingI Symbiosis with an established platform (Java)I Designed for concurrency

Stephen Adams Improving the Interoperability of Java and Clojure

Page 5: Improving the Interoperability of Java and Clojurepersonal.morris.umn.edu/~elenam/publications/stephen... · 2012-05-26 · Outline Introduction Introduction to Clojure Functional

OutlineIntroduction

Introduction to ClojureFunctional Programming in Clojure

Java InteropConclusion & References

LISP and Functional Programming

I Lisp was developed in 1958I The earliest functional languageI Emphasizes the application of functionsI Imperative programming emphasizes changes in state

Stephen Adams Improving the Interoperability of Java and Clojure

Page 6: Improving the Interoperability of Java and Clojurepersonal.morris.umn.edu/~elenam/publications/stephen... · 2012-05-26 · Outline Introduction Introduction to Clojure Functional

OutlineIntroduction

Introduction to ClojureFunctional Programming in Clojure

Java InteropConclusion & References

Functional Programming and concurrency

I Functional languages have immutable data by defaultI A function will always return the same results given the

same argumentsI Easier to predict behavior of a program in a concurrent

environment

Stephen Adams Improving the Interoperability of Java and Clojure

Page 7: Improving the Interoperability of Java and Clojurepersonal.morris.umn.edu/~elenam/publications/stephen... · 2012-05-26 · Outline Introduction Introduction to Clojure Functional

OutlineIntroduction

Introduction to ClojureFunctional Programming in Clojure

Java InteropConclusion & References

Who is this talk for?

I Need to parallelize your work?I Are you tired of Java?

I Have a project already written in Java?I Have a Java library you use a lot?

Stephen Adams Improving the Interoperability of Java and Clojure

Page 8: Improving the Interoperability of Java and Clojurepersonal.morris.umn.edu/~elenam/publications/stephen... · 2012-05-26 · Outline Introduction Introduction to Clojure Functional

OutlineIntroduction

Introduction to ClojureFunctional Programming in Clojure

Java InteropConclusion & References

Who is this talk for?

I Need to parallelize your work?I Are you tired of Java?I Have a project already written in Java?I Have a Java library you use a lot?

Stephen Adams Improving the Interoperability of Java and Clojure

Page 9: Improving the Interoperability of Java and Clojurepersonal.morris.umn.edu/~elenam/publications/stephen... · 2012-05-26 · Outline Introduction Introduction to Clojure Functional

OutlineIntroduction

Introduction to ClojureFunctional Programming in Clojure

Java InteropConclusion & References

Who is this talk is for?

"Clojure does Java better than Java" - Stuart Halloway, at theGreater Atlanta Software Symposium, 2009.

Stephen Adams Improving the Interoperability of Java and Clojure

Page 10: Improving the Interoperability of Java and Clojurepersonal.morris.umn.edu/~elenam/publications/stephen... · 2012-05-26 · Outline Introduction Introduction to Clojure Functional

OutlineIntroduction

Introduction to ClojureFunctional Programming in Clojure

Java InteropConclusion & References

Introduction

Introduction to ClojureBasic Clojure syntaxData Structures and Collections

Functional Programming in ClojureFirst Class Functions in ClojureAnonymous Functions in Clojure

Java InteropBasic Java callingJava Objects in ClojureCustom types in Clojure

Conclusion & References

Stephen Adams Improving the Interoperability of Java and Clojure

Page 11: Improving the Interoperability of Java and Clojurepersonal.morris.umn.edu/~elenam/publications/stephen... · 2012-05-26 · Outline Introduction Introduction to Clojure Functional

OutlineIntroduction

Introduction to ClojureFunctional Programming in Clojure

Java InteropConclusion & References

Prefix Notation

(+ 2 3)=> 5

(+ 2 3 4)=> 9

(inc 4)=> 5

Stephen Adams Improving the Interoperability of Java and Clojure

Page 12: Improving the Interoperability of Java and Clojurepersonal.morris.umn.edu/~elenam/publications/stephen... · 2012-05-26 · Outline Introduction Introduction to Clojure Functional

OutlineIntroduction

Introduction to ClojureFunctional Programming in Clojure

Java InteropConclusion & References

Prefix Notation

(+ 2 3)=> 5

(+ 2 3 4)=> 9

(inc 4)=> 5

Stephen Adams Improving the Interoperability of Java and Clojure

Page 13: Improving the Interoperability of Java and Clojurepersonal.morris.umn.edu/~elenam/publications/stephen... · 2012-05-26 · Outline Introduction Introduction to Clojure Functional

OutlineIntroduction

Introduction to ClojureFunctional Programming in Clojure

Java InteropConclusion & References

Clojure Data Structures

I Many of Clojure’s data structures are just Java datastructures; strings, characters, and all numbers are justJava types.

I Clojure provides its own collections.

Stephen Adams Improving the Interoperability of Java and Clojure

Page 14: Improving the Interoperability of Java and Clojurepersonal.morris.umn.edu/~elenam/publications/stephen... · 2012-05-26 · Outline Introduction Introduction to Clojure Functional

OutlineIntroduction

Introduction to ClojureFunctional Programming in Clojure

Java InteropConclusion & References

Clojure Data Structures

I Many of Clojure’s data structures are just Java datastructures; strings, characters, and all numbers are justJava types.

I Clojure provides its own collections.

Stephen Adams Improving the Interoperability of Java and Clojure

Page 15: Improving the Interoperability of Java and Clojurepersonal.morris.umn.edu/~elenam/publications/stephen... · 2012-05-26 · Outline Introduction Introduction to Clojure Functional

OutlineIntroduction

Introduction to ClojureFunctional Programming in Clojure

Java InteropConclusion & References

Every Clojure collection is denoted by a different literal symbolpair.

Collection Literals

List (1 2 3 4)Vector ["apple" "banana" "orange"]Hashmap { :name "Stephen Adams" :phone 555555555 }

I Keywords are symbolic identifiers, denoted with a leadingcolon.

I Provide very fast equality tests

Stephen Adams Improving the Interoperability of Java and Clojure

Page 16: Improving the Interoperability of Java and Clojurepersonal.morris.umn.edu/~elenam/publications/stephen... · 2012-05-26 · Outline Introduction Introduction to Clojure Functional

OutlineIntroduction

Introduction to ClojureFunctional Programming in Clojure

Java InteropConclusion & References

Variables and Functions

(def vect [1 2 3 4 5])

(defn square [x](* x x))

Stephen Adams Improving the Interoperability of Java and Clojure

Page 17: Improving the Interoperability of Java and Clojurepersonal.morris.umn.edu/~elenam/publications/stephen... · 2012-05-26 · Outline Introduction Introduction to Clojure Functional

OutlineIntroduction

Introduction to ClojureFunctional Programming in Clojure

Java InteropConclusion & References

The functional features of Clojure

Functional programming primarily refers to two languagefeatures:

I First class functionsI Anonymous functions

Clojure supports both of these features.

Stephen Adams Improving the Interoperability of Java and Clojure

Page 18: Improving the Interoperability of Java and Clojurepersonal.morris.umn.edu/~elenam/publications/stephen... · 2012-05-26 · Outline Introduction Introduction to Clojure Functional

OutlineIntroduction

Introduction to ClojureFunctional Programming in Clojure

Java InteropConclusion & References

First Class FunctionsPassing functions to other functions

(defn square [x](* x x))

(map square [1 2 3 4 5])=> [1 4 9 16 25]

(reduce + [1 2 3 4 5])=> 15

Stephen Adams Improving the Interoperability of Java and Clojure

Page 19: Improving the Interoperability of Java and Clojurepersonal.morris.umn.edu/~elenam/publications/stephen... · 2012-05-26 · Outline Introduction Introduction to Clojure Functional

OutlineIntroduction

Introduction to ClojureFunctional Programming in Clojure

Java InteropConclusion & References

First Class FunctionsPassing functions to other functions

(defn square [x](* x x))

(map square [1 2 3 4 5])=> [1 4 9 16 25]

(reduce + [1 2 3 4 5])=> 15

Stephen Adams Improving the Interoperability of Java and Clojure

Page 20: Improving the Interoperability of Java and Clojurepersonal.morris.umn.edu/~elenam/publications/stephen... · 2012-05-26 · Outline Introduction Introduction to Clojure Functional

OutlineIntroduction

Introduction to ClojureFunctional Programming in Clojure

Java InteropConclusion & References

First Class FunctionsPassing functions to other functions

(defn square [x](* x x))

(map square [1 2 3 4 5])=> [1 4 9 16 25]

(reduce + [1 2 3 4 5])=> 15

Stephen Adams Improving the Interoperability of Java and Clojure

Page 21: Improving the Interoperability of Java and Clojurepersonal.morris.umn.edu/~elenam/publications/stephen... · 2012-05-26 · Outline Introduction Introduction to Clojure Functional

OutlineIntroduction

Introduction to ClojureFunctional Programming in Clojure

Java InteropConclusion & References

Anonymous Functions

(defn all-same? [vect](if (empty? vect)true(every?(fn [x] (= (first vect) x )) (rest vect))

Stephen Adams Improving the Interoperability of Java and Clojure

Page 22: Improving the Interoperability of Java and Clojurepersonal.morris.umn.edu/~elenam/publications/stephen... · 2012-05-26 · Outline Introduction Introduction to Clojure Functional

OutlineIntroduction

Introduction to ClojureFunctional Programming in Clojure

Java InteropConclusion & References

Basic Java callingJava Objects in ClojureCustom types in Clojure

Introduction to Java Interop

The idea for Clojure always involved interoperability with anexisting language. Java was chosen for various reasons:

I Access to previously written Java librariesI Already implemented, garbage collection and other

memory & resource management tools.I JVM is OS agnostic.

Stephen Adams Improving the Interoperability of Java and Clojure

Page 23: Improving the Interoperability of Java and Clojurepersonal.morris.umn.edu/~elenam/publications/stephen... · 2012-05-26 · Outline Introduction Introduction to Clojure Functional

OutlineIntroduction

Introduction to ClojureFunctional Programming in Clojure

Java InteropConclusion & References

Basic Java callingJava Objects in ClojureCustom types in Clojure

The dot special form

I Provides basic access to Java fields and methodsI Can also be read as “in the scope of."

(. "fred" toUpperCase)=> "FRED"

"fred".toUpperCase();

Stephen Adams Improving the Interoperability of Java and Clojure

Page 24: Improving the Interoperability of Java and Clojurepersonal.morris.umn.edu/~elenam/publications/stephen... · 2012-05-26 · Outline Introduction Introduction to Clojure Functional

OutlineIntroduction

Introduction to ClojureFunctional Programming in Clojure

Java InteropConclusion & References

Basic Java callingJava Objects in ClojureCustom types in Clojure

The dot special form

I Provides basic access to Java fields and methodsI Can also be read as “in the scope of."

(. "fred" toUpperCase)=> "FRED"

"fred".toUpperCase();

Stephen Adams Improving the Interoperability of Java and Clojure

Page 25: Improving the Interoperability of Java and Clojurepersonal.morris.umn.edu/~elenam/publications/stephen... · 2012-05-26 · Outline Introduction Introduction to Clojure Functional

OutlineIntroduction

Introduction to ClojureFunctional Programming in Clojure

Java InteropConclusion & References

Basic Java callingJava Objects in ClojureCustom types in Clojure

The dot special form cont.

(.toUpperCase "fred")

Accessing static methods and fields

(. Math PI)=> 3.141592653589793

(Math/PI)=> 3.141592653589793(Math/abs -2)=> 2

Stephen Adams Improving the Interoperability of Java and Clojure

Page 26: Improving the Interoperability of Java and Clojurepersonal.morris.umn.edu/~elenam/publications/stephen... · 2012-05-26 · Outline Introduction Introduction to Clojure Functional

OutlineIntroduction

Introduction to ClojureFunctional Programming in Clojure

Java InteropConclusion & References

Basic Java callingJava Objects in ClojureCustom types in Clojure

The dot special form cont.

(.toUpperCase "fred")

Accessing static methods and fields

(. Math PI)=> 3.141592653589793

(Math/PI)=> 3.141592653589793(Math/abs -2)=> 2

Stephen Adams Improving the Interoperability of Java and Clojure

Page 27: Improving the Interoperability of Java and Clojurepersonal.morris.umn.edu/~elenam/publications/stephen... · 2012-05-26 · Outline Introduction Introduction to Clojure Functional

OutlineIntroduction

Introduction to ClojureFunctional Programming in Clojure

Java InteropConclusion & References

Basic Java callingJava Objects in ClojureCustom types in Clojure

Object Construction and modification

(new StringBuffer "fred")=> #<StringBuffer fred>

(doto (new StringBuffer "fred")(.setCharAt 0 \F)(.append " is a nice guy!"))=> #<StringBuffer Fred is a nice guy!>

Stephen Adams Improving the Interoperability of Java and Clojure

Page 28: Improving the Interoperability of Java and Clojurepersonal.morris.umn.edu/~elenam/publications/stephen... · 2012-05-26 · Outline Introduction Introduction to Clojure Functional

OutlineIntroduction

Introduction to ClojureFunctional Programming in Clojure

Java InteropConclusion & References

Basic Java callingJava Objects in ClojureCustom types in Clojure

Object Construction and modification

(new StringBuffer "fred")=> #<StringBuffer fred>

(doto (new StringBuffer "fred")(.setCharAt 0 \F)(.append " is a nice guy!"))=> #<StringBuffer Fred is a nice guy!>

Stephen Adams Improving the Interoperability of Java and Clojure

Page 29: Improving the Interoperability of Java and Clojurepersonal.morris.umn.edu/~elenam/publications/stephen... · 2012-05-26 · Outline Introduction Introduction to Clojure Functional

OutlineIntroduction

Introduction to ClojureFunctional Programming in Clojure

Java InteropConclusion & References

Basic Java callingJava Objects in ClojureCustom types in Clojure

Object Construction and modification cont.

public class ExampleBuffer {public static void main(String[] args){

StringBuffer buff = new StringBuffer("fred");buff.setCharAt(0, "F");buff.append(" is a nice guy");

}}

Stephen Adams Improving the Interoperability of Java and Clojure

Page 30: Improving the Interoperability of Java and Clojurepersonal.morris.umn.edu/~elenam/publications/stephen... · 2012-05-26 · Outline Introduction Introduction to Clojure Functional

OutlineIntroduction

Introduction to ClojureFunctional Programming in Clojure

Java InteropConclusion & References

Basic Java callingJava Objects in ClojureCustom types in Clojure

Many ways of defining a typeBy Chas Emerick, from: http://bit.ly/IiozRP

Stephen Adams Improving the Interoperability of Java and Clojure

Page 31: Improving the Interoperability of Java and Clojurepersonal.morris.umn.edu/~elenam/publications/stephen... · 2012-05-26 · Outline Introduction Introduction to Clojure Functional

OutlineIntroduction

Introduction to ClojureFunctional Programming in Clojure

Java InteropConclusion & References

Basic Java callingJava Objects in ClojureCustom types in Clojure

Many ways of defining a type

Stephen Adams Improving the Interoperability of Java and Clojure

Page 32: Improving the Interoperability of Java and Clojurepersonal.morris.umn.edu/~elenam/publications/stephen... · 2012-05-26 · Outline Introduction Introduction to Clojure Functional

OutlineIntroduction

Introduction to ClojureFunctional Programming in Clojure

Java InteropConclusion & References

Basic Java callingJava Objects in ClojureCustom types in Clojure

Proxy

I Must implement a Java interface or extend a Java classI Creates a single instance of an anonymous Java classI Cannot define methods not declared by a superclass or

interface

Stephen Adams Improving the Interoperability of Java and Clojure

Page 33: Improving the Interoperability of Java and Clojurepersonal.morris.umn.edu/~elenam/publications/stephen... · 2012-05-26 · Outline Introduction Introduction to Clojure Functional

OutlineIntroduction

Introduction to ClojureFunctional Programming in Clojure

Java InteropConclusion & References

Basic Java callingJava Objects in ClojureCustom types in Clojure

Proxy cont.

(defn add-mousepressed-listener[component func args](let [listener

(proxy [MouseAdapter] [](mousePressed [event]

(apply func event args)))](.addMouseListener component listener)

listener))

Stephen Adams Improving the Interoperability of Java and Clojure

Page 34: Improving the Interoperability of Java and Clojurepersonal.morris.umn.edu/~elenam/publications/stephen... · 2012-05-26 · Outline Introduction Introduction to Clojure Functional

OutlineIntroduction

Introduction to ClojureFunctional Programming in Clojure

Java InteropConclusion & References

Basic Java callingJava Objects in ClojureCustom types in Clojure

Gen-class

I Proxy will only allow you to do so much because you canonly overload functions defined by a super class orinterface.

I Proxy also wont create a named type.

I Without a named type Java is unable to call Clojure code.

Stephen Adams Improving the Interoperability of Java and Clojure

Page 35: Improving the Interoperability of Java and Clojurepersonal.morris.umn.edu/~elenam/publications/stephen... · 2012-05-26 · Outline Introduction Introduction to Clojure Functional

OutlineIntroduction

Introduction to ClojureFunctional Programming in Clojure

Java InteropConclusion & References

Basic Java callingJava Objects in ClojureCustom types in Clojure

Gen-class

I Proxy will only allow you to do so much because you canonly overload functions defined by a super class orinterface.

I Proxy also wont create a named type.I Without a named type Java is unable to call Clojure code.

Stephen Adams Improving the Interoperability of Java and Clojure

Page 36: Improving the Interoperability of Java and Clojurepersonal.morris.umn.edu/~elenam/publications/stephen... · 2012-05-26 · Outline Introduction Introduction to Clojure Functional

OutlineIntroduction

Introduction to ClojureFunctional Programming in Clojure

Java InteropConclusion & References

Basic Java callingJava Objects in ClojureCustom types in Clojure

Gen-class cont.

(gen-class:name Example:prefix "example-"))

(defn example-toString[this]"Hello, world!")

(def aClass (new Example))(.toString aClass)=> "Hello, world!"

Stephen Adams Improving the Interoperability of Java and Clojure

Page 37: Improving the Interoperability of Java and Clojurepersonal.morris.umn.edu/~elenam/publications/stephen... · 2012-05-26 · Outline Introduction Introduction to Clojure Functional

OutlineIntroduction

Introduction to ClojureFunctional Programming in Clojure

Java InteropConclusion & References

Basic Java callingJava Objects in ClojureCustom types in Clojure

Gen-class cont.

(gen-class:name Example:prefix "example-"))

(defn example-toString[this]"Hello, world!")

(def aClass (new Example))(.toString aClass)=> "Hello, world!"

Stephen Adams Improving the Interoperability of Java and Clojure

Page 38: Improving the Interoperability of Java and Clojurepersonal.morris.umn.edu/~elenam/publications/stephen... · 2012-05-26 · Outline Introduction Introduction to Clojure Functional

OutlineIntroduction

Introduction to ClojureFunctional Programming in Clojure

Java InteropConclusion & References

Wrapping up the interop zone

I Proxy creates a single instance of an anonymous classI A class defined by proxy is not visable to external filesI Gen-class creates a named type which Java will be able to

see

Stephen Adams Improving the Interoperability of Java and Clojure

Page 39: Improving the Interoperability of Java and Clojurepersonal.morris.umn.edu/~elenam/publications/stephen... · 2012-05-26 · Outline Introduction Introduction to Clojure Functional

OutlineIntroduction

Introduction to ClojureFunctional Programming in Clojure

Java InteropConclusion & References

Java - Clojure Relationship

I You want to program Clojure in Clojure, not Java.I Gen-class and, to some extent, proxy break from

Clojure-like syntax.I These functions should be used sparingly.I C. Emerick’s figure (http://bit.ly/IiozRP).

Stephen Adams Improving the Interoperability of Java and Clojure

Page 40: Improving the Interoperability of Java and Clojurepersonal.morris.umn.edu/~elenam/publications/stephen... · 2012-05-26 · Outline Introduction Introduction to Clojure Functional

OutlineIntroduction

Introduction to ClojureFunctional Programming in Clojure

Java InteropConclusion & References

Recommendations

I Push Clojure’s native abstractions into interop zone,possibly macros.

I Centralize documentation sources.I Streamline IDE setup for beginners.

Stephen Adams Improving the Interoperability of Java and Clojure

Page 41: Improving the Interoperability of Java and Clojurepersonal.morris.umn.edu/~elenam/publications/stephen... · 2012-05-26 · Outline Introduction Introduction to Clojure Functional

OutlineIntroduction

Introduction to ClojureFunctional Programming in Clojure

Java InteropConclusion & References

Why this matters

“The killer app for Clojure is the JVM itself. Everyone’s fed upwith the Java language, but understandably we don’t want toabandon our investment in the Java Virtual Machine and itscapabilities: the libraries, the configuration, the monitoring, andall the other entirely valid reasons we still use it."Steve Yegge, in the foreward to Joy of Clojure.

Stephen Adams Improving the Interoperability of Java and Clojure

Page 42: Improving the Interoperability of Java and Clojurepersonal.morris.umn.edu/~elenam/publications/stephen... · 2012-05-26 · Outline Introduction Introduction to Clojure Functional

OutlineIntroduction

Introduction to ClojureFunctional Programming in Clojure

Java InteropConclusion & References

References

I CLOJUREDOCS.ORG. Mar. 2012. [Online; accessedMarch-2012].

I CLOJURE.ORG. Mar. 2012. [Online; accessedMarch-2012].

I Fogus, M., and Houser, C. The Joy of Clojure, ManningPublications.

I Halloway, S. Clojure-Java interop: A better Java than Java.QCon.

Stephen Adams Improving the Interoperability of Java and Clojure