Skip to content

Latest commit

 

History

History
43 lines (30 loc) · 967 Bytes

reduce.md

File metadata and controls

43 lines (30 loc) · 967 Bytes

reduce

Description

Returns the accumulated value of iteratively reduce the collection using a function that receives the accumulated value and returns a new one in each iteration

Parameters

fn
Function which reduce the collection calculating the accumulated value.
coll
Collection of values to be reduced by the function.
initial
initial value that will be used as accumulated value for with the first item in the collection.

Examples

A total amount calculator of a shopping cart:

<?php

use function Lambdish\Phunctional\reduce;

return reduce(
    function ($acc, array $item) {
        return $acc + ($item['quantity'] * $item['price']);
    }, 
    [
        'apple'     => ['quantity' => 2, 'price' => 1.2],
        'orange'    => ['quantity' => 1, 'price' => 3],
        'pineapple' => ['quantity' => 5, 'price' => 1],
    ]
);
            
// => 10.4