Skip to content

Commit

Permalink
Stream::unionWith(), Stream::unionCoercive() methods added and co…
Browse files Browse the repository at this point in the history
…vered by tests.
  • Loading branch information
Smoren committed Mar 7, 2023
1 parent 94d88bf commit d983b9e
Show file tree
Hide file tree
Showing 2 changed files with 214 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<iterable<mixed>> ...$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<iterable<mixed>> ...$iterables
*
* @return $this
*
* @see Set::unionCoercive()
*/
public function unionCoerciveWith(iterable ...$iterables): self
{
$this->iterable = Set::unionCoercive($this->iterable, ...$iterables);
return $this;
}

// STREAM DEBUG OPERATIONS

/**
Expand Down
176 changes: 176 additions & 0 deletions tests/Stream/SetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
],
];
}

Expand Down Expand Up @@ -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],
],
];
}

Expand Down Expand Up @@ -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],
],
];
}

Expand Down Expand Up @@ -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],
],
];
}

Expand Down

0 comments on commit d983b9e

Please sign in to comment.