HH\Set::fromItems

Creates a Set from the given Traversable, or an empty Set if null is passed.

Description

public static function fromItems(
  ?Traversable<Tv2> $items,
): Set<Tv2>

Creates a Set from the given Traversable, or an empty Set if null is passed.

This is the static method version of the Set::__construct() constructor.

Parameters

  • ?Traversable<Tv2> $items - any Traversable object from which to create a Set (e.g., array). If null, then an empty Set is created.

Return Values

Examples

<?hh

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

// Create a new Set from an array
$s = Set::fromItems(array('red', 'green', 'red', 'blue', 'blue', 'yellow'));
var_dump($s);

// Create a new Set from a Vector
$s = Set::fromItems(Vector {'red', 'green', 'red', 'blue', 'blue', 'yellow'});
var_dump($s);

// Create a new Set from the values of a Map
$s = Set::fromItems(Map {
  'red1' => 'red',
  'green' => 'green',
  'red2' => 'red',
  'blue1' => 'blue',
  'blue2' => 'blue',
  'yellow' => 'yellow',
});
var_dump($s);
Output
object(HH\Set)#1 (4) {
  string(3) "red"
  string(5) "green"
  string(4) "blue"
  string(6) "yellow"
}
object(HH\Set)#3 (4) {
  string(3) "red"
  string(5) "green"
  string(4) "blue"
  string(6) "yellow"
}
object(HH\Set)#5 (4) {
  string(3) "red"
  string(5) "green"
  string(4) "blue"
  string(6) "yellow"
}