Skip to content

Instantly share code, notes, and snippets.

@slava-vishnyakov
Last active September 5, 2017 13:38
Show Gist options
  • Save slava-vishnyakov/4d35f47b87914cf5f09281cb69fc6ec2 to your computer and use it in GitHub Desktop.
Save slava-vishnyakov/4d35f47b87914cf5f09281cb69fc6ec2 to your computer and use it in GitHub Desktop.
Laravel Slice Consecutive (like Array#each_cons in Ruby)
\Illuminate\Support\Collection::macro('sliceConsecutive', function ($size)
{
if ($size <= 0) {
return new static;
}
if ($size >= $this->count()) {
return new static([$this]);
}
$chunks = [];
for ($i = 0; $i <= $this->count() - $size; $i++) {
$chunks [] = $this->slice($i, $size)->values();
}
return new static($chunks);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment