HH\Set::zip

Throws an exception unless the current Set or the Traversable is empty.

Description

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

Throws an exception unless the current Set or the Traversable is empty.

Since Sets only support integers or strings as values, we cannot have a Pair as a Set value. So in order to avoid an InvalidArgumentException, either the current Set or the Traversable must be empty so that we actually return an empty Set.

Parameters

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

Return Values

Examples

This example shows that zip won't thrown an Exception if at least one of the current Set or the $traversable is empty:

<?hh

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

// The $traversable is empty so the result will be empty
$s = Set {'red', 'green', 'blue', 'yellow'};
$zipped = $s->zip(Vector {});
var_dump($zipped);

// The Set $s is empty so the result will be empty
$s = Set {};
$zipped = $s->zip(Vector {'My Favorite', 'My Second Favorite'});
var_dump($zipped);
Output
object(HH\Set)#3 (0) {
}
object(HH\Set)#6 (0) {
}

This example shows that zip will throw an Exception if the result is non-empty:

<?hh

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

$s = Set {'red', 'green', 'blue', 'yellow'};
$zipped = $s->zip(Vector {'My Favorite', 'My Second Favorite'});
var_dump($zipped);
Output
Fatal error: Uncaught exception 'InvalidArgumentException' with message 'Only integer values and string values may be used with Sets' in /user-documentation/api-examples/class.Set/zip/002-nonempty-exception.php:6
Stack trace:
#0 /user-documentation/api-examples/class.Set/zip/002-nonempty-exception.php(6): HH\Set->zip()
#1 {main}