Skip to content

Commit

Permalink
Make the Apply operation lazy.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Aug 28, 2019
1 parent 64f3808 commit 37ad7bd
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
3 changes: 2 additions & 1 deletion spec/drupol/collection/CollectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ public function it_can_apply(): void
};

$this
->apply($callback)
->shouldThrow(\Exception::class)
->during('apply', [$callback]);
->during('all', [$callback]);

$context = [];

Expand Down
4 changes: 2 additions & 2 deletions src/Contract/Applyable.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface Applyable
*
* @param callable ...$callables
*
* @return \drupol\collection\Contract\Collection
* @return \drupol\collection\Contract\BaseCollection
*/
public function apply(callable ...$callables): Collection;
public function apply(callable ...$callables): BaseCollection;
}
18 changes: 10 additions & 8 deletions src/Operation/Apply.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,20 @@ final class Apply extends Operation
/**
* {@inheritdoc}
*/
public function on(iterable $collection): iterable
public function on(iterable $collection): \Closure
{
[$callbacks] = $this->parameters;
$callbacks = $this->parameters;

foreach ($callbacks as $callback) {
foreach ($collection as $key => $item) {
if (false === $callback($item, $key)) {
break;
return static function () use ($callbacks, $collection): iterable {
foreach ($callbacks as $callback) {
foreach ($collection as $key => $item) {
if (false === $callback($item, $key)) {
break;
}
}
}
}

return $collection;
return $collection;
};
}
}

0 comments on commit 37ad7bd

Please sign in to comment.