diff --git a/src/Stream.php b/src/Stream.php index 5fb199a9..f73f4db4 100644 --- a/src/Stream.php +++ b/src/Stream.php @@ -876,6 +876,44 @@ public function partialIntersectionCoerciveWith(int $minIntersectionCount, itera return $this; } + /** + * Iterates union of iterable source and given iterables in strict type mode. + * + * - scalars: compares strictly by type; + * - objects: always treats different instances as not equal to each other; + * - arrays: compares serialized. + * + * @param array> ...$iterables + * + * @return $this + * + * @see Set::union() + */ + public function unionWith(iterable ...$iterables): self + { + $this->iterable = Set::union($this->iterable, ...$iterables); + return $this; + } + + /** + * Iterates union of iterable source and given iterables in non-strict type mode. + * + * - scalars: compares non-strictly by value; + * - objects: compares serialized; + * - arrays: compares serialized. + * + * @param array> ...$iterables + * + * @return $this + * + * @see Set::unionCoercive() + */ + public function unionCoerciveWith(iterable ...$iterables): self + { + $this->iterable = Set::unionCoercive($this->iterable, ...$iterables); + return $this; + } + // STREAM DEBUG OPERATIONS /** diff --git a/tests/Stream/SetTest.php b/tests/Stream/SetTest.php index 689f3f1f..b0c7942f 100644 --- a/tests/Stream/SetTest.php +++ b/tests/Stream/SetTest.php @@ -198,6 +198,50 @@ public function dataProviderForArray(): array ->toArray(), [4, 5, 6, 7, 8, 9], ], + [ + [ + [], + [2, 3, 4, 5, 6], + [3, 4, 5, 6, 7], + ], + fn (array $iterables) => Stream::of(array_shift($iterables)) + ->unionWith(...$iterables) + ->toArray(), + [2, 3, 4, 5, 6, 7], + ], + [ + [ + [1, 2, 3, 4, 5], + [2, 3, 4, 5, 6], + [3, 4, 5, 6, 7], + ], + fn (array $iterables) => Stream::of(array_shift($iterables)) + ->unionWith(...$iterables) + ->toArray(), + [1, 2, 3, 4, 5, 6, 7], + ], + [ + [ + [], + [2, 3, '4', 5, '6'], + [3, 4, 5, 6, 7], + ], + fn (array $iterables) => Stream::of(array_shift($iterables)) + ->unionCoerciveWith(...$iterables) + ->toArray(), + [2, 3, 4, 5, 6, 7], + ], + [ + [ + [1, '2', 3, '4', 5], + [2, '3', 4, '5', '6'], + [3, 4, 5, 6, 7], + ], + fn (array $iterables) => Stream::of(array_shift($iterables)) + ->unionCoerciveWith(...$iterables) + ->toArray(), + [1, 2, 3, 4, 5, 6, 7], + ], ]; } @@ -585,6 +629,50 @@ public function dataProviderForGenerators(): array ->toArray(), [4, 5, 6, 7, 8, 9], ], + [ + [ + $gen([]), + [2, 3, 4, 5, 6], + [3, 4, 5, 6, 7], + ], + fn (array $iterables) => Stream::of(array_shift($iterables)) + ->unionWith(...$iterables) + ->toArray(), + [2, 3, 4, 5, 6, 7], + ], + [ + [ + $gen([1, 2, 3, 4, 5]), + [2, 3, 4, 5, 6], + [3, 4, 5, 6, 7], + ], + fn (array $iterables) => Stream::of(array_shift($iterables)) + ->unionWith(...$iterables) + ->toArray(), + [1, 2, 3, 4, 5, 6, 7], + ], + [ + [ + $gen([]), + [2, 3, '4', 5, '6'], + [3, 4, 5, 6, 7], + ], + fn (array $iterables) => Stream::of(array_shift($iterables)) + ->unionCoerciveWith(...$iterables) + ->toArray(), + [2, 3, 4, 5, 6, 7], + ], + [ + [ + $gen([1, '2', 3, '4', 5]), + [2, '3', 4, '5', '6'], + [3, 4, 5, 6, 7], + ], + fn (array $iterables) => Stream::of(array_shift($iterables)) + ->unionCoerciveWith(...$iterables) + ->toArray(), + [1, 2, 3, 4, 5, 6, 7], + ], ]; } @@ -974,6 +1062,50 @@ public function dataProviderForIterators(): array ->toArray(), [4, 5, 6, 7, 8, 9], ], + [ + [ + $iter([]), + [2, 3, 4, 5, 6], + [3, 4, 5, 6, 7], + ], + fn (array $iterables) => Stream::of(array_shift($iterables)) + ->unionWith(...$iterables) + ->toArray(), + [2, 3, 4, 5, 6, 7], + ], + [ + [ + $iter([1, 2, 3, 4, 5]), + [2, 3, 4, 5, 6], + [3, 4, 5, 6, 7], + ], + fn (array $iterables) => Stream::of(array_shift($iterables)) + ->unionWith(...$iterables) + ->toArray(), + [1, 2, 3, 4, 5, 6, 7], + ], + [ + [ + $iter([]), + [2, 3, '4', 5, '6'], + [3, 4, 5, 6, 7], + ], + fn (array $iterables) => Stream::of(array_shift($iterables)) + ->unionCoerciveWith(...$iterables) + ->toArray(), + [2, 3, 4, 5, 6, 7], + ], + [ + [ + $iter([1, '2', 3, '4', 5]), + [2, '3', 4, '5', '6'], + [3, 4, 5, 6, 7], + ], + fn (array $iterables) => Stream::of(array_shift($iterables)) + ->unionCoerciveWith(...$iterables) + ->toArray(), + [1, 2, 3, 4, 5, 6, 7], + ], ]; } @@ -1363,6 +1495,50 @@ public function dataProviderForTraversables(): array ->toArray(), [4, 5, 6, 7, 8, 9], ], + [ + [ + $trav([]), + [2, 3, 4, 5, 6], + [3, 4, 5, 6, 7], + ], + fn (array $iterables) => Stream::of(array_shift($iterables)) + ->unionWith(...$iterables) + ->toArray(), + [2, 3, 4, 5, 6, 7], + ], + [ + [ + $trav([1, 2, 3, 4, 5]), + [2, 3, 4, 5, 6], + [3, 4, 5, 6, 7], + ], + fn (array $iterables) => Stream::of(array_shift($iterables)) + ->unionWith(...$iterables) + ->toArray(), + [1, 2, 3, 4, 5, 6, 7], + ], + [ + [ + $trav([]), + [2, 3, '4', 5, '6'], + [3, 4, 5, 6, 7], + ], + fn (array $iterables) => Stream::of(array_shift($iterables)) + ->unionCoerciveWith(...$iterables) + ->toArray(), + [2, 3, 4, 5, 6, 7], + ], + [ + [ + $trav([1, '2', 3, '4', 5]), + [2, '3', 4, '5', '6'], + [3, 4, 5, 6, 7], + ], + fn (array $iterables) => Stream::of(array_shift($iterables)) + ->unionCoerciveWith(...$iterables) + ->toArray(), + [1, 2, 3, 4, 5, 6, 7], + ], ]; }