La programmation fonctionnelle,
à quoi ça rime ?
@JoGrenat
Disclaimer
![](https://s3.amazonaws.com/media-p.slid.es/uploads/329768/images/11432872/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/329768/images/11432889/pasted-from-clipboard.png)
Kesako?
function() {
}
![](https://s3.amazonaws.com/media-p.slid.es/uploads/329768/images/11397862/Capture_d_e%CC%81cran_2024-06-10_a%CC%80_11.20.43.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/329768/images/11397862/Capture_d_e%CC%81cran_2024-06-10_a%CC%80_11.20.43.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/329768/images/11397862/Capture_d_e%CC%81cran_2024-06-10_a%CC%80_11.20.43.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/329768/images/11397862/Capture_d_e%CC%81cran_2024-06-10_a%CC%80_11.20.43.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/329768/images/11397862/Capture_d_e%CC%81cran_2024-06-10_a%CC%80_11.20.43.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/329768/images/11397862/Capture_d_e%CC%81cran_2024-06-10_a%CC%80_11.20.43.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/329768/images/11397862/Capture_d_e%CC%81cran_2024-06-10_a%CC%80_11.20.43.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/329768/images/11397862/Capture_d_e%CC%81cran_2024-06-10_a%CC%80_11.20.43.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/329768/images/11397862/Capture_d_e%CC%81cran_2024-06-10_a%CC%80_11.20.43.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/329768/images/11397862/Capture_d_e%CC%81cran_2024-06-10_a%CC%80_11.20.43.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/329768/images/11398279/pasted-from-clipboard.png)
Mutabilité
Effets de bord
Mutabilité
const items = ['Ballon', 'Fourchette'];
items.push('Clé');
console.log(items);
// ['Ballon', 'Fourchette', 'Clé']
const items = ['Ballon', 'Fourchette'];
const newItems = [...items, 'Clé'];
console.log(items);
// ['Ballon', 'Fourchette']
Effets de bord
let currentUser = null;
async function createUser() {
const user = { name: 'Jordane' };
await db.add(user);
currentUser = user;
console.log('User created');
}
await createUser();
Fonction pure
function add(a, b) {
return a + b;
}
add(1, 3); 4
add(1, 3); 4
Mutabilité
Effets de bord
LANGAGE UTILE
LANGGE UTILE
LANGGE UTLE
LANGE UTLE
LNGE UTLE
LNGE ULE
LNG ULE
NG ULE
NGULE
NGUL
NUL
![](https://s3.amazonaws.com/media-p.slid.es/uploads/329768/images/11432988/pasted-from-clipboard.png)
GOTO Considered Harmful
Poka-Yoke
Avoid
Mistakes
![](https://s3.amazonaws.com/media-p.slid.es/uploads/329768/images/11432966/pasted-from-clipboard.png)
Mutabilité
Effets de bord
Mutabilité
import moment from 'moment';
const talkDate = moment('2024-07-04 11:20:00');
const alarm = talkDate.subtract(10, 'minutes');
setAlarm(alarm);
startPresentationAt(talkDate);
![](https://s3.amazonaws.com/media-p.slid.es/uploads/329768/images/11435967/pasted-from-clipboard.png)
Effets de bord
function createUser(name) {
db.createUser({ name, createdAt: moment() });
}
createUser('Jordane');
function createUser(name) {
db.createUser({ name, createdAt: moment() });
}
createUser('Jordane');
![](https://s3.amazonaws.com/media-p.slid.es/uploads/329768/images/11435824/stockage-de-donnees.png)
{ name: 'Jordane', createdAt: '2024-01-02' }
it('should create user', async () => {
// Given
db = mockDatabase();
// When
await createUser('Jordane');
// Then
const users = await db.getAll();
expect(users[0]?.name).toEqual('Jordane');
// After
cleanDatabaseMock();
});
![](https://s3.amazonaws.com/media-p.slid.es/uploads/329768/images/11435802/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/329768/images/11435926/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/329768/images/11435896/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/329768/images/11430508/pasted-from-clipboard.png)
La perfection n'est pas atteinte quand il n'y a plus rien à ajouter, mais quand il n'y a plus rien à enlever.
Antoine de Saint-Exupéry
Programme
![](https://s3.amazonaws.com/media-p.slid.es/uploads/329768/images/11435975/separation.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/329768/images/11435976/essai.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/329768/images/11435977/stockage-de-donnees.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/329768/images/11435981/la-gestion-du-stress.png)
Functional
Core
Imperative
Shell
![](https://s3.amazonaws.com/media-p.slid.es/uploads/329768/images/11437635/Capture_d_e%CC%81cran_2024-06-26_a%CC%80_13.56.00.png)
https://www.youtube.com/watch?v=P1vES9AgfC4
Scott Wlaschin
Add
Add
Add
Add
Functional
Core
Imperative
Shell
function add(a, b) {
return a + b;
}
function add(a, b) {
return a + b;
}
async function shell() {
const a = await askUser('First number?');
const b = await askUser('Second number?');
const result = add(a, b);
console.log(`${a} + ${b} = ${result}`);
}
Programme
Runtime
Donnée
![](https://s3.amazonaws.com/media-p.slid.es/uploads/329768/images/11438740/procede-de-design.png)
Donnée
{ method: 'GET', url: 'https://www.perdu.com' }
{ status: 200, content: 'Vous êtes perdu ?' }
Tout est donnée, même les fonctions
function multiplyBy2(a) {
return a * 2;
}
[1, 2, 3].map(multiplyBy2);
first-class citizen
Tout est expression
Même les conditions
function isMajeur(age) {
return age >= 18
? 'Majeur·e'
: 'Mineur·e';
}
function isMajeur(age) {
if (age > 18) {
console.log('Majeur·e');
}
}
function factorielle(until) {
let result = 1;
for(let i = 1; i <= until; i++) {
result *= i;
}
return result;
}
function factorielle(number) {
return number < 2
? 1
: number * factorielle(number - 1);
}
Mutabilité
Effets de bord
Statements
Fonctions
Fonction
Lambda-calcul
const numbers = [1, 2, 3];
numbers.map(x => x * 2);
const add = (a, b) => a + b;
add(1, 3) === 4;
const add = (a) => {
return (b) => a + b
}
add(1)(3) === 4;
const add3 = add(3);
add3(1) === 4;
Currification
Application partielle
-
map
-
filter
-
reduce
const map = (fn) => (array) => {
// Transform values
}
const getNames = map(user => user.name);
getNames(myUsers);
const double = x => x * 2;
const add = x => y => x + y;
const result = add(4)(double(3)); // 10
const result = 3
|> double
|> add(4)
(3)
(6)
= 6
Pipe
const result = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|> filter(n => n % 2 === 0)
|> map(a => a * 10);
Pipe 🤝 Currification 🤝 Application partielle
Statements
const noUnderage = (age: number) {
if (age < 18) {
throw new Error('Vous devez être majeur·e pour voter');
}
return 'Allez voter dimanche !';
}
type Either =
| { success: true, value: string; }
| { success: false, error: Error; }
const noUnderage = (age: number): Either {
if (age < 18) {
return {
success: false,
error: new Error('Vous devez être majeur·e pour voter')
};
}
return { success: true, value: 'Allez voter dimanche !' };
}
type Either<E, A> =
| { type: 'left', value: E }
| { type: 'right', value: A }
const divide = (num: number) => (den: number): Either<string, number> => {
return den === 0
? E.left('Cannot divide by 0')
: E.right(num / den);
}
const result = divide(6, 2);
function divide: number -> number -> Either<string, number>
![](https://s3.amazonaws.com/media-p.slid.es/uploads/329768/images/11445111/boite-en-carton.png)
const add3 = (a: number) => a + 3;
const result =
divide(6)(2)
|> add3
const add3 = a => a + 3;
const result =
divide(6)(2)
|> E.map(add3)
const result = [1, 2, 3]
|> map(x => x * 2)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/329768/images/11445111/boite-en-carton.png)
Monades
![](https://s3.amazonaws.com/media-p.slid.es/uploads/329768/images/11445111/boite-en-carton.png)
A
![](https://s3.amazonaws.com/media-p.slid.es/uploads/329768/images/11445111/boite-en-carton.png)
B
f(A) → B
![](https://s3.amazonaws.com/media-p.slid.es/uploads/329768/images/11445111/boite-en-carton.png)
Either
![](https://s3.amazonaws.com/media-p.slid.es/uploads/329768/images/11445111/boite-en-carton.png)
Array
![](https://s3.amazonaws.com/media-p.slid.es/uploads/329768/images/11445111/boite-en-carton.png)
Promise
getUser().then(user => user.name);
Théorie des Catégories
![](https://s3.amazonaws.com/media-p.slid.es/uploads/329768/images/11446188/modele-3d.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/329768/images/11446189/calcul.png)
I call it my billion-dollar mistake. It was the invention of the null reference in 1965. [...] This has led to innumerable errors, vulnerabilities, and system crashes [...]
– Tony Hoare –
number
3.17
-6
0
98438I9
...
boolean
true
false
null
null
Mutabilité
Effets de bord
Statements
Null & undefined
![](https://s3.amazonaws.com/media-p.slid.es/uploads/329768/images/11445111/boite-en-carton.png)
Option
const getUser = (id: number) => (users: User[]): Option<User> => {
const user = users.find(user => user.id === id);
return user ? O.some(user) : O.none;
}
const username = myUsers
|> getUser(123)
|> O.map(user => user.name);
![](https://s3.amazonaws.com/media-p.slid.es/uploads/329768/images/11445111/boite-en-carton.png)
IO
getLine
|> IO.map(text => writeText(`Vous avez écrit: ${text}`));
Ce qui semble compliqué, ce sont les outils
Améliorer la lisibilité et réduire la charge cognitive
Rendre explicites les dépendances
Supprimer toute une gamme d'erreurs
Utiliser des outils prouvés
Mettre les données et leur transformation au centre
Avoir un code testable
Comment
commencer ?
![](https://s3.amazonaws.com/media-p.slid.es/uploads/329768/images/3551998/Elm_logo.svg.png)
Elm: A delightful language
for reliable web applications.
fp-ts: Bibliothèque fonctionnelle pour TypeScript
![](https://s3.amazonaws.com/media-p.slid.es/uploads/329768/images/11446315/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/329768/images/11262602/Capture_d_e%CC%81cran_2024-04-14_a%CC%80_15.37.18.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/329768/images/11452746/Capture_d_e%CC%81cran_2024-07-04_a%CC%80_09.39.26.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/329768/images/11437617/pasted-from-clipboard.png)
Merci !
@JoGrenat
La programmation fonctionnelle, à quoi ça rime ?
By ereold
La programmation fonctionnelle, à quoi ça rime ?
- 197