|
6 | 6 | - [Examples](#Examples) |
7 | 7 | - [Type assertions with Option](#Type-assertions-with-Option) |
8 | 8 | - [Filter chaining](#Filter-chaining) |
| 9 | + - [Filter Option](#Filter-Option) |
| 10 | + - [List of all errors](#List-of-all-errors) |
| 11 | + - [Traverse](#Traverse) |
9 | 12 |
|
10 | 13 | # Option monad |
11 | 14 |
|
@@ -334,4 +337,82 @@ function filterTGenericObjectTypeParam(Atomic $atomic): Option |
334 | 337 | default => null |
335 | 338 | })); |
336 | 339 | } |
| 340 | +``` |
| 341 | + |
| 342 | + - #### Filter Option |
| 343 | + |
| 344 | +If you want to apply an operation that returns `Option` for each element |
| 345 | +and collect only `Option::some` use `filterMap`: |
| 346 | + |
| 347 | +``` php |
| 348 | +<?php |
| 349 | + |
| 350 | +use Fp\Collections\ArrayList; |
| 351 | +use Fp\Functional\Option\Option; |
| 352 | + |
| 353 | +// Inferred as ArrayList<int> |
| 354 | +// Result is ArrayList(3, 4, 5, 6, 7) |
| 355 | +$result = ArrayList::collect([1, 2, 3, 4, 5, 6, 7]) |
| 356 | + ->filterMap(fn($i) => $i > 5 ? Option::none() : Option::some($i + 2)); |
| 357 | +``` |
| 358 | + |
| 359 | + - #### List of all errors |
| 360 | + |
| 361 | +If you want to apply an operation that returns `Either` for each element |
| 362 | +and want collect all errors, use can use `partitionMap`+`toEither` |
| 363 | + |
| 364 | +``` php |
| 365 | +<?php |
| 366 | + |
| 367 | +use Fp\Collections\ArrayList; |
| 368 | +use Fp\Functional\Either\Either; |
| 369 | + |
| 370 | +// Inferred as Either<ArrayList<string>, ArrayList<int>> |
| 371 | +// Result is Left(ArrayList('6 is greater than 5', '7 is greater than 5')) |
| 372 | +$result = ArrayList::collect([1, 2, 3, 4, 5, 6, 7]) |
| 373 | + ->partitionMap( |
| 374 | + fn($i) => $i > 5 |
| 375 | + ? Either::left("{$i} is greater than 5") |
| 376 | + : Either::right($i), |
| 377 | + ) |
| 378 | + ->toEither(); |
| 379 | +``` |
| 380 | + |
| 381 | + - #### Traverse |
| 382 | + |
| 383 | +If you want to apply operation for each element, but `$callback` returns |
| 384 | +`Option` or `Either`, use can use `traverseOption`/`traverseEither`: |
| 385 | + |
| 386 | +``` php |
| 387 | +<?php |
| 388 | + |
| 389 | +use Fp\Collections\ArrayList; |
| 390 | +use Fp\Functional\Option\Option; |
| 391 | +use Tests\Mock\Foo; |
| 392 | +use Tests\Mock\Bar; |
| 393 | + |
| 394 | +/** |
| 395 | +* @param ArrayList<Foo|Bar> $list |
| 396 | +* @return Option<ArrayList<Foo>> |
| 397 | + */ |
| 398 | +function assertAllFoo(ArrayList $list): Option |
| 399 | +{ |
| 400 | + return $list->traverseOption( |
| 401 | + fn(Foo|Bar $item) => $item instanceof Foo |
| 402 | + ? Option::some($item) |
| 403 | + : Option::none(), |
| 404 | + ); |
| 405 | +} |
| 406 | + |
| 407 | +$fooAndBarItems = ArrayList::collect([new Foo(a: 42), new Bar(a: true)]); |
| 408 | + |
| 409 | +// Inferred type ArrayList<Foo> |
| 410 | +// Result is ArrayList() |
| 411 | +$noItems = assertAllFoo($items)->getOrElse(ArrayList::empty()); |
| 412 | + |
| 413 | +$fooItems = ArrayList::collect([new Foo(a: 42), new Foo(a: 43)]); |
| 414 | + |
| 415 | +// Inferred type ArrayList<Foo> |
| 416 | +// Result is ArrayList(Foo(a: 42), Foo(a: 43)) |
| 417 | +$noItems = assertAllFoo($fooItems)->getOrElse(ArrayList::empty()); |
337 | 418 | ``` |
0 commit comments