HH\Lib\Vec\partition

Meta Engineer?

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

Returns a 2-tuple containing vecs for which the given predicate returned true and false, respectively

namespace HH\Lib\Vec;

function partition<Tv>(
  Traversable<Tv> $traversable,
  (function(Tv): bool) $predicate,
): (vec<Tv>, vec<Tv>);

Time complexity: O(n * p), where p is the complexity of $predicate Space complexity: O(n)

Parameters

Returns

  • (vec<Tv>, vec<Tv>)

Examples

$example_vec = vec[1,2,3,4,5];
$result = Vec\partition($example_vec, $val ==> $val%2 == 0);
print_r($result);
// result: [[2,4], [1,3,5]]

$result = Vec\partition($example_vec, $val ==> $val == $val);
print_r($result);
//result: [[1,2,3,4,5], []]

$result = Vec\partition($example_vec, $val ==> $val != 0);
print_r($result);
//result: [[1,2,3,4,5], []]