HH\Vector::filter

Returns a Vector containing the values of the current Vector that meet a supplied condition

public function filter(
  (function(Tv): bool) $callback,
): Vector<Tv>;

filter()'s result contains only values that meet the provided criterion; unlike map(), where a value is included for each value in the original Vector.

Guide

Parameters

  • (function(Tv): bool) $callback

Returns

  • Vector<Tv> - A Vector containing the values after a user-specified condition is applied.

Examples

$colors = Vector {'red', 'green', 'blue', 'yellow'};
$primary_colors = Set {'red', 'green', 'blue'};

// Create a Vector of colors that contain the letter 'l'
$l_colors = $colors->filter($color ==> \strpos($color, 'l') !== false);
\var_dump($l_colors);

// Create a Vector of colors that aren't listed in $primary_colors
$non_primary_colors = $colors->filter(
  $color ==> !$primary_colors->contains($color),
);
\var_dump($non_primary_colors);