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?
4. 1. FIRST CLASS FUNCTION
Définition
“Les fonctions sont traitées par le langage
comme des valeurs de première classe.”
5. 1. FIRST CLASS FUNCTION
Code:
Fnto<Srn,Srn >hlo=(tigs - "el "+s
ucin tig tig
el
Srn ) > hlo
;
Resultat:
hlo;
el
/ BsCnet$Lmd$@0e9
/ aeocps$aba1a9e2
/ ! BsCnet@0391
/ = aeocps3f99
hloapy"run);
el.pl(Eoa"
/ hloEoa
/ el run
6. 2. HIGH ORDER FUNCTION
Définition
“C'est une fonction qui prend une ou plusieurs
fonctions comme entrée et/ou qui renvoie une
fonction”
7. 2. HIGH ORDER FUNCTION
Code:
Fnto<Srn,Srn >tie Fnto<Srn,Srn >f){
ucin tig tig
wc( ucin tig tig
rtr (tigs - fapy fapys )
eun Srn ) > .pl( .pl() ;
}
Resultat:
tiehlo.pl(Eoa";
wc(el)apy"run)
/ hlohloEoa
/ el el run
8. 3. FUNCTOR
Définition
“C'est une collection d'éléments X qui peut
s'appliquer une fonction f: X -> Y pour créer une
collection Y”
9. 3. FUNCTOR - MAP
Code:
Ls<Srn >mp Fnto<Srn,Srn >f
it tig
a( ucin tig tig
,Ls<Srn >vle ){
it tig
aus
Ls<Srn >tRtr =nwAryit >)
it tig
oeun
e raLs< (;
fr Srn cret:vle ){
o( tig urn
aus
tRtr.d(fapycret )
oeunad .pl(urn) ;
}
rtr tRtr;
eun oeun
}
Resultat:
Ls<Srn >cns=
it tig
of
Ary.sit nwSrn[{jg,"eox,"aan" )
rasaLs( e tig]"u" dvx" jvoe} ;
mp s- stUprae) cns)
a(
> .opeCs(, of ;
/ [U,DVX,JVOE
/ JG EOX AAN]
Avec les Streams Java 8:
cnssra(.a(s- stUprae))clet Cletr.oit))
of.tem)mp
> .opeCs( .olc( olcostLs(
10. 3. FUNCTOR - FILTER
Code:
Ls<Srn >fle(Fnto<Srn,Boen>f
it tig
itr ucin tig ola
,
Ls<Srn >vle ){
it tig
aus
Ls<Srn >tRtr =nwAryit >)
it tig
oeun
e raLs< (;
fr Srn cret:vle ){
o( tig urn
aus
i (fapycret )
f
.pl(urn)
tRtr.d(cret)
oeunad urn ;
}
rtr tRtr;
eun oeun
}
Resultat:
Ls<Srn >cns=
it tig
of
Ary.sit nwSrn[{jg,"eox,"aan" )
rasaLs( e tig]"u" dvx" jvoe} ;
fle( - scnan(j) cns ;
itrs > .otis"", of)
/ [u,jvoe
/ jg aan]
Avec les Streams Java 8:
cnssra(.itr s- scnan(j))cletCletr.oit)
of.tem)fle(
> .otis"" .olc(olcostLs()