Skip to main content

map

Note

This is a point-in-time snapshot of the API documentation from January 2026. Going forward, we will not be maintaining a public copy of these references, and recommend users to refer to the built-in signature helpers available in the Hack LSP instead for complete and up-to-date information.

Returns a Vector containing the results of applying an operation to each value in the current Vector

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

map()'s result contains a value for every value in the current Vector; unlike filter(), where only values that meet a certain criterion are included in the resulting Vector.

Guide

Parameters

  • (function(Tv): Tu) $callback

Returns

  • Vector<Tu> - A Vector containing the results of applying a user-specified operation to each value of the current Vector in turn.

Examples

In this example the Vector's elements are mapped to the same type (strings):

$v = Vector {'red', 'green', 'blue', 'yellow'};

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

$shortened = $v->map($color ==> \substr($color, 0, 3));
\var_dump($shortened);

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

$v = Vector {'red', 'green', 'blue', 'yellow'};

$lengths = $v->map(fun('strlen'));
\var_dump($lengths);