Scala vs java 8

Post on 02-Jul-2015

1.866 views 1 download

description

Présenté au Paris Scala User Group #28, le 20 décembre 2012

Transcript of Scala vs java 8

ScalavsJava 8

List("Sarradin", "Armand") .map("François " + _)

2012-12-20

François SarradinXebiaTwitter: @fsarradinBlog: http://kerflyn.wordpress.com/

François ArmandNormation

Twitter: @fanf42

Dernier PSUGAvant la fin du monde

Pourquoi cette présentation (au PSUG ?)

!!! DISCLAIMER !!!● Pas de troll● Ni monade, ni typeclass● Pas de conflit d'intérêt● Vous avez le droit de parler aux chauffeurs

À venir

● Présentation de concepts

● Faire des trucs avec les concepts

● Parler des concepts

Java 8

Plus de Processeurs

Plus d'ExpressivitéPlus de Lisibilité

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

vs.

fact n = product [1..n]

Is it all Chinese to you?

FP & Concurrence

GuavaClojureC# / F#

C++

(Anonymous) Inner ClassUne Solution λ

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

@Overridepublic boolean apply(Person person) {

return person.getAge() >= 18;}

}); // Java + Guava

(Anonymous) Inner ClassUne Solution λ

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

@Overridepublic boolean apply(Person person) {

return person.getAge() >= 18;}

}); // Java + Guava

(Anonymous) Inner Class(Pas Vraiment) Une 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)

Annotation on Types (JSR 308)

Java 8 (JSR 337)

Productivité

Modularité

Performance

Project Jigsaw (JSR ?)

λ Project (JSR 335) Project Coin

(JSR ?)

Date and Time API (JSR 310)

Java 9

Nashorn

λ Project (JSR 335)

Virtual Extension

Method

Collection

λ Expression

API Extension

[New]x -> f(x)

[New]Parallel Collections

Type Inference Engine

Enhancement

[New] Interface Methoddefault void m()...

[New]Method ReferenceT::m T::new

obj::m

Change in Property

Inheritance [New]Static Method in

Interface

Une Lambda Expression

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

Une Lambda Expression

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

Une Lambda Expression

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

Scala(x:Int, y:Int) => x + y

Une Lambda Expression

(x, y) -> x + y

Scala(x, y) => x + y

D'autres formes de λ

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

}

x -> 2 * x

() -> 42

Inner Class vs λ Solution

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

@Overridepublic boolean apply(Person p) {

return p.getAge() >= 18;}

}); // Java 5-7 + Guava

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

λ = SAM(Single Abstract Method)

● Indépendant du type

● Une et une seule méthode abstraite

● Fonctionne avec○ Runnable, Callable, Comparator○ JDK's function API, Guava API○ types persos, ...

λ et variables externes

int x = 1;

y -> y + x;

x = 2;

=> Implicitement final

Method Reference

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

myBigDecimal::add// like: (n) -> myBigDecimal.add(n)

Arrays.sort(myInts, Integer::compare)

String::new// like: v -> new String(v)

Virtual Extension Method(VEM)

interface Message {

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

}

}

VEM : Dangereux ?

● Brian Goetz : NON○ Héritage de comportement uniquement

● François Sarradin : OUI O_O'○ Article : "Java 8: Now You Have Mixins?"○ (Voir les kata)

Faire évoluer les APIs

● "APIs need a mechanism for evolving, or they will become increasingly constraining to use, and replacing them is expensive." (Brian Goetz - 07/2012)

Parallel ComputingAussi simple que 1-2-3 ?

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

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

The Dream Team ;)

photo: @crazybob

Java 8Sites Web

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

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

Mailing Listmailto:lambda-dev@openjdk.java.nethttp://mail.openjdk.java.net/pipermail/lambda-dev/

Et maintenant,scéance de code

in vivo...

Questions ?

● Adoption de Java 8 / Scala ?

● Debuggage ?

● Utilisation des Lambda Java 8 en Scala ?