HH\Pair::map

Returns an ImmVector containing the values after an operation has been applied to each value in the current Pair.

Description

public function map(
  callable $callback,
): ImmVector<Tu>

Returns an ImmVector containing the values after an operation has been applied to each value in the current Pair.

Every value in the current Pair is affected by a call to map(), unlike filter() where only values that meet a certain criteria are affected.

Parameters

  • callable $callback - The callback containing the operation to apply to the current Pair values.

Return Values

Examples

In this example the Pair's values are mapped to 0 if they're NULL:

<?hh

namespace Hack\UserDocumentation\API\Examples\Pair\Map\Strings;

$p = Pair {null, -1.5};

$immutable_v = $p->map($value ==> {
  if ($value === null) {
    return 0;
  }
  return $value;
});
var_dump($immutable_v);
Output
object(HH\ImmVector)#3 (2) {
  [0]=>
  int(0)
  [1]=>
  float(-1.5)
}