HH\Lib\Dict\from_keys

Meta Engineer?

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

Returns a new dict where each value is the result of calling the given function on the corresponding key

namespace HH\Lib\Dict;

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

Time complexity: O(n * f), where f is the complexity of $value_func Space complexity: O(n)

Parameters

Returns

  • dict<Tk, Tv>

Examples

$keys = vec[1, 2, 3, 4];
$dict = Dict\from_keys($keys, $x ==> $x + 1);
\print_r($dict);

$values = vec[1, 2, 3, 4];
$dict = Dict\from_keys($keys, $x ==> 5);
\print_r($dict);