HH\Map::map

Returns a Map after an operation has been applied to each value in the current Map

public function map<Tu>(
  (function(Tv): Tu) $callback,
): Map<Tk, Tu>;

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

The keys will remain unchanged from the current Map to the returned Map.

Guide

Parameters

  • (function(Tv): Tu) $callback

Returns

  • Map<Tk,Tu> - a Map containing key/value pairs after a user-specified operation is applied.

Examples

In this example the Map's values are mapped to the same type (strings):

$m = Map {
  'red' => '#ff0000',
  'green' => '#00ff00',
  'blue' => '#0000ff',
  'yellow' => '#ffff00',
};

$capitalized = $m->map(fun('strtoupper'));
\var_dump($capitalized);

$css_colors = $capitalized->map($hex_code ==> "color: {$hex_code};");
\var_dump($css_colors);

In this example the Map's values are mapped to a different type (ints):

$m = Map {
  'red' => '#ff0000',
  'green' => '#00ff00',
  'blue' => '#0000ff',
  'yellow' => '#ffff00',
};

$decimal_codes = $m->map(fun('hexdec'));
\var_dump($decimal_codes);