HH\Lib\Keyset\chunk

Meta Engineer?

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

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

namespace HH\Lib\Keyset;

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

If the given Traversable doesn't divide evenly, the final chunk will be smaller than the specified size. If there are duplicate values in the Traversable, some chunks may be smaller than the specified size.

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

Parameters

Returns

  • vec<keyset<Tv>>

Examples

$result = Keyset\chunk(vec[1,2,3,4,5,6],3);
print_r($result);
//result: [keyset[1,2,3], keyset[4,5,6]]

$result = Keyset\chunk(vec[1,2,2,3,4,5],3);
print_r($result);
//result: [keyset[1,2], keyset[3,4,5]]