PHP - Ds Map::reduce() Function



The PHP Ds\Map::reduce() function is used to reduce a map to a single value by applying a callback function to each element of the current map.

The return value of the callback becomes the new carry-over value for the next element, and after all elements have been processed, the reduce() function returns the final carry-over value.

Syntax

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

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

Parameters

Following are the parameters of this function −

  • callback − This parameter holds a function that operates on the set element.
  • initial − The initial value of the carry value, and it can be null.

Following is the syntax of the callback function −

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

Return value

This function returns the value of the final callback function.

Example 1

The following is the basic example of the PHP Ds\Map::reduce() function −

<?php  
   $map = new \Ds\Map([1, 2, 3, 4, 5]);  
   echo("The map elements: \n");  
   print_r($map);  
   echo("\nAn element after performing operation: \n");  
   var_dump($map->reduce(function($carry, $key, $element) {  
      return $carry + $element + 2; 
   }));
?>

Output

The above program produces the following output −

The map elements:
Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => 0
            [value] => 1
        )

    [1] => Ds\Pair Object
        (
            [key] => 1
            [value] => 2
        )

    [2] => Ds\Pair Object
        (
            [key] => 2
            [value] => 3
        )

    [3] => Ds\Pair Object
        (
            [key] => 3
            [value] => 4
        )

    [4] => Ds\Pair Object
        (
            [key] => 4
            [value] => 5
        )

)

An element after performing operation:
int(25)

Example 2

Following is another example of the PHP Ds\Map::reduce() function. We use this function to reduce this map ([10, 20, 30, 40, 50]) to a single value using the callback function.

<?php  
   $map = new \Ds\Map([10, 20, 30, 40, 50]);  
   echo("The original map elements: \n");  
   print_r($map);  
   $func = function($carry, $key, $element) {  
      return $carry * $element;  
   };  
   echo("\nThe map after reducing to single element: \n");  
   var_dump($map->reduce($func, 10));
?>

Output

After executing the above program, it displays the following output −

The original map elements:
Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => 0
            [value] => 10
        )

    [1] => Ds\Pair Object
        (
            [key] => 1
            [value] => 20
        )

    [2] => Ds\Pair Object
        (
            [key] => 2
            [value] => 30
        )

    [3] => Ds\Pair Object
        (
            [key] => 3
            [value] => 40
        )

    [4] => Ds\Pair Object
        (
            [key] => 4
            [value] => 50
        )

)

The map after reducing to single element:
int(120000000)
php_function_reference.htm
Advertisements