Skip to content
Permalink
Browse files

Documenting the ZipIterator

  • Loading branch information...
lorenzo committed May 16, 2015
1 parent c8c60fb commit 2911a03e9ba88043d67c8c9ac00a2584a7fb5d9b
Showing with 39 additions and 2 deletions.
  1. +8 −0 src/Collection/CollectionTrait.php
  2. +31 −2 src/Collection/Iterator/ZipIterator.php
@@ -531,11 +531,19 @@ public function through(callable $handler)
return $result instanceof CollectionInterface ? $result: new Collection($result);
}
/**
* {@inheritDoc}
*
*/
public function zip($items)
{
return new ZipIterator(array_merge([$this], func_get_args()));
}
/**
* {@inheritDoc}
*
*/
public function zipWith($items, $callable)
{
$items = [$items];
@@ -25,17 +25,40 @@
* ### Example
*
* {{{
* $iterator = new ZipIterator([1, 2], [3, 4]);
* $iterator->toList(); // Returns [[1, 2], [3, 4]]
* $iterator = new ZipIterator([[1, 2], [3, 4]]);
* $iterator->toList(); // Returns [[1, 3], [2, 4]]
* }}}
*
* You can also chose a custom function to zip the elements together, such
* as doing a sum by index:
*
* ### Example
* {{{
* $iterator = new ZipIterator([[1, 2], [3, 4]], function ($a, $b) {
* return $a + $b;
* });
* $iterator->toList(); // Returns [3, 6]
* }}}
*/
class ZipIterator extends MultipleIterator implements CollectionInterface
{
use CollectionTrait;
/**
* The function to use for zipping items together
*
* @var callable
*/
protected $_callback;
/**
* Creates the iterator to merge together the values by for all the passed
* iterators by their corresponding index.
*
* @param array $sets The list of array or iterators to be zipped.
* @param callable $callable The function to use for zipping the elements of each iterator.
*/
public function __construct(array $sets, $callable = null)
{
$sets = array_map(function ($items) {
@@ -50,6 +73,12 @@ public function __construct(array $sets, $callable = null)
}
}
/**
* Returns the value resulting out of zipping all the elements for all the
* iterators with the same positional index.
*
* @return mixed
*/
public function current()
{
if ($this->_callback === null) {

0 comments on commit 2911a03

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