-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathReduce.php
342 lines (304 loc) · 9.5 KB
/
Reduce.php
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
<?php
declare(strict_types=1);
namespace IterTools;
use IterTools\Util\NoValueMonad;
class Reduce
{
/**
* Reduces given collection like array_reduce() function.
*
* But unlike array_reduce(), it works with all iterable types.
*
* @template T
* @param iterable<mixed> $data
* @param callable $reducer
* @param T $initialValue
*
* @return T
*/
public static function toValue(iterable $data, callable $reducer, $initialValue = null)
{
$carry = $initialValue;
foreach ($data as $datum) {
$carry = $reducer($carry, $datum);
}
return $carry;
}
/**
* Reduces given iterable to its min value.
*
* Optional callable param $compareBy must return comparable value.
* If $compareBy is not provided then items of given collection must be comparable.
*
* Returns null if given collection is empty.
*
* @param iterable<mixed> $data
* @param callable|null $compareBy (optional) function to extract comparable value from element. Ex: $item->getSomeValue()
*
* @return mixed|null
*/
public static function toMin(iterable $data, callable $compareBy = null)
{
if ($compareBy !== null) {
return static::toValue(
$data,
fn ($carry, $datum) => $compareBy($datum) < $compareBy($carry ?? $datum)
? $datum
: $carry ?? $datum
);
}
return static::toValue($data, fn ($carry, $datum) => \min($carry ?? $datum, $datum));
}
/**
* Reduces given iterable to its max value.
*
* Optional callable param $compareBy must return comparable value.
* If $compareBy is not provided then items of given collection must be comparable.
*
* Returns null if given collection is empty.
*
* @param iterable<mixed> $data
* @param callable|null $compareBy (optional) function to extract comparable value from element. Ex: $item->getSomeValue()
*
* @return mixed|null
*/
public static function toMax(iterable $data, callable $compareBy = null)
{
if ($compareBy !== null) {
return static::toValue(
$data,
fn ($carry, $datum) => $compareBy($datum) > $compareBy($carry ?? $datum)
? $datum
: $carry ?? $datum
);
}
return static::toValue($data, fn ($carry, $datum) => \max($carry ?? $datum, $datum));
}
/**
* Reduces given collection to array of its upper and lower bounds.
*
* Callable param $compareBy must return comparable value.
*
* If $compareBy is not proposed then items of given collection must be comparable.
*
* Returns [null, null] if given collection is empty.
*
* @param iterable<numeric> $numbers
* @param callable|null $compareBy
*
* @return array{numeric, numeric}|array{null, null}
*/
public static function toMinMax(iterable $numbers, callable $compareBy = null): array
{
if ($compareBy !== null) {
return static::toValue($numbers, static function (array $carry, $datum) use ($compareBy) {
return [
$compareBy($datum) <= $compareBy($carry[0] ?? $datum)
? $datum
: $carry[0] ?? $datum,
$compareBy($datum) >= $compareBy($carry[1] ?? $datum)
? $datum
: $carry[1] ?? $datum,
];
}, [null, null]);
}
return static::toValue(
$numbers,
fn ($carry, $datum) => [
\min($carry[0] ?? $datum, $datum),
\max($carry[1] ?? $datum, $datum)
],
[null, null]
);
}
/**
* Reduces given iterable to its length.
*
* @param iterable<mixed> $data
*
* @return int
*/
public static function toCount(iterable $data): int
{
if (\is_countable($data)) {
return \count($data);
}
return static::toValue($data, fn ($carry) => $carry + 1, 0);
}
/**
* Reduces given collection to the sum of its items.
*
* @param iterable<numeric> $data
*
* @return int|float
*/
public static function toSum(iterable $data)
{
return static::toValue($data, fn ($carry, $datum) => $carry + $datum, 0);
}
/**
* Reduces given collection to the product of its items.
*
* Returns null if given collection is empty.
*
* @param iterable<numeric> $data
*
* @return int|float|null
*/
public static function toProduct(iterable $data)
{
return static::toValue($data, fn ($carry, $datum) => ($carry ?? 1) * $datum);
}
/**
* Reduces given collection to the mean average of its items.
*
* Returns null if given collection is empty.
*
* @param iterable<numeric> $data
*
* @return int|float|null
*/
public static function toAverage(iterable $data)
{
[$count, $sum] = static::toValue($data, static function ($carry, $datum): array {
[$count, $sum] = $carry;
return [$count + 1, $sum + $datum];
}, [0, 0]);
return $count ? ($sum / $count) : null;
}
/**
* Reduces to a string with optional glue, prefix, and suffix.
*
* Returns empty string (with optional prefix and suffix) if collection is empty.
*
* @param iterable<mixed> $data
* @param string $separator (optional) inserted between each item. Ex: ', ' for 1, 2, 3, ...
* @param string $prefix (optional) prepended to string
* @param string $suffix (optional) appended to string
*
* @return string
*/
public static function toString(iterable $data, string $separator = '', string $prefix = '', string $suffix = ''): string
{
$items = [];
foreach ($data as $datum) {
$items[] = $datum;
}
$joined = \implode($separator, $items);
return $prefix . $joined . $suffix;
}
/**
* Reduces given collection to its range.
*
* Returns 0 if given collection is empty.
*
* @param iterable<numeric> $numbers
*
* @return int|float
*/
public static function toRange(iterable $numbers)
{
[$min, $max] = static::toMinMax($numbers);
return ($max ?? 0) - ($min ?? 0);
}
/**
* Reduces given collection to its first value.
*
* @param iterable<mixed> $data
* @return mixed
*
* @throws \LengthException if collection is empty
*/
public static function toFirst(iterable $data)
{
foreach ($data as $datum) {
return $datum;
}
throw new \LengthException('collection is empty');
}
/**
* Reduces given collection to its last value.
*
* @param iterable<mixed> $data
* @return mixed
*
* @throws \LengthException if collection is empty
*/
public static function toLast(iterable $data)
{
/** @var mixed|NoValueMonad $result */
$result = static::toValue($data, fn ($carry, $datum) => $datum, NoValueMonad::getInstance());
if ($result instanceof NoValueMonad) {
throw new \LengthException('collection is empty');
}
return $result;
}
/**
* Reduces given collection to its first and last values.
*
* @param iterable<mixed> $data
* @return array{mixed, mixed}
*
* @throws \LengthException if collection is empty
*/
public static function toFirstAndLast(iterable $data): array
{
return [static::toFirst($data), static::toLast($data)];
}
/**
* Reduces given collection random value in from within it.
*
* @param iterable<mixed> $data
*
* @return mixed
*
* @throws \LengthException if given iterable is empty
*/
public static function toRandomValue(iterable $data)
{
if (\is_countable($data)) {
if (\count($data) === 0) {
throw new \LengthException('Given iterable must be non-empty');
}
$targetIndex = \mt_rand(0, \count($data) - 1);
$index = 0;
foreach ($data as $datum) {
if ($targetIndex === $index) {
return $datum;
}
++$index;
}
}
$data = Transform::toArray($data);
if (\count($data) === 0) {
throw new \LengthException('Given iterable must be non-empty');
}
return $data[\array_rand($data)];
}
/**
* Reduces given iterable to the value at the nth position.
*
* @template T
*
* @param iterable<T> $data
* @param int $position
*
* @return T
*
* @throws \LengthException if given iterable does not contain item with target position.
*/
public static function toNth(iterable $data, int $position)
{
if (\is_countable($data) && \count($data) <= $position) {
throw new \LengthException("Given iterable does not contain item with position {$position}");
}
$i = 0;
foreach ($data as $datum) {
if ($i === $position) {
return $datum;
}
++$i;
}
throw new \LengthException("Given iterable does not contain item with position {$position}");
}
}