HH\Vector::zip

Returns a Vector where each element is a Pair that combines the element of the current Vector and the provided Traversable.

Description

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

Returns a Vector where each element is a Pair that combines the element of the current Vector and the provided Traversable.

If the number of elements of the Vector 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.

Parameters

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

Return Values

Examples

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

<?hh

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

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

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

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

foreach ($labeled_colors as list($color, $label)) {
  echo $label.': '.$color."\n";
}
Output
int(3)
My Favorite: red
My 2nd Favorite: green
My 3rd Favorite: blue