Skip to content

Commit eb3b88d

Browse files
committed
Update README: Add union.
1 parent 2ee3cc8 commit eb3b88d

4 files changed

Lines changed: 122 additions & 1 deletion

File tree

README.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ Quick Reference
116116
| [`partialIntersectionCoercive`](#Partial-Intersection-Coercive) | Partial intersection with type coercion | `Set::partialIntersectionCoercive($minCount, ...$iterables)` |
117117
| [`symmetricDifference`](#Symmetric-Difference) | Symmetric difference of iterables | `Set::symmetricDifference(...$iterables)` |
118118
| [`symmetricDifferenceCoercive`](#Symmetric-Difference-Coercive) | Symmetric difference with type coercion | `Set::symmetricDifferenceCoercive(...$iterables)` |
119+
| [`union`](#Union) | Union of iterables | `Set::union(...$iterables)` |
120+
| [`unionCoercive`](#Union-Coercive) | Union with type coercion | `Set::unionCoercive(...$iterables)` |
119121

120122
#### Sort Iteration
121123
| Iterator | Description | Code Snippet |
@@ -227,6 +229,8 @@ Quick Reference
227229
| [`symmetricDifferenceWith`](#Symmetric-Difference-With) | Symmetric difference of iterable source and given iterables | `$this->symmetricDifferenceWith(...$iterables)` |
228230
| [`symmetricDifference CoerciveWith`](#Symmetric-Difference-Coercive-With) | Symmetric difference of iterable source and given iterables with type coercion | `$this->symmetricDifferenceCoerciveWith( ...$iterables)` |
229231
| [`takeWhile`](#Take-While-1) | Return elements from the iterable source as long as the predicate is true | `$stream->takeWhile($predicate)` |
232+
| [`unionWith`](#Union-With) | Union of stream with iterables | `$stream->unionWith(...$iterables)` |
233+
| [`unionCoerciveWith`](#Union-Coercive-With) | Union of stream with iterables with type coercion | `$stream->unionCoerciveWith(...$iterables)` |
230234
| [`zipWith`](#Zip-With) | Iterate iterable source with another iterable collections simultaneously | `$stream->zipWith(...$iterables)` |
231235
| [`zipLongestWith`](#Zip-Longest-With) | Iterate iterable source with another iterable collections simultaneously | `$stream->zipLongestWith(...$iterables)` |
232236
| [`zipEqualWith`](#Zip-Equal-With) | Iterate iterable source with another iterable collections of equal lengths simultaneously | `$stream->zipEqualWith(...$iterables)` |
@@ -1329,6 +1333,46 @@ foreach (Set::symmetricDifferenceCoercive($a, $b, $c) as $item) {
13291333
// 4, 5, 6, 7, 8, 9
13301334
```
13311335

1336+
### Union
1337+
Iterates the union of iterables.
1338+
1339+
```Set::union(iterable ...$iterables)```
1340+
1341+
If input iterables produce duplicate items, then [multiset](https://en.wikipedia.org/wiki/Multiset) union rules apply.
1342+
1343+
```php
1344+
use IterTools\Set;
1345+
1346+
$a = [1, 2, 3];
1347+
$b = [3, 4];
1348+
$c = [1, 2, 3, 6, 7];
1349+
1350+
foreach (Set::union($a, $b, $c) as $item) {
1351+
print($item);
1352+
}
1353+
//1, 2, 3, 4, 6, 7
1354+
```
1355+
1356+
### Union Coercive
1357+
Iterates the union of iterables with [type coercion](#Strict-and-Coercive-Types).
1358+
1359+
```Set::union(iterable ...$iterables)```
1360+
1361+
If input iterables produce duplicate items, then [multiset](https://en.wikipedia.org/wiki/Multiset) union rules apply.
1362+
1363+
```php
1364+
use IterTools\Set;
1365+
1366+
$a = ['1', 2, 3];
1367+
$b = [3, 4];
1368+
$c = [1, 2, 3, 6, 7];
1369+
1370+
foreach (Set::union($a, $b, $c) as $item) {
1371+
print($item);
1372+
}
1373+
//1, 2, 3, 4, 6, 7
1374+
```
1375+
13321376
## Sort Iteration
13331377
### ASort
13341378
Iterate the collection sorted while maintaining the associative key index relations.
@@ -2939,6 +2983,42 @@ $result = Stream::of($input)
29392983
// 1, -1, 2, -2
29402984
```
29412985

2986+
#### Union With
2987+
Return a stream consisting of the union of the stream and the input iterables.
2988+
2989+
```$stream->unionWith(iterable ...$iterables): Stream```
2990+
2991+
Note: If input iterables produce duplicate items, then [multiset](https://en.wikipedia.org/wiki/Multiset) union rules apply.
2992+
2993+
```php
2994+
use IterTools\Stream;
2995+
2996+
$input = [1, 2, 3];
2997+
2998+
$stream = Stream::of($input)
2999+
->unionWith([3, 4, 5, 6])
3000+
->toArray();
3001+
// [1, 2, 3, 4, 5, 6]
3002+
```
3003+
3004+
#### Union Coercive With
3005+
Return a stream consisting of the union of the stream and the input iterables using [type coercion](#Strict-and-Coercive-Types).
3006+
3007+
```$stream->unionWith(iterable ...$iterables): Stream```
3008+
3009+
Note: If input iterables produce duplicate items, then [multiset](https://en.wikipedia.org/wiki/Multiset) union rules apply.
3010+
3011+
```php
3012+
use IterTools\Stream;
3013+
3014+
$input = [1, 2, 3];
3015+
3016+
$stream = Stream::of($input)
3017+
->unionWith(['3', 4, 5, 6])
3018+
->toArray();
3019+
// [1, 2, 3, 4, 5, 6]
3020+
```
3021+
29423022
#### Zip With
29433023
Return a stream consisting of multiple iterable collections streamed simultaneously.
29443024

src/Set.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,6 @@ public static function union(iterable ...$iterables): \Generator
143143
return static::partialIntersection(1, ...$iterables);
144144
}
145145

146-
147146
/**
148147
* Iterates union of given iterables using type coercion.
149148
*

tests/Set/UnionCoerciveTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,27 @@
1111

1212
class UnionCoerciveTest extends \PHPUnit\Framework\TestCase
1313
{
14+
/**
15+
* @test unionCoercive example usage
16+
*/
17+
public function testExampleUsage(): void
18+
{
19+
// Given
20+
$a = ['1', 2, 3];
21+
$b = [3, 4];
22+
$c = [1, 2, 3, 6, 7];
23+
24+
// When
25+
$result = [];
26+
foreach (Set::unionCoercive($a, $b, $c) as $datum) {
27+
$result[] = $datum;
28+
}
29+
30+
// Then
31+
$expected = [1, 2, 3, 4, 6, 7];
32+
$this->assertEqualsCanonicalizing($expected, $result);
33+
}
34+
1435
/**
1536
* @dataProvider dataProviderForArraySets
1637
* @dataProvider dataProviderForArrayMultisets

tests/Set/UnionTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,27 @@
1111

1212
class UnionTest extends \PHPUnit\Framework\TestCase
1313
{
14+
/**
15+
* @test union example usage
16+
*/
17+
public function testExampleUsage(): void
18+
{
19+
// Given
20+
$a = [1, 2, 3];
21+
$b = [3, 4];
22+
$c = [1, 2, 3, 6, 7];
23+
24+
// When
25+
$result = [];
26+
foreach (Set::union($a, $b, $c) as $datum) {
27+
$result[] = $datum;
28+
}
29+
30+
// Then
31+
$expected = [1, 2, 3, 4, 6, 7];
32+
$this->assertEqualsCanonicalizing($expected, $result);
33+
}
34+
1435
/**
1536
* @dataProvider dataProviderForArraySets
1637
* @dataProvider dataProviderForArrayMultisets

0 commit comments

Comments
 (0)