HH\Lib\C\reduce_with_key

Meta Engineer?

This is available as C\reduce_with_key in the www repository.

Reduces the given KeyedTraversable into a single value by applying an accumulator function against an intermediate result and each key/value

namespace HH\Lib\C;

function reduce_with_key<Tk, Tv, Ta>(
  KeyedTraversable<Tk, Tv> $traversable,
  (function(Ta, Tk, Tv): Ta) $accumulator,
  Ta $initial,
): Ta;

Time complexity: O(n) Space complexity: O(1)

Parameters

Returns

  • Ta

Examples

$dict = dict["a" => 1, "b" => 2, "c" => 3];
$list = vec["a","b"];
$reduce_with_key_result = C\reduce_with_key($dict, ($a, $k, $v) ==> $a + (C\contains($list, $k) ? 0 : $v), 0);
echo "Reduce with key result: $reduce_with_key_result\n";
//Output: Reduce with key result: 3