Skip to content
This repository was archived by the owner on Dec 28, 2023. It is now read-only.

Commit 1b19a9a

Browse files
committed
Merge pull request #4 from emonkak/change-to-static-method
Change Collection::union(), concat(), zip() to static method
2 parents 7a3f6db + 8859474 commit 1b19a9a

File tree

6 files changed

+83
-23
lines changed

6 files changed

+83
-23
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,7 @@ Collection::iterate(0, function($n) { return $n + 1; })
2828
// 6
2929
// 8
3030
```
31+
32+
## Documentation
33+
34+
Please see [Wiki](https://github.com/emonkak/php-collection/wiki). (but wiki pages are Japanese only)

benchmarks/ConcatEvent.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ public function arrayImpl()
2323

2424
protected function execute($xs)
2525
{
26-
foreach ($xs->concat($this->data) as $x);
26+
foreach ($xs->concatWith($this->data) as $x);
2727
}
2828
}

benchmarks/MethodChainEvent.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ protected function execute($xs)
3535
$result = $xs
3636
->filter(function($x) { return $x % 2 === 0; })
3737
->map(function($x) { return $x * 2; })
38-
->concat($this->data)
39-
->zip(Collection::range(0, INF))
38+
->concatWith($this->data)
39+
->zipWith(Collection::range(0, INF))
4040
->take(100);
4141
foreach ($result as $x);
4242
}

src/Collection.php

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Emonkak\Collection\Provider\CollectionProviderInterface;
66
use Emonkak\Collection\Provider\GeneratorProvider;
77
use Emonkak\Collection\Provider\IteratorProvider;
8+
use Emonkak\Collection\Selector\ValueSelector;
89
use Emonkak\Collection\Utils\Iterators;
910

1011
class Collection implements \IteratorAggregate
@@ -28,7 +29,7 @@ public static function from($source)
2829
return new Collection($source, self::$defaultProvider);
2930
}
3031

31-
public static function combine($sources)
32+
public static function concat($sources)
3233
{
3334
if (!Iterators::isTraversable($sources)) {
3435
$type = gettype($sources);
@@ -41,6 +42,14 @@ public static function combine($sources)
4142
);
4243
}
4344

45+
public static function iterate($initial, $f)
46+
{
47+
return new Collection(
48+
self::$defaultProvider->iterate($initial, $f),
49+
self::$defaultProvider
50+
);
51+
}
52+
4453
public static function range($start, $stop = null, $step = 1)
4554
{
4655
if ($stop === null) {
@@ -53,18 +62,26 @@ public static function range($start, $stop = null, $step = 1)
5362
);
5463
}
5564

56-
public static function iterate($initial, $f)
65+
public static function repeat($value, $n = null)
5766
{
5867
return new Collection(
59-
self::$defaultProvider->iterate($initial, $f),
68+
self::$defaultProvider->repeat($value, $n),
6069
self::$defaultProvider
6170
);
6271
}
6372

64-
public static function repeat($value, $n = null)
73+
public static function union($sources)
6574
{
6675
return new Collection(
67-
self::$defaultProvider->repeat($value, $n),
76+
self::$defaultProvider->uniq(self::$defaultProvider->concat($sources), ValueSelector::getInstance()),
77+
self::$defaultProvider
78+
);
79+
}
80+
81+
public static function zip($sources)
82+
{
83+
return new Collection(
84+
self::$defaultProvider->zip($sources),
6885
self::$defaultProvider
6986
);
7087
}

src/Enumerable.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -590,9 +590,9 @@ public function without()
590590
return $this->difference(func_get_args());
591591
}
592592

593-
public function union()
593+
public function unionWith()
594594
{
595-
return call_user_func_array([$this, 'concat'], func_get_args())->uniq();
595+
return call_user_func_array([$this, 'concatWith'], func_get_args())->uniq();
596596
}
597597

598598
public function intersection()
@@ -624,7 +624,7 @@ public function uniq($selector = null)
624624
return $this->newCollection($this->getProvider()->uniq($xs, $selector));
625625
}
626626

627-
public function zip()
627+
public function zipWith()
628628
{
629629
$xss = array_merge([$this->getSource()], func_get_args());
630630
return $this->newCollection($this->getProvider()->zip($xss));
@@ -660,7 +660,7 @@ public function sort($comparer = null)
660660
});
661661
}
662662

663-
public function concat()
663+
public function concatWith()
664664
{
665665
$xss = array_merge([$this->getSource()], func_get_args());
666666
return $this->newCollection($this->getProvider()->concat($xss));

tests/AbstractCollectionTest.php

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,17 @@ public function testFromThrowsInvalidArgumentException()
3939
}
4040

4141
/**
42-
* @dataProvider provideCombine
42+
* @dataProvider provideConcat
4343
*/
44-
public function testCombine($sources, $expected)
44+
public function testConcat($sources, $expected)
4545
{
46-
$collection = Collection::combine($sources);
46+
$collection = Collection::concat($sources);
4747

4848
$this->assertSame($expected, $collection->toList());
4949
$this->assertSame(Collection::getDefaultProvider(), $collection->getProvider());
5050
}
5151

52-
public function provideCombine()
52+
public function provideConcat()
5353
{
5454
return [
5555
[[], []],
@@ -60,9 +60,9 @@ public function provideCombine()
6060
/**
6161
* @expectedException \InvalidArgumentException
6262
*/
63-
public function testCombineThrowsInvalidArgumentException()
63+
public function testConcatThrowsInvalidArgumentException()
6464
{
65-
Collection::combine(1);
65+
Collection::concat(1);
6666
}
6767

6868
public function testRange()
@@ -110,6 +110,31 @@ public function testRepeat()
110110
$this->assertEmpty($result);
111111
}
112112

113+
public function testUnion()
114+
{
115+
$result = Collection::union([[1, 2, 3], [2, 30, 1], [1, 40]])->toList();
116+
$shouldBe = [1, 2, 3, 30, 40];
117+
$this->assertSame($shouldBe, $result, 'takes the union of a list of arrays');
118+
119+
$result = Collection::union([[1, 2, 3], [2, 30, 1], [1, 40, [1]]])->toList();
120+
$shouldBe = [1, 2, 3, 30, 40, [1]];
121+
$this->assertSame($shouldBe, $result, 'takes the union of a list of nested arrays');
122+
}
123+
124+
public function testZip()
125+
{
126+
$names = ['moe', 'larry', 'curly'];
127+
$ages = [30, 40, 50];
128+
$leaders = [true, false];
129+
130+
$result = Collection::zip([$names, $ages, $leaders])->toList();
131+
$expected = [
132+
['moe', 30, true],
133+
['larry', 40, false]
134+
];
135+
$this->assertSame($expected, $result);
136+
}
137+
113138
/**
114139
* @dataProvider provideCollectionFactory
115140
*/
@@ -1203,13 +1228,13 @@ public function testIntersection($factory)
12031228
/**
12041229
* @dataProvider provideCollectionFactory
12051230
*/
1206-
public function testUnion($factory)
1231+
public function testUnionWith($factory)
12071232
{
1208-
$result = $factory([1, 2, 3])->union([2, 30, 1], [1, 40])->toList();
1233+
$result = $factory([1, 2, 3])->unionWith([2, 30, 1], [1, 40])->toList();
12091234
$shouldBe = [1, 2, 3, 30, 40];
12101235
$this->assertSame($shouldBe, $result, 'takes the union of a list of arrays');
12111236

1212-
$result = $factory([1, 2, 3])->union([2, 30, 1], [1, 40, [1]])->toList();
1237+
$result = $factory([1, 2, 3])->unionWith([2, 30, 1], [1, 40, [1]])->toList();
12131238
$shouldBe = [1, 2, 3, 30, 40, [1]];
12141239
$this->assertSame($shouldBe, $result, 'takes the union of a list of nested arrays');
12151240
}
@@ -1231,13 +1256,13 @@ public function testDifference($factory)
12311256
/**
12321257
* @dataProvider provideCollectionFactory
12331258
*/
1234-
public function testZip($factory)
1259+
public function testZipWith($factory)
12351260
{
12361261
$names = ['moe', 'larry', 'curly'];
12371262
$ages = [30, 40, 50];
12381263
$leaders = [true, false];
12391264

1240-
$stooges = $factory($names)->zip($ages, $leaders)->toList();
1265+
$stooges = $factory($names)->zipWith($ages, $leaders)->toList();
12411266
$shouldBe = [
12421267
['moe', 30, true],
12431268
['larry', 40, false]
@@ -1419,6 +1444,20 @@ public function testSort($factory)
14191444
$this->assertEmpty($result);
14201445
}
14211446

1447+
/**
1448+
* @dataProvider provideConcat
1449+
*/
1450+
public function testConcatWith($sources, $expected)
1451+
{
1452+
$result = Collection::from([1, 2])->concatWith([3, 4, 5])->toList();
1453+
$expected = [1, 2, 3, 4, 5];
1454+
$this->assertSame($expected, $result);
1455+
1456+
$result = Collection::from([])->concatWith([])->toList();
1457+
$expected = [];
1458+
$this->assertSame($expected, $result);
1459+
}
1460+
14221461
/**
14231462
* @dataProvider provideCollectionFactory
14241463
*/

0 commit comments

Comments
 (0)