HH\Lib\Vec\chunk

Meta Engineer?

This is available as Vec\chunk in the www repository.

Returns a vec containing the original vec split into chunks of the given size

namespace HH\Lib\Vec;

function chunk<Tv>(
  Traversable<Tv> $traversable,
  int $size,
): vec<vec<Tv>>;

If the original vec doesn't divide evenly, the final chunk will be smaller.

Time complexity: O(n) Space complexity: O(n)

Parameters

Returns

  • vec<vec<Tv>>

Examples

$example_vec = vec[1,2,3,4,5];
$chunks = Vec\chunk($example_vec, 3);
print_r($chunks);
// result [[1,2,3], [4,5]]
$chunks = Vec\chunk($example_vec, 5);
print_r($chunks);
// result [1,2,3,4,5]