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 22, 2020
1 parent f826fa4 commit c59cea9
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Operation/Apply.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __invoke(): Closure
* @psalm-return Generator<TKey, T>
*/
static function (Iterator $iterator) use ($callbacks): Generator {
for (; true === $iterator->valid(); $iterator->next()) {
for (; $iterator->valid(); $iterator->next()) {
$key = $iterator->key();
$current = $iterator->current();

Expand Down
4 changes: 2 additions & 2 deletions src/Operation/Normalize.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public function __invoke(): Closure
* @psalm-return Generator<int, T, mixed, void>
*/
static function (Iterator $iterator): Generator {
foreach ($iterator as $value) {
yield $value;
for (; $iterator->valid(); $iterator->next()) {
yield $iterator->current();
}
};
}
Expand Down
5 changes: 4 additions & 1 deletion src/Operation/Since.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 (; $iterator->valid(); $iterator->next()) {
$key = $iterator->key();
$current = $iterator->current();

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

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

Expand Down
5 changes: 4 additions & 1 deletion src/Operation/Until.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 (; $iterator->valid(); $iterator->next()) {
$key = $iterator->key();
$current = $iterator->current();

yield $key => $current;

$result = array_reduce(
Expand Down

0 comments on commit c59cea9

Please sign in to comment.