HH\Set::retain

Alters the current Set so that it only contains the values that meet a supplied condition on each value.

Description

public function retain(
  (function(Tv):bool) $callback,
): Set<Tv>

Alters the current Set so that it only contains the values that meet a supplied condition on each value.

This method is like filter(), but mutates the current Set too in addition to returning a shallow copy of the current Set.

Future changes made to the current Set ARE reflected in the returned Set, and vice-versa.

Parameters

  • (function(Tv):bool) $callback - The callback containing the condition to apply to the current Set values.

Return Values

  • Set<Tv> - a shallow copy of the current Set containing the values after a user-specified condition is applied.

Examples

<?hh

namespace Hack\UserDocumentation\API\Examples\Set\Retain;

$s = Set {'red', 'green', 'blue', 'yellow'};

// Only keep values beginning with 'r' or 'b'
$s->retain($color ==> $color[0] === 'r' || $color[0] === 'b');
var_dump($s);
Output
object(HH\Set)#1 (2) {
  string(3) "red"
  string(4) "blue"
}