Skip to content

Commit

Permalink
Minor style tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
DusanKasan committed Apr 11, 2016
1 parent a18c3f4 commit ed3bf03
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/collection_functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,10 @@ function drop($collection, $numberOfItems)
*/
function iterate($value, callable $function)
{
$generatorFactory = function () use ($value, $function) {
$duplicated = duplicate($value);
$generatorFactory = function () use ($duplicated, $function) {
$value = $duplicated;

yield $value;

while (true) {
Expand Down Expand Up @@ -714,7 +717,7 @@ function interpose($collection, $separator)
function interleave(...$collections)
{
$generatorFactory = function () use ($collections) {
$collections = array_map(
$normalizedCollection = array_map(
function ($collection) {
$c = (is_array($collection)) ? new ArrayIterator($collection) : $collection;
$c->rewind();
Expand All @@ -725,7 +728,7 @@ function ($collection) {

do {
$valid = false;
foreach ($collections as $collection) {
foreach ($normalizedCollection as $collection) {
if ($collection->valid()) {
yield $collection->key() => $collection->current();
$collection->next();
Expand Down Expand Up @@ -970,14 +973,14 @@ function partition($collection, $numberOfItems, $step = -1, $padding = [])
$generatorFactory = function () use ($collection, $numberOfItems, $step, $padding) {
$buffer = [];
$itemsToSkip = 0;
$step = $step ?: $numberOfItems;
$tmpStep = $step ?: $numberOfItems;

foreach ($collection as $key => $value) {
if (count($buffer) == $numberOfItems) {
yield dereferenceKeyValue($buffer);

$buffer = array_slice($buffer, $step);
$itemsToSkip = $step - $numberOfItems;
$buffer = array_slice($buffer, $tmpStep);
$itemsToSkip = $tmpStep - $numberOfItems;
}

if ($itemsToSkip <= 0) {
Expand Down Expand Up @@ -1073,10 +1076,12 @@ function($value) use ($key) {
function repeat($value, $times = -1)
{
$generatorFactory = function () use ($value, $times) {
while ($times != 0) {
$tmpTimes = $times;

while ($tmpTimes != 0) {
yield $value;

$times = $times < 0 ? -1 : $times - 1;
$tmpTimes = $tmpTimes < 0 ? -1 : $tmpTimes - 1;
}
};

Expand Down

0 comments on commit ed3bf03

Please sign in to comment.