/
Standard.php
697 lines (581 loc) · 19.9 KB
/
Standard.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
<?php
/**
* Copyright 2017, 2018 Alexey Kopytko <alexey@kopytko.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
declare(strict_types=1);
namespace Pipeline;
use function array_filter;
use function array_map;
use function array_reduce;
use function array_shift;
use function array_slice;
use function array_values;
use ArrayIterator;
use CallbackFilterIterator;
use function count;
use Countable;
use EmptyIterator;
use Generator;
use function is_array;
use function is_iterable;
use function is_string;
use Iterator;
use function iterator_to_array;
use IteratorAggregate;
use function mt_getrandmax;
use function mt_rand;
use Traversable;
/**
* Concrete pipeline with sensible default callbacks.
*/
class Standard implements IteratorAggregate, Countable
{
/**
* Pre-primed pipeline. This is not a full `iterable` per se because we exclude IteratorAggregate before assigning a value.
*
* @var ?iterable
*/
private $pipeline;
/**
* Contructor with an optional source of data.
*
* @param ?iterable $input
*/
public function __construct(iterable $input = null)
{
// IteratorAggregate is a nuance best we avoid dealing with.
// For example, CallbackFilterIterator needs a plain Iterator.
while ($input instanceof IteratorAggregate) {
$input = $input->getIterator();
}
$this->pipeline = $input;
}
/**
* An extra variant of `map` which unpacks arrays into arguments. Flattens inputs if no callback provided.
*
* @param ?callable $func
* @psalm-suppress InvalidArgument
*
* @return $this
*/
public function unpack(?callable $func = null): self
{
$func = $func ?? static function (...$args) {
yield from $args;
};
return $this->map(static function (iterable $args = []) use ($func) {
return $func(...$args);
});
}
/**
* Takes a callback that for each input value may return one or yield many. Also takes an initial generator, where it must not require any arguments.
*
* With no callback is a no-op (can safely take a null).
*
* @param ?callable $func a callback must either return a value or yield values (return a generator)
*
* @return $this
*/
public function map(?callable $func = null): self
{
if (null === $func) {
return $this;
}
// That's the standard case for any next stages.
if (is_iterable($this->pipeline)) {
/** @phan-suppress-next-line PhanTypeMismatchArgument */
$this->pipeline = self::apply($this->pipeline, $func);
return $this;
}
// Let's check what we got for a start.
$this->pipeline = $func();
// Generator is a generator, moving along
if ($this->pipeline instanceof Generator) {
// It is possible to detect if callback is a generator like so:
// (new \ReflectionFunction($func))->isGenerator();
// Yet this will restrict users from replacing the pipeline and has unknown performance impact.
// But, again, we could add a direct internal method to replace the pipeline, e.g. as done by unpack()
return $this;
}
// Not a generator means we were given a simple value to be treated as an array.
// We do not cast to an array here because casting a null to an array results in
// an empty array; that's surprising and not how it works for other values.
$this->pipeline = [
$this->pipeline,
];
return $this;
}
private static function apply(iterable $previous, callable $func): iterable
{
foreach ($previous as $key => $value) {
$result = $func($value);
// For generators we use keys they provide
if ($result instanceof Generator) {
yield from $result;
continue;
}
// In case of a plain old mapping function we use the original key
yield $key => $result;
}
}
/**
* Takes a callback that for each input value expected to return another single value. Unlike map(), it assumes no special treatment for generators.
*
* With no callback is a no-op (can safely take a null).
*
* @param ?callable $func a callback must return a value
*
* @return $this
*/
public function cast(callable $func = null): self
{
if (null === $func) {
return $this;
}
// We got an array, that's what we need. Moving along.
if (is_array($this->pipeline)) {
$this->pipeline = array_map($func, $this->pipeline);
return $this;
}
if (is_iterable($this->pipeline)) {
/** @phan-suppress-next-line PhanTypeMismatchArgument */
$this->pipeline = self::applyOnce($this->pipeline, $func);
return $this;
}
// Else get the seed value.
// We do not cast to an array here because casting a null to an array results in
// an empty array; that's surprising and not how it works for other values.
$this->pipeline = [
$func(),
];
return $this;
}
private static function applyOnce(iterable $previous, callable $func): iterable
{
foreach ($previous as $key => $value) {
yield $key => $func($value);
}
}
/**
* Removes elements unless a callback returns true.
*
* With no callback drops all null and false values (not unlike array_filter does by default).
*
* @param ?callable $func {@inheritdoc}
*
* @return $this
*/
public function filter(?callable $func = null): self
{
if (null === $this->pipeline) {
// No-op: null.
return $this;
}
if ([] === $this->pipeline) {
// No-op: an empty array.
return $this;
}
$func = self::resolvePredicate($func);
// We got an array, that's what we need. Moving along.
if (is_array($this->pipeline)) {
$this->pipeline = array_filter($this->pipeline, $func);
return $this;
}
/** @var Iterator $iterator */
$iterator = $this->pipeline;
/** @phan-suppress-next-line PhanTypeMismatchArgumentInternal */
$this->pipeline = new CallbackFilterIterator($iterator, $func);
return $this;
}
/**
* Resolves a nullable predicate into a sensible non-null callable.
*/
private static function resolvePredicate(?callable $func): callable
{
if (null === $func) {
return static function ($value) {
// Cast is unnecessary for non-stict filtering
return $value;
};
}
// Strings usually are internal functions, which typically require exactly one parameter.
if (is_string($func)) {
return static function ($value) use ($func) {
return $func($value);
};
}
return $func;
}
/**
* Reduces input values to a single value. Defaults to summation. This is a terminal operation.
*
* @param ?callable $func function (mixed $carry, mixed $item) { must return updated $carry }
* @param ?mixed $initial initial value for a $carry
*
* @return ?mixed
*/
public function reduce(?callable $func = null, $initial = null)
{
return $this->fold($initial ?? 0, $func);
}
/**
* Reduces input values to a single value. Defaults to summation. Requires an initial value. This is a terminal operation.
*
* @param mixed $initial initial value for a $carry
* @param ?callable $func function (mixed $carry, mixed $item) { must return updated $carry }
*
* @return ?mixed
*/
public function fold($initial, ?callable $func = null)
{
$func = self::resolveReducer($func);
if (is_array($this->pipeline)) {
return array_reduce($this->pipeline, $func, $initial);
}
foreach ($this as $value) {
$initial = $func($initial, $value);
}
return $initial;
}
/**
* Resolves a nullable reducer into a sensible callable.
*/
private static function resolveReducer(?callable $func): callable
{
if (null !== $func) {
return $func;
}
return static function ($carry, $item) {
$carry += $item;
return $carry;
};
}
public function getIterator(): Traversable
{
if ($this->pipeline instanceof Traversable) {
return $this->pipeline;
}
if (null !== $this->pipeline) {
return new ArrayIterator($this->pipeline);
}
return new EmptyIterator();
}
/**
* By default returns all values regardless of keys used, discarding all keys in the process. Has an option to keep the keys. This is a terminal operation.
*/
public function toArray(bool $useKeys = false): array
{
if (null === $this->pipeline) {
// With non-primed pipeline just return an empty array.
return [];
}
if ([] === $this->pipeline) {
// Empty array is empty.
return [];
}
// We got what we need, moving along.
if (is_array($this->pipeline)) {
if ($useKeys) {
return $this->pipeline;
}
return array_values($this->pipeline);
}
// Because `yield from` does not reset keys we have to ignore them on export by default to return every item.
// http://php.net/manual/en/language.generators.syntax.php#control-structures.yield.from
return iterator_to_array($this, $useKeys);
}
/**
* {@inheritdoc}
*
* This is a terminal operation.
*
* @see \Countable::count()
*/
public function count(): int
{
if (null === $this->pipeline) {
// With non-primed pipeline just return zero.
return 0;
}
if ([] === $this->pipeline) {
// Empty array is empty.
return 0;
}
if (!is_array($this->pipeline)) {
// Count values for an iterator.
$this->pipeline = iterator_to_array($this, false);
}
return count($this->pipeline);
}
private static function makeNonRewindable(iterable $input): Generator
{
if ($input instanceof Generator) {
return $input;
}
return (static function (iterable $input) {
yield from $input;
})($input);
}
/**
* Extracts a slice from the inputs. Keys are not discarded intentionally.
*
* @see \array_slice()
*
* @param int $offset If offset is non-negative, the sequence will start at that offset. If offset is negative, the sequence will start that far from the end.
* @param ?int $length If length is given and is positive, then the sequence will have up to that many elements in it. If length is given and is negative then the sequence will stop that many elements from the end.
*
* @return $this
*/
public function slice(int $offset, ?int $length = null)
{
if (null === $this->pipeline) {
// With non-primed pipeline just move along.
return $this;
}
if (0 === $length) {
// We're not consuming anything assuming total laziness.
$this->pipeline = null;
return $this;
}
// Shortcut to array_slice() for actual arrays.
if (is_array($this->pipeline)) {
$this->pipeline = array_slice($this->pipeline, $offset, $length, true);
return $this;
}
$this->pipeline = self::makeNonRewindable($this->pipeline);
if ($offset < 0) {
// If offset is negative, the sequence will start that far from the end of the array.
$this->pipeline = self::tail($this->pipeline, -$offset);
}
if ($offset > 0) {
// If offset is non-negative, the sequence will start at that offset in the array.
$this->pipeline = self::skip($this->pipeline, $offset);
}
if ($length < 0) {
// If length is given and is negative then the sequence will stop that many elements from the end of the array.
$this->pipeline = self::head($this->pipeline, -$length);
}
if ($length > 0) {
// If length is given and is positive, then the sequence will have up to that many elements in it.
$this->pipeline = self::take($this->pipeline, $length);
}
return $this;
}
/**
* @psalm-param positive-int $skip
*/
private static function skip(Iterator $input, int $skip): Generator
{
// Consume until seen enough.
foreach ($input as $_) {
if (0 === $skip--) {
break;
}
}
// Avoid yielding from an exhausted generator. Gives error:
// Generator passed to yield from was aborted without proper return and is unable to continue
if (!$input->valid()) {
return;
}
yield from $input;
}
/**
* @psalm-param positive-int $take
*/
private static function take(Generator $input, int $take): Generator
{
while ($input->valid()) {
yield $input->key() => $input->current();
$input->next();
// Stop once taken enough.
if (0 === --$take) {
break;
}
}
}
private static function tail(iterable $input, int $length): Generator
{
$buffer = [];
foreach ($input as $key => $value) {
if (count($buffer) < $length) {
// Read at most N records.
$buffer[] = [$key, $value];
continue;
}
// Remove and add one record each time.
array_shift($buffer);
$buffer[] = [$key, $value];
}
foreach ($buffer as list($key, $value)) {
yield $key => $value;
}
}
/**
* Allocates a buffer of $length, and reads records into it, proceeding with FIFO when buffer is full.
*/
private static function head(iterable $input, int $length): Generator
{
$buffer = [];
foreach ($input as $key => $value) {
$buffer[] = [$key, $value];
if (count($buffer) > $length) {
[$key, $value] = array_shift($buffer);
yield $key => $value;
}
}
}
/**
* Performs a lazy zip operation on iterables, not unlike that of
* array_map with first argument set to null. Also known as transposition.
*
* @return $this
*/
public function zip(iterable ...$inputs)
{
if (null === $this->pipeline) {
$this->pipeline = array_shift($inputs);
}
if ([] === $inputs) {
return $this;
}
$this->map(static function ($item): array {
return [$item];
});
foreach (self::toIterators(...$inputs) as $iterator) {
// MultipleIterator won't work here because it'll stop at first invalid iterator.
$this->map(static function (array $current) use ($iterator) {
if (!$iterator->valid()) {
$current[] = null;
return $current;
}
$current[] = $iterator->current();
$iterator->next();
return $current;
});
}
return $this;
}
/**
* @return Iterator[]
*/
private static function toIterators(iterable ...$inputs): array
{
return array_map(static function (iterable $input): Iterator {
while ($input instanceof IteratorAggregate) {
$input = $input->getIterator();
}
if ($input instanceof Iterator) {
return $input;
}
// IteratorAggregate and Iterator are out of picture, which leaves... an array.
/** @var array $input */
return new ArrayIterator($input);
}, $inputs);
}
/**
* Reservoir sampling method with an optional weighting function. Uses the most optimal algorithm.
*
* @see https://en.wikipedia.org/wiki/Reservoir_sampling
*
* @param int $size The desired sample size
* @param ?callable $weightFunc The optional weighting function
*/
public function reservoir(int $size, ?callable $weightFunc = null): array
{
if (null === $this->pipeline) {
return [];
}
if ($size <= 0) {
return [];
}
// Algorithms below assume inputs are non-rewindable
$this->pipeline = self::makeNonRewindable($this->pipeline);
$result = null === $weightFunc ?
self::reservoirRandom($this->pipeline, $size) :
self::reservoirWeighted($this->pipeline, $size, $weightFunc);
return iterator_to_array($result, true);
}
private static function drainValues(Generator $input): Generator
{
while ($input->valid()) {
yield $input->current();
$input->next();
}
}
/**
* Simple and slow algorithm, commonly known as Algorithm R.
*
* @see https://en.wikipedia.org/wiki/Reservoir_sampling#Simple_algorithm
* @psalm-param positive-int $size
*/
private static function reservoirRandom(Generator $input, int $size): Generator
{
// Take an initial sample (AKA fill the reservoir array)
foreach (self::take($input, $size) as $output) {
yield $output;
}
// Return if there's nothing more to fetch
if (!$input->valid()) {
return;
}
$counter = $size;
// Produce replacement elements with gradually decreasing probability
foreach (self::drainValues($input) as $value) {
$key = mt_rand(0, $counter);
if ($key < $size) {
yield $key => $value;
}
++$counter;
}
}
/**
* Weighted random sampling.
*
* @see https://en.wikipedia.org/wiki/Reservoir_sampling#Algorithm_A-Chao
* @psalm-param positive-int $size
*/
private static function reservoirWeighted(Generator $input, int $size, callable $weightFunc): Generator
{
$sum = 0.0;
// Take an initial sample (AKA fill the reservoir array)
foreach (self::take($input, $size) as $output) {
yield $output;
$sum += $weightFunc($output);
}
// Return if there's nothing more to fetch
if (!$input->valid()) {
return;
}
foreach (self::drainValues($input) as $value) {
$weight = $weightFunc($value);
$sum += $weight;
// probability for this item
$probability = $weight / $sum;
// @infection-ignore-all
if (self::random() <= $probability) {
yield mt_rand(0, $size - 1) => $value;
}
}
}
/**
* Returns a pseudorandom value between zero (inclusive) and one (exclusive).
*
* @infection-ignore-all
*/
private static function random(): float
{
return mt_rand(0, mt_getrandmax() - 1) / mt_getrandmax();
}
}