## Monades : ### Paradigme unique pour la programation (a)synchrone
- Baptiste Langlade - Lyon - Efalia - 10+ ans XP - ~90 packages open source Notes: - main XP PHP mais aussi d'autres langages - packages pour du crawling - exceptions => monades (cf conf) - passerelles
## Asynchrone Notes: - Problématique IO - Mono thread
![](parallelism.png)
![](async.png)
### Exercice - Lire un fichier par ligne - Une fonction avec un type explicite - Fonctionne en synchrone et asynchrone
### Réactif ```php use React\EventLoop\Loop; $stream = \fopen('some-file', 'r'); $lines = []; Loop::addReadStream($stream, function($stream) use (&$lines) { $lines[] = \fgets($stream); }); Loop::run(); ``` Notes: - React/AMP - Sync et Async - Pas de type de retour

Impératif

Fiber

## Compromis Notes: - Chaque solution a un mérite - Complexité du choix
## Monades
- `Maybe` - `Either` - `Set` - `Sequence` - `State` - `Validation` - `Free` - ...
- `Sequence` (`innmind/immutable`)
- Conteneur immuable - Une méthode `map` - Une méthode `flatMap` - Une/des méthode(s) d'extraction Notes: - Schrödinger

foreach

$numbers = [1, 2, 3, 4];
$double = static fn(int $i): int => $i * 2;
$doubles = [];
foreach ($numbers as $i) {
    $doubles[] = $double($i);
}
$doubles === [2, 4, 6, 8];
                        

map

$numbers = [1, 2, 3, 4];
$double = static fn(int $i): int => $i * 2;
$doubles = \array_map(
    static fn(int $i): int => $double($i),
    $numbers,
);
$doubles === [2, 4, 6, 8];
                        

flatMap

$numbers = [1, 2, 3, 4];
$double = static fn(int $i): int => $i * 2;
$doubles = \array_flatMap(
    static fn(int $i): array => [$i, $double($i)],
    $numbers,
);
$doubles === [1, 2, 2, 4, 3, 6, 4, 8];
                        

flatMap

$numbers = [1, 2, 3, 4];
$isEven = static fn(int $i): bool => $i % 2 === 0;
$even = \array_flatMap(
    static fn(int $i): array => match ($isEven($i)) {
        true => [$i],
        false => [],
    },
    $numbers,
);
$even === [2, 4];
                        

Sequence

$numbers = Sequence::of(1, 2, 3, 4);
$double = static fn(int $i): int => $i * 2;
$doubles = $numbers->map(
    static fn(int $i): int => $double($i),
);
$doubles->toList() === [2, 4, 6, 8];
                        

Sequence

$numbers = Sequence::of(1, 2, 3, 4);
$double = static fn(int $i): int => $i * 2;
$doubles = $numbers->flatMap(
    static fn(int $i): Sequence => Sequence::of($i, $double($i)),
);
$doubles->toList() === [1, 2, 2, 4, 3, 6, 4, 8];
                        
### Sequence - `->toList()` - `->reduce()` - `->foreach()` - ...
```php read('some-file') ->map(static fn($line) => \strtoupper($line)) ->foreach(function(string $line) { echo $line; }); ``` Notes: Question de style Quel interet ?
`innmind/operating-system`

innmind/operating-system

clock filesystem
processes volumes
sockets http
sql signals
sleep fork

innmind/async-operating-system

clock filesystem
processes volumes
sockets http
sql signals
sleep fork
```php use Innmind\OperatingSystem\OperatingSystem; function myApp(OperatingSystem $os): mixed { // your code here } ``` Notes: - Coeur de l'app agnostic - Permet de switch le contexte de façon transparente
## Contexte Execution Notes: HTTP vs CLI
### Async HTTP
`innmind/framework` Notes: Contexte d'execution == Main
```php use Innmind\Framework\Main\Http; use Innmind\Framework\Main\Cli; ```
```php use Innmind\Framework\Main\Async\Http; ```
## Demo
## Déclaratif
## Futur Notes: - Ouvre portes à d'autre outils (ie actors) - Passerelles - Connaissance trans langages
## PHP is [~dead~ alive] 🐈 Notes: Référence à Schrödinger
## Questions ![](joind.png) Twitter @Baptouuuu Github @Baptouuuu/talks