PHP - Funzione Ds Deque reduce ()

La funzione Ds \ Deque :: reduce () può ridurre la deque a un singolo valore utilizzando una funzione di callback.

Sintassi

public mixed Ds\Deque::reduce( callable $callback [, mixed $initial ] )

La funzione Ds \ Deque :: reduce () può restituire il valore del callback finale.

Esempio 1

<?php 
   $deque = new \Ds\Deque([10, 20, 30, 40, 50]); 
   echo("The elements in deque: \n"); 
   print_r($deque); 
   
   $func = function($carry, $element) { 
      return $carry + $element; 
   }; 
   echo("\n The deque after reduced into single element:");   
   var_dump($deque->reduce($func, 3)); 
?>

Esempio 2

<?php 
   $deque = new \Ds\Deque([10, 20, 30, 40, 50]); 
   echo("The elements in deque: \n"); 
   print_r($deque); 
   
   $func = function($carry, $element) { 
      return $carry * $element + 30; 
   }; 
   echo("\n The deque after reduced into single element:"); 
   var_dump($deque->reduce($func, 10)); 
?>