HH\Set::map
Returns a Set
containing the values after an operation has been applied
to each value in the current Set
public function map<Tu as arraykey>(
(function(Tv): Tu) $callback,
): Set<Tu>;
Every value in the current Set
is affected by a call to map()
, unlike
filter()
where only values that meet a certain criteria are affected.
Guide
Parameters
(function(Tv): Tu) $callback
Returns
Examples
In this example the Set
's elements are mapped to the same type (string
s):
$s = Set {'red', 'green', 'blue', 'yellow'};
$capitalized = $s->map(fun('strtoupper'));
\var_dump($capitalized);
$shortened = $s->map($color ==> \substr($color, 0, 3));
\var_dump($shortened);
In this example the Set
's elements are mapped to a different type (int
s):
$s = Set {'red', 'green', 'blue', 'yellow'};
$lengths = $s->map(fun('strlen'));
\var_dump($lengths);