View
1.240
Download
3
Category
Preview:
DESCRIPTION
JAX London 2012 presentation about the coming features of Java 8, and the effect they have on programming idioms and use of parallelism.
Citation preview
Copyright © 2012 Russel Winder 1
ClosuresThe Next “Big Thing” in Java
Russel Winder
email: russel@winder.org.ukxmpp: russel@winder.org.uk
twitter: @russel_winder
Copyright © 2012 Russel Winder 2
ClosuresThe Next “Big Thing” in Java?
Russel Winder
email: russel@winder.org.ukxmpp: russel@winder.org.uk
twitter: @russel_winder
Copyright © 2012 Russel Winder 3
Aims, Goals and Objectives
● Investigate why some idioms from Java 0–7 lead to bad code in a post-Java 7 world.
● Show what some of the idioms of Java 8 and beyond are likely to be.
● Survive to the end of the session.
Copyright © 2012 Russel Winder 4
Structure
A beginning.
A middle.
An end.
Copyright © 2012 Russel Winder 5
Interstitial Advertisement
Copyright © 2012 Russel Winder 6
A Beginning
Copyright © 2012 Russel Winder 7
Many people are still using Java 1.4.
Copyright © 2012 Russel Winder 8
They see no reason to move to anylater version of Java:
no real benefit, for too much pain.
Copyright © 2012 Russel Winder 9
Java 5 is a barrier too far for them.
Copyright © 2012 Russel Winder 10
The Java 7 Java 8 change→is an even more disruptive change.
Copyright © 2012 Russel Winder 11
This possibly means Javais now a legacy language?
Copyright © 2012 Russel Winder 12
No, it's an opportunity to stopprogramming using 1970s techniques
and start using 1930s ones.
Copyright © 2012 Russel Winder 13
Alonzo ChurchAlan Turing
Stephen KleeneJ B Rosser
Copyright © 2012 Russel Winder 14
And an opportunity to do 1990s programming.
Copyright © 2012 Russel Winder 15
i.e. We finally get to do real object-oriented programming using Java.
Copyright © 2012 Russel Winder 16
Another Beginning
Copyright © 2012 Russel Winder 17
What is a “closure”?
Copyright © 2012 Russel Winder 18
A closure is a function with an associated environment containing values for all the free
variables in the function.
Copyright © 2012 Russel Winder 19
Integer f(final Integer x) { return x * y ;}
Free variable.
A Function
Copyright © 2012 Russel Winder 20
A closure should be referentially transparent.
Whenever function f is evaluated on the same value,a say, then the same value is returned.
f(a) = b
Copyright © 2012 Russel Winder 21
Java has had things (sort of) like thissince (almost) the beginning…
Copyright © 2012 Russel Winder 22
A closure can be realized as an instance ofa class with a single method and
single assignment fields.
Copyright © 2012 Russel Winder 23
public class ClosureClass { private final Integer multiplier; public ClosureClass(final Integer m) { multiplier = m; } public Integer call(final Integer i) { return multiplier * i; }}
No free variables in call.
Copyright © 2012 Russel Winder 24
final ClosureClass multiplyBy5 = new ClosureClass(5);
multiplyBy5.call(4)
Copyright © 2012 Russel Winder 25
Alternatively…
Copyright © 2012 Russel Winder 26
public interface ClosureInterface<T> { T call(T t);}
final ClosureInterface<Integer> multiplyBy5 = new ClosureInterface<Integer>() { public Integer call(final Integer i) { return 5 * i; } };
multiplyBy5.call(4)
Copyright © 2012 Russel Winder 27
If Java had operator overloading, thiscould be made to look much nicer.
Copyright © 2012 Russel Winder 28
Just as it can in proper programming languages such as Groovy, Scala, C++, Python, etc.
Copyright © 2012 Russel Winder 29
final multiplyBy5 = {5 * it}
multiplyBy5(4)
multiplyBy5.call(4)
final multiplyBy5 = {i -> 5 * i}
Copyright © 2012 Russel Winder 30
def multiplyBy5(i: Int): Int = 5 * i
multiplyBy5(4)
Copyright © 2012 Russel Winder 31
But Java 8 can do stuff a bit like that…
Copyright © 2012 Russel Winder 32
final ClosureInterface<Integer> multiplyBy5 = i -> 5 * i;
multiplyBy5.call(4)
Copyright © 2012 Russel Winder 33
Nothing much revolutionary here,just a bit of syntactic sugar…
Copyright © 2012 Russel Winder 34
…true, but that isn't all there is…
Copyright © 2012 Russel Winder 35
A Middle
Copyright © 2012 Russel Winder 36
It all about where the iteration is.
Copyright © 2012 Russel Winder 37
Explicit iteration
vs.
Implicit iteration
Copyright © 2012 Russel Winder 38
Work with a (trivial) example:
Copyright © 2012 Russel Winder 39
Calculate the sum of the squares of the numbers between 0 and 100 that are
divisible by 7.
Copyright © 2012 Russel Winder 40
final List<Integer> numbers = new ArrayList<>();for (int i = 0; i < 100; ++i) { if (i % 7 == 0) { numbers.add(i); }}Integer sum = 0 ;for (final Integer i: numbers) { sum += i * i;}
Copyright © 2012 Russel Winder 41
(for (i <- 0 until 100; if i % 7 == 0) yield i * i).sum
Copyright © 2012 Russel Winder 42
sum(i * i for i in range(100) if i % 7 == 0)
Copyright © 2012 Russel Winder 43
(0 until 100).filter(i => i %7 == 0).map(i => i * i).sum
Copyright © 2012 Russel Winder 44
(0..100).findAll{i -> i % 7 == 0}.collect{i -> i * i}.sum()
Copyright © 2012 Russel Winder 45
(0..100).findAll{it % 7 == 0}.collect{it * it}.sum()
Copyright © 2012 Russel Winder 46
numbers.filter(i -> i % 7 == 0).map(i -> i * i).reduce(0, (t, x) -> t + x)
Huh?
Copyright © 2012 Russel Winder 47
final List<Integer> numbers = new ArrayList<>();for (int i = 0; i < 100; ++i) { numbers.add(i); }
Oh ffs.
Copyright © 2012 Russel Winder 48
Higher Order Functions
Copyright © 2012 Russel Winder 49
Functions that take functions as parameters and/or return functions as result.
Copyright © 2012 Russel Winder 50
Some may be thinking:Why do I give a f###
Copyright © 2012 Russel Winder 51
After all nothing good has happenedin Java since Java 1.4.2.
Copyright © 2012 Russel Winder 52
Copyright © 2012 Russel Winder 53
Because all computers arenow parallel computers.
Copyright © 2012 Russel Winder 54
Copyright © 2012 Russel Winder 55
We have to move to an attitude where we assume our software is not uniprocessor.
Copyright © 2012 Russel Winder 56
We have to actually do object-oriented and functional programming.
Copyright © 2012 Russel Winder 57
Instead of just saying we write Java and so are doing object-oriented programming.
Copyright © 2012 Russel Winder 58
numbers.parallel().filter(i -> i % 7 == 0). map(i -> i * i).reduce(0, (t, x) -> t + x)
Copyright © 2012 Russel Winder 59
GParsPool.withPool { value = (0..100).findAllParallel{it % 7 == 0}.collectParallel{it * it}.sum()}
Copyright © 2012 Russel Winder 60
GParsPool.withPool { value = (0..100).parallel.filter{it % 7 == 0}.map{it * it}.sum()}
Very Java 8.
Copyright © 2012 Russel Winder 61
def value = (0..100)ParallelEnhancer.enhanceInstance(value)value = value.parallel.filter{it % 7 == 0}.map{it * it}.sum()
Copyright © 2012 Russel Winder 62
def value = (0..100)ParallelEnhancer.enhanceInstance(value)value = value.findAllParallel{it % 7 == 0}.collectParallel{it * it}.sum()
Copyright © 2012 Russel Winder 63
GPars and Groovy give you Java 8 styleapproach and parallelism today
for Java with JDK6 or JDK7.
Copyright © 2012 Russel Winder 64
Guava, TotallyLazy, and FunctionalJava can be usedtoday to practice the functional approach using Java.
Or just install the JDK8 Lambda release.
Copyright © 2012 Russel Winder 65
Using Scala is an option fordoing functional programming*.
*And just ignore Java altogether.
Copyright © 2012 Russel Winder 66
(0 until 100).par.filter(i => i %7 == 0).map(i => i * i).sum
Copyright © 2012 Russel Winder 67
An End
Copyright © 2012 Russel Winder 68
Java is about to get the functional programming aspect.
Copyright © 2012 Russel Winder 69
Java is about to becomean object-oriented language.
Copyright © 2012 Russel Winder 70
Scala, Groovy, Python, C++, etc.already have object-oriented and functional.
Copyright © 2012 Russel Winder 71
It's all about how your data evolves.
It's not about the flow of control.
Copyright © 2012 Russel Winder 72
It's all about sending messages to your objects, requesting activity.
Copyright © 2012 Russel Winder 73
Closure the next “big thing” in Java?
Copyright © 2012 Russel Winder 74
Copyright © 2012 Russel Winder 75
Squirrels deny parallelism.
Copyright © 2012 Russel Winder 76
Copyright © 2012 Russel Winder 77
Yes*.
*But will everyone ignore it?
Copyright © 2012 Russel Winder 78
No.
Copyright © 2012 Russel Winder 79
It is not the lambda expressions in Java 8 that is the disruptive revolution.
It's the change to the Java library that is.
It's all about those defender methods.
Copyright © 2012 Russel Winder 80
Surreptitious Advertisement
Copyright © 2012 Russel Winder 81
The End
Copyright © 2012 Russel Winder 82
ClosuresThe Next “Big Thing” in Java?
Russel Winder
email: russel@winder.org.ukxmpp: russel@winder.org.uk
twitter: @russel_winder
Recommended