Skip to content
Permalink
Browse files

Initial implementation of Collection::zip()

  • Loading branch information...
lorenzo committed May 16, 2015
1 parent a7a3e0c commit ac97df1f0a15fb3da2aaf4ff77c56bd4334f2342
Showing with 29 additions and 0 deletions.
  1. +13 −0 src/Collection/CollectionTrait.php
  2. +16 −0 tests/TestCase/Collection/CollectionTest.php
@@ -28,6 +28,7 @@
use Cake\Collection\Iterator\StoppableIterator;
use Cake\Collection\Iterator\TreeIterator;
use Cake\Collection\Iterator\UnfoldIterator;
use Cake\Collection\Iterator\ZipIterator;
use Iterator;
use LimitIterator;
use RecursiveIteratorIterator;
@@ -530,6 +531,18 @@ public function through(callable $handler)
return $result instanceof CollectionInterface ? $result: new Collection($result);
}
public function zip($items)
{
return new ZipIterator(array_merge([$this], func_get_args()));
}
public function zipWith($items, $callable)
{
$args = func_get_args();
$callable = array_pop($args);
return new ZipIterator($args, $callable);
}
/**
* {@inheritDoc}
*
@@ -1285,4 +1285,20 @@ public function testIsEmpty()
$collection = $collection->filter();
$this->assertTrue($collection->isEmpty());
}
/**
* Tests the zip() method
*
* @return void
*/
public function testZip()
{
$collection = new Collection([1, 2]);
$zipped = $collection->zip([3, 4]);
$this->assertEquals([[1, 3], [2, 4]], $zipped->toList());
$collection = new Collection([1, 2]);
$zipped = $collection->zip([3]);
$this->assertEquals([[1, 3], [2, null]], $zipped->toList());
}
}

0 comments on commit ac97df1

Please sign in to comment.
You can’t perform that action at this time.