Download - Java 8 Lambda

Transcript
Page 1: Java 8 Lambda

JSR 335:Project LambdaFrançois Sarradin -- Xebia

λ

Page 2: Java 8 Lambda

JSR337

Java RoadmapJava 8

aug. 2013Java 7

jul. 2011 Java 9?

● FP / Parallel comp.● Date API improv.● Type annotation● Compact profiles● Nashorn

ModularityPerformanceProductivity

Page 3: Java 8 Lambda

Project LambdaWhat's in it?

Project Lambda

JSR335

Collection API Bulk Operations

Lambda Expression Virtual Extension Method

Collection API Parallel CollectionsFunctional API

Page 4: Java 8 Lambda

Question

What will be the impactof Project Lambdaon the Java world?

Page 5: Java 8 Lambda

AnswerLook at Speed Car!

Page 6: Java 8 Lambda

Planning...Presentation + live coding (free to interrupt)

● Genesis

● Lambda of Java

● Project Lambda

Page 7: Java 8 Lambda

...And Then● Debate

● Retrospective (agile style!)

● Report○ Xebia blog○ Project Lambda ML

Page 8: Java 8 Lambda

Genesis

Page 9: Java 8 Lambda

More Processors

Page 10: Java 8 Lambda

More ExpressivenessMore Readability

结果 汉字 = new 物().処理();

vs.

fact n = product [1..n]

Is it all Chinese to you?

Page 11: Java 8 Lambda

FP & Competition

Guava

Clojure

C# / F#C++

Page 12: Java 8 Lambda

(Anonymous) Inner ClassA λ Solution

Iterables.filter(persons, new Predicate<Person>() {

@Overridepublic boolean apply(Person person) {

return person.getAge() >= 18;}

}); // Java + Guava

Page 13: Java 8 Lambda

This is what you really need!

Iterables.filter(persons, IS_ADULT);

Predicate<Person> IS_ADULT= new Predicate<Person>() {

@Overridepublic boolean apply(Person person) {

return person.getAge() >= 18;}

});

(Anonymous) Inner ClassA (better?) λ Solution

Page 14: Java 8 Lambda

(Anonymous) Inner Class(Not Really) A λ Solution

Average length of Java one-liner is 21 line!@DEVOPS_BORAT - 12/09/2011

● "The pain of anonymous inner classes makes us roll our eyes in the back of our heads every day." (a wise Oracle's client, 01/2011)

Page 15: Java 8 Lambda

What about Oracle?

● "It’s time to add closures to Java" (Mark Reinhold, 12/2009)

● "Oracle's position is that Java must evolve -- carefully, of course -- in order to remain competitive." (Brian Goetz - 08/2011)

Page 16: Java 8 Lambda

Lambda of Java

Page 17: Java 8 Lambda

Here is the λ!

(int x, int y) -> x + y

(x, y) -> x + y

(int x, int y) -> { return x + y; }

(x, y) -> { return x + y; }

Page 18: Java 8 Lambda

Other λ forms

m -> {m.put("France", "Paris");m.put("UK", "London");m.put("USA", "Washington");

}

x -> 2 * x

() -> 42

Page 19: Java 8 Lambda

Inner Classvs. λ SolutionIterables.filter(persons, new Predicate<Person>() {

@Overridepublic boolean apply(Person p) {

return p.getAge() >= 18;}

}); // Java 5-7 + Guava

Iterables.filter(persons, // Java 8 + Guavap -> p.getAge() >= 18);

Page 20: Java 8 Lambda

Method Reference

String::valueOf// like: v -> String.valueOf(v)

Integer::compare// like: (i1, i2) -> Integer.compare(i1, i2)

Arrays.sort(myInts, Integer::compare)

Page 21: Java 8 Lambda

Virtual Extension Method(VEM)

interface Message {

String getMessage() default {return "Look Ma'! Interface with code!";

}

}

Page 22: Java 8 Lambda

VEMMotivation

Help to extend existing APIs

Minimize rewrite of existing API implementation

eg. Collection API

eg. Hibernate Collection Implementation

VEM

Page 23: Java 8 Lambda

Collection API Extended

java.util.functions.*

java.util.streams.*

Extension of Iterable, Iterator, Collection, Map, ...

java.util.Optional

Page 24: Java 8 Lambda

Parallel ComputingAs Easy as 1-2-3?

myCollection.parallel().filter(x -> ...).map(x -> ...).reduce((x, y) -> ...) // parallel

myCollection.stream().filter(x -> ...).map(x -> ...).reduce((x, y) -> ...) // sequential

Page 25: Java 8 Lambda

Some kata and demos...

Page 26: Java 8 Lambda

Project Lambda

Page 27: Java 8 Lambda

The Dream Team ;)

photo: @crazybob

Page 28: Java 8 Lambda

Web Sites

OpenJDK 8 Web sitehttp://openjdk.java.net/projects/lambda/

JSR335http://jcp.org/en/jsr/detail?id=335

Page 29: Java 8 Lambda

Where to Get Java 8

Mercurial repohttp://hg.openjdk.java.net/lambda/lambda

Java SE 8 Early Access (with lambda)http://jdk8.java.net/lambda/

Page 30: Java 8 Lambda

Mailing Lists

Technical Discussionmailto:[email protected]://mail.openjdk.java.net/pipermail/lambda-dev/

Libs Spec Discussionhttp://mail.openjdk.java.net/pipermail/lambda-libs-spec-<*>/

Spec Discussionhttp://mail.openjdk.java.net/pipermail/lambda-spec-<*>/

Page 31: Java 8 Lambda

Around Project Lambda

OpenJDK OSX Build (7 & 8)http://code.google.com/p/openjdk-osx-build/

Java 8 vs Scala: a Feature Comparisonhttp://www.infoq.com/articles/java-8-vs-scala

H. Gomez

U. PeterS. van den Berg

Page 32: Java 8 Lambda

Around Project LambdaIn France

Curious about Project Lambda ;)http://blog.xebia.fr/ [FR]http://kerflyn.wordpress.com/

JDK 8: lambdas in Action [FR]http://www.devoxx.com/display/FR12/JDK+8+demo++lambdas+in+Action

Java 8 et les Lambda [FR]http://thecodersbreakfast.net/index.php?post/2012/05/30/Java-8-et-les-Lambda

G. Tardif

O. Croisier

Guess Who?

Page 33: Java 8 Lambda

Project Lambdaand the Community● Transparency

○ Presentations / Publications○ Source code available

● Early testing by community○ Pre-version available○ AdoptOpenJDK (http://java.

net/projects/adoptopenjdk/pages/AdoptOpenJDK)

● Quick feedback○ ML, Hackdays

Page 34: Java 8 Lambda

Question● Oneliner, Readability, and Debugability

● Java vs. Competitors

● What to do for Java 8 to be adopted in projects?

● Is there a future for Java?