1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bdf\Collection\Stream\Accumulator; |
4
|
|
|
|
5
|
|
|
use Bdf\Collection\Stream\StreamInterface; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Base accumulators for perform standard reduce operations on streams |
9
|
|
|
* |
10
|
|
|
* @see AccumulatorInterface |
11
|
|
|
* @see StreamInterface::reduce() |
12
|
|
|
*/ |
13
|
|
|
final class Accumulators |
14
|
|
|
{ |
15
|
|
|
/** @var AccumulatorInterface|null */ |
16
|
|
|
private static $sum; |
17
|
|
|
/** @var AccumulatorInterface|null */ |
18
|
|
|
private static $multiply; |
19
|
|
|
/** @var AccumulatorInterface|null */ |
20
|
|
|
private static $min; |
21
|
|
|
/** @var AccumulatorInterface|null */ |
22
|
|
|
private static $max; |
23
|
|
|
|
24
|
|
|
/** Deny instantiation */ |
25
|
|
|
private function __construct() { } |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Perform a sum on each elements of the stream |
29
|
|
|
* |
30
|
|
|
* <code> |
31
|
|
|
* $stream = new ArrayStream([1, 2, 3]); |
32
|
|
|
* $stream->reduce(Accumulators::sum()); // 6 |
33
|
|
|
* </code> |
34
|
|
|
* |
35
|
|
|
* @template V as numeric |
36
|
|
|
* |
37
|
|
|
* @return AccumulatorInterface<V, V> |
38
|
|
|
*/ |
39
|
19 |
|
public static function sum() |
40
|
|
|
{ |
41
|
19 |
|
if (self::$sum) { |
42
|
19 |
|
return self::$sum; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return self::$sum = new class implements AccumulatorInterface { |
46
|
|
|
public function __invoke($carry, $item) { return $carry + $item; } |
47
|
|
|
public function initial() { return 0; } |
|
|
|
|
48
|
|
|
}; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Perform a multiplication on each elements of the stream |
53
|
|
|
* |
54
|
|
|
* <code> |
55
|
|
|
* $stream = new ArrayStream([2, 5, 4]); |
56
|
|
|
* $stream->reduce(Accumulators::multiply()); // 80 |
57
|
|
|
* </code> |
58
|
|
|
* |
59
|
|
|
* @template V as numeric |
60
|
|
|
* |
61
|
|
|
* @return AccumulatorInterface<V, V> |
62
|
|
|
*/ |
63
|
2 |
|
public static function multiply() |
64
|
|
|
{ |
65
|
2 |
|
if (self::$multiply) { |
66
|
2 |
|
return self::$multiply; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return self::$multiply = new class implements AccumulatorInterface { |
70
|
|
|
public function __invoke($carry, $item) { return $carry * $item; } |
|
|
|
|
71
|
|
|
public function initial() { return 1; } |
|
|
|
|
72
|
|
|
}; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get the lowest element of the stream |
77
|
|
|
* |
78
|
|
|
* <code> |
79
|
|
|
* $stream = new ArrayStream([8, 5, 4]); |
80
|
|
|
* $stream->reduce(Accumulators::min()); // 4 |
81
|
|
|
* </code> |
82
|
|
|
* |
83
|
|
|
* @template V |
84
|
|
|
* |
85
|
|
|
* @return AccumulatorInterface<V, V> |
86
|
|
|
*/ |
87
|
2 |
|
public static function min() |
88
|
|
|
{ |
89
|
2 |
|
if (self::$min) { |
90
|
2 |
|
return self::$min; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return self::$min = new class implements AccumulatorInterface { |
94
|
|
|
public function __invoke($carry, $item) { return $item < $carry ? $item : $carry; } |
95
|
|
|
public function initial() { return PHP_INT_MAX; } |
|
|
|
|
96
|
|
|
}; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get the highest element of the stream |
101
|
|
|
* |
102
|
|
|
* <code> |
103
|
|
|
* $stream = new ArrayStream([4, 8, 2]); |
104
|
|
|
* $stream->reduce(Accumulators::max()); // 8 |
105
|
|
|
* </code> |
106
|
|
|
* |
107
|
|
|
* @template V as numeric |
108
|
|
|
* |
109
|
|
|
* @return AccumulatorInterface<V, V> |
110
|
|
|
*/ |
111
|
2 |
|
public static function max() |
112
|
|
|
{ |
113
|
2 |
|
if (self::$max) { |
114
|
2 |
|
return self::$max; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return self::$max = new class implements AccumulatorInterface { |
118
|
|
|
public function __invoke($carry, $item) { return $item > $carry ? $item : $carry; } |
119
|
|
|
public function initial() { return PHP_INT_MIN; } |
120
|
|
|
}; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: