HH\Lib\Keyset\partition

Meta Engineer?

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

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

namespace HH\Lib\Keyset;

function partition<Tv as arraykey>(
  Traversable<Tv> $traversable,
  (function(Tv): bool) $predicate,
): (keyset<Tv>, keyset<Tv>);

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

Parameters

Returns

  • (keyset<Tv>, keyset<Tv>)

Examples

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

$result = Keyset\partition(keyset[1,2,3,4], $val ==> $val==0);
print_r($result);
//result: vec[keyset[], keyset[1,2,3,4]]