HH\Lib\Dict\associate

Meta Engineer?

This is available as Dict\associate in the www repository.

Returns a new dict where each element in $keys maps to the corresponding element in $values

namespace HH\Lib\Dict;

function associate<Tk as arraykey, Tv>(
  Traversable<Tk> $keys,
  Traversable<Tv> $values,
): dict<Tk, Tv>;

Time complexity: O(n) where n is the size of $keys (which must be the same as the size of $values) Space complexity: O(n) where n is the size of $keys (which must be the same as the size of $values)

Parameters

Returns

  • dict<Tk, Tv>

Examples

// NOTE: $keys, $values must be the same length
$keys = vec[1,2,3,4,5];
$values = vec[1,4,9,16,25];
$dict = Dict\associate($keys, $values);
\print_r($dict);
//Output: dict[1 => 1, 2 => 4, 3 => 9, 4 => 16, 5 => 25]