You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
->reduce(fn(int $accumulator, int $element) => $accumulator + $element)
162
-
->getOrElse(0);
191
+
$collection = getCollection();
163
192
164
193
/**
165
194
* @return Option<float>
166
195
*/
167
-
function div(int $a, int $b): Option {
168
-
return 0 === $b
169
-
? Option::none()
170
-
: Option::some($a / $b)
196
+
function div(int $a, int $b): Option
197
+
{
198
+
return Option::when(0 !== $b, fn() => $a / $b);
171
199
}
172
200
173
201
/**
@@ -177,33 +205,43 @@ function div(int $a, int $b): Option {
177
205
* In this case the execution will short circuit (stop)
178
206
* and no Null Pointer Exception will be thrown.
179
207
*/
180
-
$emptyCollection
208
+
$collection
181
209
->first(fn(int $elem) => $elem > 0)
182
210
->map(fn(int $elem) => $elem + 1)
183
211
->flatMap(fn(int $elem) => div($elem, $elem - 1))
184
212
->getOrElse(0)
185
213
```
186
214
187
-
- Type assertions with Option monad via [PHP generators](https://www.php.net/manual/en/language.generators.syntax.php) based [do-notation](https://en.wikibooks.org/wiki/Haskell/do_notation) implementation
215
+
-[Do-notation](https://en.wikibooks.org/wiki/Haskell/do_notation) via [PHP generators](https://www.php.net/manual/en/language.generators.syntax.php):
216
+
188
217
```php
218
+
<?php
219
+
220
+
use Tests\Mock\Foo;
221
+
use Fp\Functional\Option\Option;
222
+
223
+
use function Fp\Evidence\proveTrue;
224
+
use function Fp\Evidence\proveNonEmptyList;
225
+
189
226
/**
190
227
* Inferred type is Option<Foo>
191
228
*/
192
229
$maybeFooMaybeNot = Option::do(function() use ($untrusted) {
230
+
// If $untrusted is not null then bind this value to $notNull
0 commit comments