A função Ds \ Vector :: reduce () pode reduzir o vetor a um único valor usando a função de retorno de chamada.
Sintaxe
public mixed Ds\Vector::reduce( callable $callback [, mixed $initial ] )
A função Ds \ Vector :: reduce () pode retornar o valor do retorno de chamada final.
Exemplo 1
<?php
$vector = new \Ds\Vector([1, 2, 3, 4, 5]);
echo("The original vector elements: \n");
print_r($vector);
echo("\n The element after performing operation: \n");
var_dump($vector->reduce(function($carry, $element) {
return $carry + $element + 2;
}));
?>
Exemplo 2
<?php
$vector = new \Ds\Vector([10, 20, 30, 40, 50]);
echo("The original vector elements: \n");
print_r($vector);
$func = function($carry, $element) {
return $carry * $element;
};
echo("\n The vector after reducing to single element: \n");
var_dump($vect->reduce($func, 10));
?>