-
Notifications
You must be signed in to change notification settings - Fork 4
Feat: iterable_chunk() #50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BenTools\IterableFunctions; | ||
|
||
use Iterator; | ||
use IteratorIterator; | ||
use Traversable; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @template TKey | ||
* @template TValue | ||
* | ||
* @implements Iterator<int, array<TKey, TValue>> | ||
*/ | ||
final class ChunkIterator implements Iterator | ||
{ | ||
/** @var Iterator<TKey, TValue> */ | ||
private Iterator $iterator; | ||
|
||
private int $chunkSize; | ||
|
||
private bool $preserveKeys; | ||
|
||
private int $chunkIndex = 0; | ||
|
||
/** @var array<TKey, TValue> */ | ||
private array $buffer = []; | ||
|
||
/** | ||
* @param Traversable<TKey, TValue> $iterator | ||
*/ | ||
public function __construct( | ||
Traversable $iterator, | ||
int $chunkSize, | ||
bool $preserveKeys = false, | ||
) { | ||
$this->iterator = $iterator instanceof Iterator ? $iterator : new IteratorIterator($iterator); | ||
$this->chunkSize = $chunkSize; | ||
$this->preserveKeys = $preserveKeys; | ||
} | ||
|
||
public function current(): mixed | ||
{ | ||
return $this->buffer; | ||
} | ||
|
||
public function next(): void | ||
{ | ||
$this->fill(); | ||
$this->chunkIndex++; | ||
} | ||
|
||
public function key(): int | ||
{ | ||
return $this->chunkIndex; | ||
} | ||
|
||
public function valid(): bool | ||
{ | ||
if ($this->chunkIndex === 0) { | ||
$this->fill(); | ||
} | ||
|
||
return $this->buffer !== []; | ||
} | ||
|
||
public function rewind(): void | ||
{ | ||
$this->iterator->rewind(); | ||
$this->chunkIndex = 0; | ||
$this->buffer = []; | ||
} | ||
|
||
private function fill(): void | ||
{ | ||
$this->buffer = []; | ||
$i = 0; | ||
while ($this->iterator->valid() && $i++ < $this->chunkSize) { | ||
$current = $this->iterator->current(); | ||
|
||
if ($this->preserveKeys) { | ||
$this->buffer[$this->iterator->key()] = $current; | ||
} else { | ||
$this->buffer[] = $current; | ||
} | ||
|
||
$this->iterator->next(); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BenTools\IterableFunctions\Tests; | ||
|
||
use function BenTools\IterableFunctions\iterable_chunk; | ||
use function expect; | ||
use function it; | ||
|
||
it('chunks an iterable', function (iterable $fruits): void { | ||
$chunks = iterable_chunk($fruits, 2); | ||
$expectedChunks = [ | ||
['banana', 'apple'], | ||
['strawberry', 'raspberry'], | ||
['pineapple'], | ||
]; | ||
expect([...$chunks])->toEqual($expectedChunks); | ||
})->with(function () { | ||
$fruits = [ | ||
'banana', | ||
'apple', | ||
'strawberry', | ||
'raspberry', | ||
'pineapple', | ||
]; | ||
yield 'array' => [$fruits]; | ||
yield 'traversable' => [(fn () => yield from $fruits)()]; | ||
}); | ||
|
||
it('preserves keys', function (iterable $fruits): void { | ||
$chunks = iterable_chunk($fruits, 2, true); | ||
$expectedChunks = [ | ||
[ | ||
'banana' => 0, | ||
'apple' => 1, | ||
], | ||
[ | ||
'strawberry' => 2, | ||
'raspberry' => 3, | ||
], | ||
['pineapple' => 4], | ||
]; | ||
expect([...$chunks])->toEqual($expectedChunks); | ||
})->with(function () { | ||
$fruits = [ | ||
'banana' => 0, | ||
'apple' => 1, | ||
'strawberry' => 2, | ||
'raspberry' => 3, | ||
'pineapple' => 4, | ||
]; | ||
yield 'array' => [$fruits]; | ||
yield 'traversable' => [(fn () => yield from $fruits)()]; | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/** @return iterable<int, iterable<TKey, TValue>> */
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shit - didn't notice on time static analysis was failing, will open another PR