Intro Programmation Fonctionnelle en
JAVA
M Billaud
12 novembre 2018
Préparatifs
Inscrivez-vous au cours Moodle
I [Link]
I Cours “LPRO-DAGPI Programmation Fonctionnelle”
I clé d’inscription pf2018
Outils de travail
I IDE (Netbeans ?)
I Java version 8 au moins.
Exemple : Faire afficher un List<Personne>
Style traditionnel (boucle)
for (Personne p : personnes) {
[Link]();
}
Style fonctionnel
[Link](Personne::afficher);
Explication
Le code
[Link](Personne::afficher);
/*--------------*/
I applique la méthode afficher() à chaque élément
I ici, son paramètre est une fonction (méthode)
Note
I forEach : méthode applicable à toutes les Collections
Démonstration : le code
Personne[] tableau = {
new Personne ("Kevin", 2003),
new Personne ("Julie", 1998),
new Personne ("Brice", 2002)
};
List<Personne> personnes
= [Link](tableau);
[Link](Personne::afficher);
Démonstration
Résultat
nom=Kevin annee=2003
nom=Julie annee=1998
nom=Brice annee=2002
Travail
1
I créez un projet Java
I Mettez en place le code (à prendre sur cours Moodle)
I Testez
2
I Avec forEach, arrangez-vous pour que le code affiche
Bonjour Kevin
Bonjour Julie
Bonjour Brice
Annexe : la classe Personne 1/2
class Personne {
String nom;
int annee;
Personne(String nom, int annee) {
[Link] = nom;
[Link] = annee;
}
// ...
}
Annexe : la Classe Personne 2/2
class Personne {
// ...
@Override
String toString() {
return "nom=" + nom + " annee=" + annee;
}
void afficher() {
[Link](this);
}
}
Autres possibilités
.forEach(objet :: méthode)
personnes
.forEach([Link] :: println);
I applique objet.méthode(...) à chaque élément
Rappel : println fait appel à la méthode toString de l’objet
à afficher.
Utilisation d’une Expression Fonctionnelle
Exemple : afficher seulement le nom
[Link]( p -> [Link]([Link]));
se lit
la fonction qui,
pour une personne p ->
affiche son nom println([Link])
Terminologie : fonction anonyme, lambda-expression. . .
Résumé
Plusieurs façons d’exprimer
[Link](p -> [Link]([Link]));
[Link]([Link]::println);
[Link](Personne::afficher);
avec la méthode [Link]
class List<E> {
void forEach(Consumer<? super E> action)
...
Le type Consumer<T>
Qu’est-ce que c’est
I une interface générique
I les fonctions qui “consomment” un objet de type T.
Définition
interface Consumer<T> {
void accept(T t);
...
}
Utilisation
Variables qui contiennent des fonctions. . .
Code
Consumer<Personne> action;
// action = p -> [Link]([Link]);
// action = [Link]::println;
action = Personne::afficher;
[Link](action);
Combiner des fonctions
Methode andThen de la classe Consumer
Exemple
Consumer<Personne> afficher_nom = ...;
Consumer<Personne> afficher_annee = ...;
Consumer<Personne> action = afficher_nom
. andThen(afficher_annee);
[Link](action);
Exercices
1. Completer
2. Définir des actions afficher_espace et sauter_ligne
3. les combiner à l’action
Des fonctions qui fabriquent des fonctions
I Une autre solution
[Link](afficher_nom
.andThen(afficher_chaine(" "))
.andThen(afficher_annee)
.andThen(afficher_chaine("\n")));
I La fonction afficher_chaine retourne une action
(Consumer).
Exercice
Définir la fonction
private static
Consumer<Personne> afficher_chaine(String chaine)
{
return ...
}
Conclusion
I Les fonctions implémentent des interfaces fonctionnelles
comme
I Consumer<T> – avec méthode void accept(T)
I Predicate<T> – avec méthode boolean test(T)
I Function<T,R> – avec méthode R apply(T)
I ...
I On peut
I en définir (méthodes, lambdas)
I les appliquer
I les manipuler dans des variables
I en fabriquer dynamiquement
I les composer
I ...
I Les fonctions sont des “objets de première classe”.
Version du document
copyright: M Billaud 2018
version initiale: 12 novembre 2018