les concepts de la programmation fonctionnelle illustrés avec java 8
DESCRIPTION
Support du lighting talk donné au JUG Bordeaux qui est basé sur le talk de Bodil Stokke: What Every Hipster Should Know About Functional Programming. C'est un petit exercice basé sur la question suivante: peut-on transposer tous les exemples JS de son talk en Java 8 avec des lambdas?TRANSCRIPT
LES CONCEPTS DE LA PROGRAMMATIONFONCTIONNELLE ILLUSTRÉS AVEC JAVA 8
/ Yannick Chartois @ychartois
BODIL STOKKEWhat Every Hipster Should Know About Functional
Programming / Vimeo Parleys
QUESTIONMaintenant que nous allons avoir les lambdas, peut-on faire la
même chose en Java 8?
1. FIRST CLASS FUNCTIONDéfinition
“Les fonctions sont traitées par le langagecomme des valeurs de première classe.”
1. FIRST CLASS FUNCTIONCode:
Function< String, String > hello = (String s) -> "hello " + s;
Resultat:hello ;// BaseConcepts$$Lambda$1@a09ee92// != BaseConcepts@30f39991
hello.apply("Erouan") ;// hello Erouan
2. HIGH ORDER FUNCTIONDéfinition
“C'est une fonction qui prend une ou plusieursfonctions comme entrée et/ou qui renvoie une
fonction”
2. HIGH ORDER FUNCTIONCode:
Function< String, String > twice( Function< String, String > f ) { return (String s) -> f.apply( f.apply(s) );}
Resultat:twice(hello).apply("Erouan");// hello hello Erouan
3. FUNCTORDéfinition
“C'est une collection d'éléments X qui peuts'appliquer une fonction f: X -> Y pour créer une
collection Y”
3. FUNCTOR - MAPCode:
List< String > map( Function< String, String > f , List< String > values ) { List< String > toReturn = new ArrayList< >(); for( String current : values ) { toReturn.add( f.apply(current) ); } return toReturn;}
Resultat:List< String > confs = Arrays.asList( new String[]{"jug", "devoxx", "javaone"} );
map( s -> s.toUpperCase(), confs );
// [JUG, DEVOXX, JAVAONE]
Avec les Streams Java 8:confs.stream().map( s -> s.toUpperCase() ).collect( Collectors.toList() )
3. FUNCTOR - FILTERCode:
List< String > filter( Function< String, Boolean > f, List< String > values ) { List< String > toReturn = new ArrayList< >(); for( String current : values ) { if ( f.apply(current) ) toReturn.add( current ); } return toReturn;}
Resultat:List< String > confs = Arrays.asList( new String[]{"jug", "devoxx", "javaone"} );
filter(s -> s.contains("j"), confs) ;
// [jug, javaone]
Avec les Streams Java 8:confs.stream().filter( s -> s.contains("j") ).collect(Collectors.toList())
4. COMBINATORDéfinition
“C'est une function qui définie une nouvellefonction à partir de ses arguments ou d'autre
combinators”
4. COMBINATOR - NULL COMBINATORConstat:
List< String > confs2 = Arrays.asList( new String[] {"jug", "devoxx", "javaone", null} );
map( s -> s.toUpperCase(), confs2);// Exception in thread "main" java.lang.NullPointerException
Code:Function< String, String > nullCheck( Function< String, String > f ) { return (String s) -> s == null ? "null" : f.apply(s);}
Resultat:map( nullCheck(s -> s.toUpperCase()), confs2)// [JUG, DEVOXX, JAVAONE, null]
5. COMPOSITIONDéfinition
“Combine plusieurs fonctions pour créer unenouvelle fonction”
5. COMPOSITIONCode:
Function< String, String > compose ( Function< String, String > f1, Function< String, String > f2 ) { return (String s) -> f1.apply( f2.apply(s) );}
Resultat:Function< String, String > up = (String s) -> s.toUpperCase();Function< String, String > hello = (String s) -> "hello " + s;
up.apply( hello.apply("Erouan") );
compose( up, hello).apply("Erouan") ;// HELLO EROUAN
Avec Java 8:hello.andThen(up).apply("Erouan")up.compose(hello).apply("Erouan")
END !LES SOURCES: PROGFONCJAVA8
TWITTER: @YCHARTOIS