HH\Lib\Keyset\filter_with_key
Returns a new keyset containing only the values for which the given predicate
returns true
namespace HH\Lib\Keyset;
function filter_with_key<Tk, Tv as arraykey>(
KeyedTraversable<Tk, Tv> $traversable,
(function(Tk, Tv): bool) $predicate,
): keyset<Tv>;
If you don't need access to the key, see Keyset\filter()
.
Time complexity: O(n * p), where p is the complexity of $predicate
Space complexity: O(n)
Parameters
KeyedTraversable<Tk,
Tv> $traversable
(function(Tk, Tv): bool) $predicate
Returns
keyset<Tv>
Examples
$result = Keyset\filter_with_key(dict[1 => 2, 2 => 4, 3 => 5], ($key, $val) ==> $val%2==0);
print_r($result);
//result: keyset[2,4]
$result = Keyset\filter_with_key(dict[1 => 2, 2 => 4], ($key, $val) ==> $val==0);
print_r($result);
//result: keyset[]
$result = Keyset\filter_with_key(dict[1 => 2, 2 => 4, 3 => 5], ($key, $val) ==> $val == $val);
print_r($result);
//result: keyset[2,4,5]