Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PHP - Ds Deque::reduce() Function



The PHP Ds\Deque::reduce() function reduces the deque to a single value. To perform this operation, the function uses a callback function. The callback function contains two parameters, which are carry and value.

The carry is the previous callback return value, and the value is the current iteration value. Based on these two parameters, the callback function is applied.

The final result of this callback function is considered as one of the parameters of the reduce() function. There is another parameter called 'initial', which represents the initial value of the carrying value.

Syntax

Following is the syntax of the PHP Ds\Deque::reduce() function −

public Ds\Deque::reduce(callable $callback, mixed $initial = ?): mixed

Parameters

Following are the parameters of this function −

  • callback − A callback function reduces a single deque value.
  • initial − It represents the initial value of the carry value. This value can possibly be null.

Here is the syntax for the callback function:

callback(mixed $carry, mixed $value): mixed

Return value

This function returns the final callback return value.

Example 1

The following example demonstrates the usage of the PHP Ds\Deque::reduce() function −

<?php
   $deque = new \Ds\Deque([5, 10, 15, 20, 25]);
   echo "The deque elements are: \n";
   print_r($deque);
   $func = function($carry, $element) {
      return $carry + $element*2;
   };
   echo "The reduced single value: ";
   print_r($deque->reduce($func));
?>

Output

On executing the above program, it will generate the following output −

The deque elements are:
Ds\Deque Object
(
    [0] => 5
    [1] => 10
    [2] => 15
    [3] => 20
    [4] => 25
)
The reduced single value: 150

Example 2

Following is another example of the PHP Ds\Deque::reduce() function. We use this function to reduce this deque ([10, 20, 30]) to a single value using a provided callback function −

<?php
   $deque = new \Ds\Deque([10, 20, 30]);
   echo "The deque elements are: \n";
   print_r($deque);
   $func = function($carry, $element) {
      return $carry * $element*2;
   };
   echo "The reduced single value: ";
   print_r($deque->reduce($func, 10));
?>

Output

After executing the above program, the following output will be displayed −

The deque elements are:
Ds\Deque Object
(
    [0] => 10
    [1] => 20
    [2] => 30
)
The reduced single value: 480000

Example 3

If the current deque is empty ([]), and an "initial" value is provided, this function will return the initial value as a result.

In the example below, we use the PHP Ds\Deque::reduce() function to reduce the current deque to a single using a callback function as follows:

<?php
   $deque = new \Ds\Deque([]);
   echo "The deque elements are: \n";
   print_r($deque);
   $func = function($carry, $element) {
      return $element - $carry;
   };
   echo "The reduced single is: ";
   print_r($deque->reduce($func,15));
?>

Output

The above program produces the following output −

The deque elements are:
Ds\Deque Object
(
)
The reduced single is: 15
Advertisements