Skip to content

Commit

Permalink
refactor: Use For loop instead of Foreach.
Browse files Browse the repository at this point in the history
So we do not call rewind before the loop.
  • Loading branch information
drupol committed Dec 21, 2020
1 parent a1d636f commit 47e0f5e
Show file tree
Hide file tree
Showing 17 changed files with 80 additions and 44 deletions.
9 changes: 6 additions & 3 deletions src/Operation/Apply.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,19 @@ public function __invoke(): Closure
* @psalm-return Generator<TKey, T>
*/
static function (Iterator $iterator) use ($callbacks): Generator {
foreach ($iterator as $key => $value) {
for (; true === $iterator->valid(); $iterator->next()) {
$key = $iterator->key();
$current = $iterator->current();

foreach ($callbacks as $callback) {
if (true === $callback($value, $key)) {
if (true === $callback($current, $key)) {
continue;
}

break;
}

yield $key => $value;
yield $key => $current;
}
};
}
Expand Down
9 changes: 6 additions & 3 deletions src/Operation/Associate.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,15 @@ static function (Iterator $iterator) use ($callbackForKeys, $callbackForValues):
*/
static fn ($initial, callable $callback, int $callbackId, Iterator $iterator) => $callback($initial, $key, $value, $iterator);

foreach ($iterator as $key => $value) {
for (; $iterator->valid(); $iterator->next()) {
$key = $iterator->key();
$current = $iterator->current();

/** @psalm-var Generator<int, TKey|T> $k */
$k = FoldLeft::of()($callbackFactory($key)($value))($key)(new ArrayIterator($callbackForKeys));
$k = FoldLeft::of()($callbackFactory($key)($current))($key)(new ArrayIterator($callbackForKeys));

/** @psalm-var Generator<int, T|TKey> $c */
$c = FoldLeft::of()($callbackFactory($key)($value))($value)(new ArrayIterator($callbackForValues));
$c = FoldLeft::of()($callbackFactory($key)($current))($current)(new ArrayIterator($callbackForValues));

yield $k->current() => $c->current();
}
Expand Down
8 changes: 5 additions & 3 deletions src/Operation/Chunk.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@ static function (Iterator $iterator) use ($sizes): Generator {

$values = [];

foreach ($iterator as $value) {
for (; $iterator->valid(); $iterator->next()) {
$current = $iterator->current();

if (0 >= $sizesIterator->current()) {
return new EmptyIterator();
}

if (count($values) !== $sizesIterator->current()) {
$values[] = $value;
$values[] = $current;

continue;
}
Expand All @@ -56,7 +58,7 @@ static function (Iterator $iterator) use ($sizes): Generator {

yield $values;

$values = [$value];
$values = [$current];
}

return yield $values;
Expand Down
8 changes: 5 additions & 3 deletions src/Operation/Collapse.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ public function __invoke(): Closure
* @psalm-return Generator<TKey, T>
*/
static function (Iterator $iterator): Generator {
foreach ($iterator as $value) {
if (false === is_iterable($value)) {
for (; $iterator->valid(); $iterator->next()) {
$current = $iterator->current();

if (false === is_iterable($current)) {
continue;
}

yield from $value;
yield from $current;
}
};
}
Expand Down
6 changes: 4 additions & 2 deletions src/Operation/Contains.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ static function (Iterator $iterator) use ($values): Generator {
// yield count($values) === Intersect::of()(...$values)($iterator);
// But it would not be very optimal because it would have to traverse
// the whole iterator.
foreach ($iterator as $value) {
for (; $iterator->valid(); $iterator->next()) {
$current = $iterator->current();

foreach ($values as $k => $v) {
if ($v === $value) {
if ($v === $current) {
unset($values[$k]);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Operation/DropWhile.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __invoke(): Closure
{
return
/**
* @psalm-param callable(T, TKey, Iterator<TKey, T>):bool $callback
* @psalm-param callable(T, TKey, Iterator<TKey, T>): bool ...$callbacks
*
* @psalm-return Closure(Iterator<TKey, T>): Generator<TKey, T>
*/
Expand Down
11 changes: 7 additions & 4 deletions src/Operation/Duplicate.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@ public function __invoke(): Closure
static function (Iterator $iterator): Generator {
$stack = [];

foreach ($iterator as $key => $value) {
if (true === in_array($value, $stack, true)) {
yield $key => $value;
for (; $iterator->valid(); $iterator->next()) {
$key = $iterator->key();
$current = $iterator->current();

if (true === in_array($current, $stack, true)) {
yield $key => $current;
}

$stack[] = $value;
$stack[] = $current;
}
};
}
Expand Down
13 changes: 8 additions & 5 deletions src/Operation/Flatten.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ public function __invoke(): Closure
* @psalm-param Iterator<TKey, T> $iterator
*/
static function (Iterator $iterator) use ($depth): Generator {
foreach ($iterator as $key => $value) {
if (false === is_iterable($value)) {
yield $key => $value;
for (; $iterator->valid(); $iterator->next()) {
$key = $iterator->key();
$current = $iterator->current();

if (false === is_iterable($current)) {
yield $key => $current;

continue;
}
Expand All @@ -41,10 +44,10 @@ static function (Iterator $iterator) use ($depth): Generator {
/** @psalm-var callable(Iterator<TKey, T>): Generator<TKey, T> $flatten */
$flatten = Flatten::of()($depth - 1);

$value = $flatten(new IterableIterator($value));
$current = $flatten(new IterableIterator($current));
}

yield from $value;
yield from $current;
}
};
}
Expand Down
4 changes: 2 additions & 2 deletions src/Operation/Flip.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
final class Flip extends AbstractOperation
{
/**
* @psalm-return Closure(Iterator<TKey, T>): Closure(Iterator<T, TKey>): Generator<T, TKey>
* @psalm-return Closure(Iterator<T, TKey>): Generator<TKey, T>
*/
public function __invoke(): Closure
{
Expand All @@ -40,7 +40,7 @@ public function __invoke(): Closure
*/
static fn ($carry, $key, $value) => $key;

/** @psalm-var Closure(Iterator<TKey, T>): Generator<T, TKey> $associate */
/** @psalm-var Closure(Iterator<T, TKey>): Generator<TKey, T> $associate */
$associate = Associate::of()($callbackForKeys)($callbackForValues);

// Point free style.
Expand Down
7 changes: 5 additions & 2 deletions src/Operation/Intersperse.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,15 @@ static function (Iterator $iterator) use ($element, $atEvery, $startAt): Generat
);
}

foreach ($iterator as $key => $value) {
for (; $iterator->valid(); $iterator->next()) {
$key = $iterator->key();
$current = $iterator->current();

if (0 === $startAt++ % $atEvery) {
yield $element;
}

yield $key => $value;
yield $key => $current;
}
};
}
Expand Down
7 changes: 5 additions & 2 deletions src/Operation/Map.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,11 @@ static function (Iterator $iterator) use ($callbacks): Generator {
*/
static fn ($carry, callable $callback) => $callback($carry, $key, $iterator);

foreach ($iterator as $key => $value) {
yield $key => array_reduce($callbacks, $callbackFactory($key), $value);
for (; $iterator->valid(); $iterator->next()) {
$key = $iterator->key();
$current = $iterator->current();

yield $key => array_reduce($callbacks, $callbackFactory($key), $current);
}
};
}
Expand Down
4 changes: 2 additions & 2 deletions src/Operation/Pad.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ public function __invoke(): Closure
static function (Iterator $iterator) use ($size, $padValue): Generator {
$y = 0;

foreach ($iterator as $key => $value) {
for (; $iterator->valid(); $iterator->next()) {
++$y;

yield $key => $value;
yield $iterator->key() => $iterator->current();
}

while ($y++ < $size) {
Expand Down
7 changes: 5 additions & 2 deletions src/Operation/Reduction.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@ public function __invoke(): Closure
* @psalm-return Generator<TKey, T|null>
*/
static function (Iterator $iterator) use ($callback, $initial): Generator {
foreach ($iterator as $key => $value) {
yield $key => ($initial = $callback($initial, $value, $key, $iterator));
for (; $iterator->valid(); $iterator->next()) {
$key = $iterator->key();
$current = $iterator->current();

yield $key => ($initial = $callback($initial, $current, $key, $iterator));
}
};
}
Expand Down
11 changes: 7 additions & 4 deletions src/Operation/Split.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,14 @@ static function (Iterator $iterator) use ($type, $callbacks): Generator {
*/
static fn (bool $carry, callable $callback): bool => $callback($value, $key) || $carry;

foreach ($iterator as $key => $value) {
$callbackReturn = array_reduce($callbacks, $reducer($key)($value), false);
for (; $iterator->valid(); $iterator->next()) {
$key = $iterator->key();
$current = $iterator->current();

$callbackReturn = array_reduce($callbacks, $reducer($key)($current), false);

if (Splitable::AFTER === $type) {
$carry[] = $value;
$carry[] = $current;
}

if (Splitable::REMOVE === $type && true === $callbackReturn) {
Expand All @@ -85,7 +88,7 @@ static function (Iterator $iterator) use ($type, $callbacks): Generator {
}

if (Splitable::BEFORE === $type || Splitable::REMOVE === $type) {
$carry[] = $value;
$carry[] = $current;
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/Operation/TakeWhile.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ static function (Iterator $iterator) use ($callbacks): Generator {
*/
static fn (bool $carry, callable $callable): bool => ($callable($current, $key, $iterator)) ? $carry : false;

foreach ($iterator as $key => $current) {
for (; true === $iterator->valid(); $iterator->next()) {
$key = $iterator->key();
$current = $iterator->current();

$result = array_reduce(
$callbacks,
$reducerCallback($key)($current)($iterator),
Expand Down
7 changes: 5 additions & 2 deletions src/Operation/Transpose.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __invoke(): Closure
*
* @psalm-return Generator<TKey, list<T>>
*/
static function (Iterator $iterator): Iterator {
static function (Iterator $iterator): Generator {
$mit = new MultipleIterator(MultipleIterator::MIT_NEED_ANY);

foreach ($iterator as $iterableIterator) {
Expand All @@ -54,7 +54,10 @@ static function (Iterator $iterator): Iterator {
*/
static fn (array $carry, array $key, array $value): array => $value;

return Associate::of()($callbackForKeys)($callbackForValues)($mit);
/** @psalm-var Closure(Iterator<TKey, T>): Generator<TKey, list<T>> $associate */
$associate = Associate::of()($callbackForKeys)($callbackForValues);

return $associate($mit);
};
}
}
6 changes: 3 additions & 3 deletions src/Operation/Unpair.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ public function __invoke(): Closure
* @psalm-return Generator<int, array{TKey, T}>
*/
static function (Iterator $iterator): Generator {
foreach ($iterator as $key => $value) {
yield $key;
for (; $iterator->valid(); $iterator->next()) {
yield $iterator->key();

yield $value;
yield $iterator->current();
}
};
}
Expand Down

0 comments on commit 47e0f5e

Please sign in to comment.