HH\Map::zip

Returns a Map where each value is a Pair that combines the value of the current Map and the provided Traversable.

Description

public function zip(
  Traversable<Tu> $traversable,
): Map<Tk,Pair<Tv,Tu>>

Returns a Map where each value is a Pair that combines the value of the current Map and the provided Traversable.

If the number of values of the current Map are not equal to the number of elements in the Traversable, then only the combined elements up to and including the final element of the one with the least number of elements is included.

The keys associated with the current Map remain unchanged in the returned Map.

Parameters

  • Traversable<Tu> $traversable - The Traversable to use to combine with the elements of the current Map.

Return Values

Examples

This example shows how zip combines the values of the Map and another Traversable. The resulting Map $labeled_colors has three elements because $labels doesn't have a fourth element to pair with $m.

<?hh

namespace Hack\UserDocumentation\API\Examples\Map\Zip;

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

$labels = Vector {'My Favorite', 'My 2nd Favorite', 'My 3rd Favorite'};
$labeled_colors = $m->zip($labels);

var_dump($labeled_colors->count()); // 3

foreach ($labeled_colors as $color => $pair) {
  $hex_code = $pair[0];
  $label = $pair[1];
  echo "{$label}: {$color} ($hex_code)\n";
}
Output
int(3)
My Favorite: red (#ff0000)
My 2nd Favorite: green (#00ff00)
My 3rd Favorite: blue (#0000ff)