|
| 1 | +# `Fold` |
| 2 | + |
| 3 | +The `Fold` monad is intented to work with _(infinte) stream of data_ by folding each element to a single value. This monad distinguishes between the type used to fold and the result type, this allows to inform the _stream_ that it's no longer necessary to extract elements as the folding is done. |
| 4 | + |
| 5 | +An example is reading from a socket as it's an infinite stream of strings: |
| 6 | + |
| 7 | +```php |
| 8 | +$socket = \stream_socket_client(/* args */); |
| 9 | +/** @var Fold<string, list<string>, list<string>> */ |
| 10 | +$fold = Fold::with([]); |
| 11 | + |
| 12 | +do { |
| 13 | + // production code should wait for the socket to be "ready" |
| 14 | + $line = \fgets($socket); |
| 15 | + |
| 16 | + if ($line === false) { |
| 17 | + $fold = Fold::fail('socket not readable'); |
| 18 | + } |
| 19 | + |
| 20 | + $fold = $fold |
| 21 | + ->map(static fn($lines) => \array_merge($lines, [$line])) |
| 22 | + ->flatMap(static fn($lines) => match (\end($lines)) { |
| 23 | + "quit\n" => Fold::result($lines), |
| 24 | + default => Fold::with($lines), |
| 25 | + }); |
| 26 | + $continue = $fold->match( |
| 27 | + static fn() => true, // still folding |
| 28 | + static fn() => false, // got a result so stop |
| 29 | + static fn() => false, // got a failure so stop |
| 30 | + ); |
| 31 | +} while ($continue); |
| 32 | + |
| 33 | +$fold->match( |
| 34 | + static fn() => null, // unreachable in this case because no more folding outside the loop |
| 35 | + static fn($lines) => \var_dump($lines), |
| 36 | + static fn($failure) => throw new \RuntimeException($failure), |
| 37 | +); |
| 38 | +``` |
| 39 | + |
| 40 | +This example will read all lines from the socket until one line contains `quit\n` then the loop will stop and either dump all the lines to the output or `throw new RuntimeException('socket not reachable')`. |
| 41 | + |
| 42 | +## `::with()` |
| 43 | + |
| 44 | +This named constructor accepts a value with the notion that more elements are necessary to compute a result |
| 45 | + |
| 46 | +## `::result()` |
| 47 | + |
| 48 | +This named constructor accepts a _result_ value meaning that folding is finished. |
| 49 | + |
| 50 | +## `::fail()` |
| 51 | + |
| 52 | +This named constructor accepts a _failure_ value meaning that the folding operation failed and no _result_ will be reachable. |
| 53 | + |
| 54 | +## `->map()` |
| 55 | + |
| 56 | +This method allows to transform the value being folded. |
| 57 | + |
| 58 | +```php |
| 59 | +$fold = Fold::with([])->map(static fn(array $folding) => new \ArrayObject($folding)); |
| 60 | +``` |
| 61 | + |
| 62 | +## `->flatMap()` |
| 63 | + |
| 64 | +This method allows to both change the value and the _state_, for example switching from _folding_ to _result_. |
| 65 | + |
| 66 | +```php |
| 67 | +$someElement = /* some data */; |
| 68 | +$fold = Fold::with([])->flatMap(static fn($elements) => match ($someElement) { |
| 69 | + 'finish' => Fold::result($elements), |
| 70 | + default => Fold::with(\array_merge($elements, [$someElement])), |
| 71 | +}); |
| 72 | +``` |
| 73 | + |
| 74 | +## `->mapResult()` |
| 75 | + |
| 76 | +Same as [`->map()`](#map) except that it will transform the _result_ value when there is one. |
| 77 | + |
| 78 | +## `->mapFailure()` |
| 79 | + |
| 80 | +Same as [`->map()`](#map) except that it will transform the _failure_ value when there is one. |
| 81 | + |
| 82 | +## `->maybe()` |
| 83 | + |
| 84 | +This will return the _terminal_ value of the folding, meaning either a _result_ or a _failure_. |
| 85 | + |
| 86 | +```php |
| 87 | +Fold::with([])->maybe()->match( |
| 88 | + static fn() => null, // not called as still folding |
| 89 | + static fn() => doStuff(), // called as it is still folding |
| 90 | +); |
| 91 | +Fold::result([])->maybe()->match( |
| 92 | + static fn($either) => $either->match( |
| 93 | + static fn($result) => $result, // the value here is the array passed to ::result() above |
| 94 | + static fn() => null, // not called as it doesn't contain a failure |
| 95 | + ), |
| 96 | + static fn() => null, // not called as we have a result |
| 97 | +); |
| 98 | +Fold::fail('some error')->maybe()->match( |
| 99 | + static fn($either) => $either->match( |
| 100 | + static fn() => null, // not called as we have a failure |
| 101 | + static fn($error) => var_dump($error), // the value here is the string passed to ::fail() above |
| 102 | + ), |
| 103 | + static fn() => null, // not called as we have a result |
| 104 | +); |
| 105 | +``` |
| 106 | + |
| 107 | +## `->match()` |
| 108 | + |
| 109 | +This method allows to extract the value contained in the object. |
| 110 | + |
| 111 | +```php |
| 112 | +Fold::with([])->match( |
| 113 | + static fn($folding) => doStuf($folding), // value from ::with() |
| 114 | + static fn() => null, // not called |
| 115 | + static fn() => null, // not called |
| 116 | +); |
| 117 | +Fold::result([])->match( |
| 118 | + static fn() => null, // not called |
| 119 | + static fn($result) => doStuf($result), // value from ::result() |
| 120 | + static fn() => null, // not called |
| 121 | +); |
| 122 | +Fold::fail('some error')->match( |
| 123 | + static fn() => null, // not called |
| 124 | + static fn() => null, // not called |
| 125 | + static fn($error) => doStuf($error), // value from ::fail() |
| 126 | +); |
| 127 | +``` |
0 commit comments