Skip to content

Commit

Permalink
refactor: stop using paththru in take method
Browse files Browse the repository at this point in the history
  • Loading branch information
fuwasegu committed Sep 13, 2023
1 parent a02a39b commit f9fea87
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/Illuminate/Collections/LazyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1421,7 +1421,24 @@ public function sortKeysUsing(callable $callback)
public function take($limit)
{
if ($limit < 0) {
return $this->passthru('take', func_get_args());
return new static(function () use ($limit) {
$queue = [];
$positiveLimit = abs($limit);

$iterator = $this->getIterator();
foreach ($iterator as $key => $value) {
$queue[] = [$key, $value];

// If the length of tempQueue exceeds abs($limit), remove the first element.
if (count($queue) > $positiveLimit) {
array_shift($queue);
}
}

foreach ($queue as $item) {
yield $item[0] => $item[1];
}
});
}

return new static(function () use ($limit) {
Expand Down

0 comments on commit f9fea87

Please sign in to comment.