HH\Set::takeWhile

Returns a Set containing the values of the current Set up to but not including the first value that produces false when passed to the specified callback.

Description

public function takeWhile(
  (function(Tv):bool) $fn,
): Set<Tv>

Returns a Set containing the values of the current Set up to but not including the first value that produces false when passed to the specified callback.

The returned Set will always be a proper subset of the current Set.

Parameters

  • (function(Tv):bool) $fn - The callback that is used to determine the stopping condition.

Return Values

  • Set<Tv> - A Set that is a proper subset of the current Set up until the callback returns false.

Examples

This example shows how takeWhile can be used to create a new Set by taking elements from the beginning of an existing Set:

<?hh

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

$s = Set {0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144};

// Include values until we reach one over 10
$s2 = $s->takeWhile($x ==> $x <= 10);
var_dump($s2);
Output
object(HH\Set)#3 (6) {
  int(0)
  int(1)
  int(2)
  int(3)
  int(5)
  int(8)
}