-
-
Notifications
You must be signed in to change notification settings - Fork 207
Expand file tree
/
Copy pathDifference.php
More file actions
40 lines (34 loc) · 907 Bytes
/
Copy pathDifference.php
File metadata and controls
40 lines (34 loc) · 907 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
/**
* @package Functional-php
* @author Lars Strojny <lstrojny@php.net>
* @copyright 2011-2021 Lars Strojny
* @license https://opensource.org/licenses/MIT MIT
* @link https://github.com/lstrojny/functional-php
*/
namespace Functional;
use Functional\Exceptions\InvalidArgumentException;
/**
* Takes a collection and returns the difference of all elements
*
* @template V
* @template I of int|float
*
* @param iterable<V> $collection
* @param I $initial
*
* @return (V is int ? ($initial is int ? int : float) : (V is float ? float : int|float))
*
* @no-named-arguments
*/
function difference($collection, $initial = 0)
{
InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1);
$result = $initial;
foreach ($collection as $value) {
if (\is_numeric($value)) {
$result -= $value;
}
}
return $result;
}