Skip to content

Commit

Permalink
refactor: Update some operation to make them lazy by default.
Browse files Browse the repository at this point in the history
This doesn't change anything in the behavior of the library.
  • Loading branch information
drupol committed Sep 12, 2020
1 parent 1318e41 commit ff6c556
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ public function count(): int

public function current(int $index = 0)
{
return Current::of()($index)($this->getIterator());
return $this->run(Current::of()($index))->getIterator()->current();
}

public function cycle(): CollectionInterface
Expand Down Expand Up @@ -537,7 +537,7 @@ public function jsonSerialize(): array

public function key(int $index = 0)
{
return Key::of()($index)($this->getIterator());
return $this->run(Key::of()($index))->getIterator()->current();
}

public function keys(): CollectionInterface
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Current.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ static function (Iterator $iterator) use ($index) {
for ($i = 0; $i < $index; $i++, $iterator->next()) {
}

return $iterator->current();
return yield $iterator->current();
};
};
}
Expand Down
7 changes: 6 additions & 1 deletion src/Operation/FoldLeft.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ static function (Iterator $iterator) use ($callback, $initial): Generator {
/** @psalm-var Generator<TKey, T> $iterator */
$iterator = Last::of()(Reduction::of()($callback)($initial)($iterator));

return yield Key::of()(0)($iterator) => Current::of()(0)($iterator);
/** @psalm-var Generator<int, TKey> $key */
$key = (Key::of()(0)($iterator));
/** @psalm-var Generator<int, T> $current */
$current = (Current::of()(0)($iterator));

return yield $key->current() => $current->current();
};
};
};
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Key.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ static function (Iterator $iterator) use ($index) {
for ($i = 0; $i < $index; $i++, $iterator->next()) {
}

return $iterator->key();
return yield $iterator->key();
};
};
}
Expand Down

0 comments on commit ff6c556

Please sign in to comment.