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
* develop:
specify next release
fix calling ->distinct() to many times
do not unwrap the whole set to remove an element
add Set::safeguard
fix test
add Sequence::safeguard
This method allows you to make sure all values conforms to an assertion before continuing using the sequence.
507
+
508
+
```php
509
+
$uniqueFiles = Sequence::of('a', 'b', 'c', 'a')
510
+
->safeguard(
511
+
Set::strings()
512
+
static fn(Set $names, string $name) => match ($names->contains($name)) {
513
+
true => throw new \LogicException("$name is already used"),
514
+
false => $names->add($name),
515
+
},
516
+
);
517
+
```
518
+
519
+
This example will throw because there is the value `a` twice.
520
+
521
+
This method is especially useful for deferred or lazy sequences because it allows to make sure all values conforms after this call whithout unwrapping the whole sequence first. The downside of this lazy evaluation is that some operations may start before reaching a non conforming value (example below).
522
+
523
+
```php
524
+
Sequence::lazyStartingWith('a', 'b', 'c', 'a')
525
+
->safeguard(
526
+
Set::strings()
527
+
static fn(Set $names, string $name) => match ($names->contains($name)) {
528
+
true => throw new \LogicException("$name is already used"),
529
+
false => $names->add($name),
530
+
},
531
+
)
532
+
->foreach(static fn($name) => print($name));
533
+
```
534
+
535
+
This example will print `a`, `b` and `c` before throwing an exception because of the second `a`. Use this method carefully.
This method allows you to make sure all values conforms to an assertion before continuing using the set.
346
+
347
+
```php
348
+
$uniqueFiles = Set::of(
349
+
new \ArrayObject(['name' => 'a']),
350
+
new \ArrayObject(['name' => 'b']),
351
+
new \ArrayObject(['name' => 'c']),
352
+
new \ArrayObject(['name' => 'a']),
353
+
)
354
+
->safeguard(
355
+
Set::strings()
356
+
static fn(Set $names, string $value) => match ($names->contains($value['name'])) {
357
+
true => throw new \LogicException("{$value['name']} is already used"),
358
+
false => $names->add($value['name']),
359
+
},
360
+
);
361
+
```
362
+
363
+
This example will throw because there is the value `a` twice.
364
+
365
+
This method is especially useful for deferred or lazy sets because it allows to make sure all values conforms after this call whithout unwrapping the whole set first. The downside of this lazy evaluation is that some operations may start before reaching a non conforming value (example below).
366
+
367
+
```php
368
+
Set::lazy(function() {
369
+
yield new \ArrayObject(['name' => 'a']);
370
+
yield new \ArrayObject(['name' => 'b']);
371
+
yield new \ArrayObject(['name' => 'c']);
372
+
yield new \ArrayObject(['name' => 'a']);
373
+
})
374
+
->safeguard(
375
+
Set::strings()
376
+
static fn(Set $names, string $value) => match ($names->contains($value['name'])) {
377
+
true => throw new \LogicException("{$value['name']} is already used"),
0 commit comments