-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sequence.php
685 lines (624 loc) · 21.8 KB
/
Sequence.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
<?php
declare(strict_types=1);
namespace BradynPoulsen\Sequences;
use InvalidArgumentException;
use IteratorAggregate;
use OutOfRangeException;
use Traversable;
use UnexpectedValueException;
/**
* A sequence of values that can be iterated over. The values are evaluated lazily, and the sequence
* is potentially infinite.
*
* Sequences can be iterated multiple times, unless implementations constrain themselves to be iterated
* only once. Operations, like map, filter, etc, generally preserved this constraint, and must be
* documented if it doesn't.
*
* Operations must be classified into groups of state requirements and effect.
*
* State Requirements:
* - @state stateless - operations which require no state and process each element independently.
* - @state stateful - operations which require an amount of state, usually proportional to number of elements
*
* Effect:
* - @effect intermediate - operations that return another sequence, which process each element lazily
* - @effect terminal - operations that consume the sequence to return a non-sequence result
*/
interface Sequence extends IteratorAggregate
{
/**
* Returns an iterator over the values in this sequence.
*
* @effect terminal
*
* @throws SequenceAlreadyIteratedException if the sequence is constrained to be iterated only once and
* {@see Sequence::getIterator()} is invoked a second time.
*/
public function getIterator(): Traversable;
/**
* Returns `true` if all elements match the given $predicate.
*
* @effect terminal
*
* @param callable $predicate (T) -> bool
* @return bool Sequence<T> -> bool
*/
public function all(callable $predicate): bool;
/**
* Returns `true` if at least one element matches the given $predicate. If no $predicate is specified, returns
* `true` if this sequence contains at least one element.
*
* @effect terminal
*
* @param callable|null $predicate (T) -> bool
* @return bool Sequence<T> -> bool
*/
public function any(?callable $predicate = null): bool;
/**
* Returns an associative array containing key-value pairs provided by $transform function applied to elements of
* this sequence.
*
* @effect terminal
*
* @param callable $transform (T) -> [K, V]
* @return array Sequence<T> -> [K => V]
*/
public function associate(callable $transform): array;
/**
* Returns an associative array containing the values provided by $valueSelector and indexed by $keySelector
* functions applied to elements of this sequence.
*
* If $valueSelector is not provided, the element will be used.
*
* @effect terminal
*
* @param callable $keySelector (T) -> K
* @param callable|null $valueSelector (T) -> V
* @return array Sequence<T> -> [K => V]
*/
public function associateBy(callable $keySelector, ?callable $valueSelector = null): array;
/**
* Returns an associative array where keys are elements from the given sequence and values are produced by the
* $valueSelector function applied to each element.
*
* @effect terminal
*
* @param callable $valueSelector (T) -> V
* @return array Sequence<T> -> [T => V]
*/
public function associateWith(callable $valueSelector): array;
/**
* Returns an average value of elements in this sequence.
*
* @effect terminal
*
* @return float Sequence<T> -> float
*/
public function average(): float;
/**
* Returns the average value of all values produced by $selector function applied to each element in this sequence.
*
* @effect terminal
*
* @param callable $selector (T) -> int|float
* @return float Sequence<T> -> float
*/
public function averageBy(callable $selector): float;
/**
* Splits this sequence into a sequence of arrays each not exceeding the given $size.
*
* The last list in the resulting sequence may have less elements than the given $size.
*
* @effect intermediate
* @state stateful
*
* @param callable|null $transform (T[] $chunk) -> R
* @return Sequence Sequence<T> -> Sequence<R>
*/
public function chunked(int $size, ?callable $transform = null): Sequence;
/**
* Returns a wrapper {@see Sequence} that provides values of this sequence, but ensures it can be iterated only
* one time.
*
* @effect intermediate
* @state stateless
*
* @return Sequence Sequence<T> -> Sequence<T>
*/
public function constrainOnce(): Sequence;
/**
* Returns true if element is found in the sequence.
*
* @param mixed $element The element to search for
* @return bool Sequence<T> -> bool
*/
public function contains($element): bool;
/**
* Returns the number of elements matching the given $predicate, if provided.
*
* @effect terminal
*
* @param callable|null $predicate (T) -> bool
* @return int Sequence<T> -> int
*/
public function count(?callable $predicate = null): int;
/**
* Returns a sequence containing only distinct elements from this sequence.
*
* @effect intermediate
* @state stateful
*
* @return Sequence Sequence<T> -> Sequence<T>
*/
public function distinct(): Sequence;
/**
* Returns a sequence containing only elements from the given sequence having distinct keys returned
* by the given selector function.
*
* @effect intermediate
* @state stateful
*
* @param callable $selector (T) -> K
* @return Sequence Sequence<T> -> Sequence<T>
*/
public function distinctBy(callable $selector): Sequence;
/**
* Returns a sequence containing all elements except first $count elements.
*
* @effect intermediate
* @state stateless
*
* @return Sequence Sequence<T> -> Sequence<T>
*/
public function drop(int $count): Sequence;
/**
* Returns a sequence containing all elements except first elements that satisfy the given $predicate.
*
* @effect intermediate
* @state stateless
*
* @param callable $predicate (T) -> bool
* @return Sequence
*/
public function dropWhile(callable $predicate): Sequence;
/**
* Returns an element at the given $index or throws an {@see OutOfRangeException} if the index is out of bounds
* of this sequence.
*
* @effect terminal
*
* @return mixed Sequence<T> -> T
* @throws OutOfRangeException if the given index is not contained.
*/
public function elementAt(int $index);
/**
* Returns an element at the given $index or the result of calling the $defaultValue function if the index is out
* of bounds of this sequence.
*
* @effect terminal
*
* @param callable $defaultValue (int) -> T
* @return mixed Sequence<T> -> T
*/
public function elementAtOrElse(int $index, callable $defaultValue);
/**
* Returns an element at the given $index or null if the index is out of bounds of this sequence.
*
* @effect terminal
*
* @return mixed|null Sequence<T> -> ?T
*/
public function elementAtOrNull(int $index);
/**
* Returns a sequence contain all elements of this sequence that match the provided $predicate.
*
* The index of an element may be obtained by accepting a 2nd argument in the $predicate function.
*
* @effect intermediate
* @state stateful
*
* @param callable $predicate (T $element[, int $index]) -> bool
* @return Sequence Sequence<T> -> Sequence<T>
*/
public function filter(callable $predicate): Sequence;
/**
* Returns a sequence containing all elements of this sequence that are instances of the provided object $type.
*
* @effect intermediate
* @state stateful
*
* @param string $type <R> The FQCN of the desired type.
* @return Sequence Sequence<T> -> Sequence<R>
*/
public function filterIsInstance(string $type): Sequence;
/**
* Returns a sequence contain all elements of this sequence that DO NOT match the provided $predicate.
*
* The index of an element may be obtained by accepting a 2nd argument in the $predicate function.
*
* @effect intermediate
* @state stateful
*
* @param callable $predicate (T $element[, int $index]) -> bool
* @return Sequence Sequence<T> -> Sequence<T>
*/
public function filterNot(callable $predicate): Sequence;
/**
* Returns the first element matching the given $predicate.
*
* @effect terminal
*
* @param callable|null $predicate (T) -> bool
* @return mixed Sequence<T> -> T
* @throws UnexpectedValueException if no elements matched the predicate
*/
public function first(?callable $predicate = null);
/**
* Returns the first element matching the given $predicate, or null if element was not found.
*
* @effect terminal
*
* @param callable|null $predicate (T) -> bool
* @return mixed Sequence<T> -> T
*/
public function firstOrNull(?callable $predicate = null);
/**
* Returns a single sequence of all elements from results of the provided $transform function being invoked
* on each element of the original sequence.
*
* @effect intermediate
* @state stateless
*
* @param callable $transform (T $element[, int $index]) -> iterable<R>
* @return Sequence Sequence<T> -> Sequence<R>
*/
public function flatMap(callable $transform): Sequence;
/**
* Returns a single sequence of all elements from each elements of the original sequence.
*
* @effect intermediate
* @state stateless
*
* @return Sequence Sequence<iterable<R>> -> Sequence<R>
*/
public function flatten(): Sequence;
/**
* Accumulates value starting with initial value and applying $operation from left to right to current accumulator
* value and each element with its index in the original sequence.
*
* @effect terminal
*
* @param mixed $initial <R> The initial accumulator value to use in the $operation.
* @param callable $operation (R $acc, T $element[, int $index]) -> R
* @return mixed Sequence<T> -> R
*/
public function fold($initial, callable $operation);
/**
* Groups values returned by the $valueTransform function, if given, applied to each element of the this
* sequence by the key returned by the given $keySelector function applied to the element and returns an
* associative array where each group key is associated with a list of corresponding values.
*
* @effect terminal
*
* @param callable $keySelector (T) -> K
* @param callable|null $valueTransform (T) -> V
* @return array [K => V[]]
*/
public function groupBy(callable $keySelector, ?callable $valueTransform = null): array;
/**
* Returns a sequence that iterates through the elements either of this sequence or, if this sequence turns
* out to be empty, of the sequence returned by the provided $supplier function.
*
* @effect intermediate
* @state stateless
*
* @param callable $supplier () -> Sequence<B>
* @return Sequence Sequence<A> -> Sequence<A|B>
*/
public function ifEmpty(callable $supplier): Sequence;
/**
* Returns first index of $element, or -1 if this sequence does not contain $element.
*
* @effect terminal
*
* @param mixed $element The element to search for
* @return int Sequence<T> -> int
*/
public function indexOf($element): int;
/**
* Returns the last element matching the given $predicate.
*
* @effect terminal
*
* @param callable|null $predicate (T) -> bool
* @return mixed Sequence<T> -> T
* @throws UnexpectedValueException if no elements matched the predicate
*/
public function last(?callable $predicate = null);
/**
* Returns last index of $element, or -1 if this sequence does not contain $element.
*
* @effect terminal
*
* @param mixed $element The element to search for
* @return int Sequence<T> -> int
*/
public function lastIndexOf($element): int;
/**
* Returns the last element matching the given $predicate, or null if element was not found.
*
* @effect terminal
*
* @param callable|null $predicate (T) -> bool
* @return mixed Sequence<T> -> T
*/
public function lastOrNull(?callable $predicate = null);
/**
* Returns a sequence containing the results of applying the given $transform function to each element of
* this sequence.
*
* The index of an element may be obtained by accepting a 2nd argument in the $transform function.
*
* @effect intermediate
* @state stateless
*
* @param callable $transform (T $element[, int $index]) -> R
* @return Sequence Sequence<T> -> Sequence<R>
*/
public function map(callable $transform): Sequence;
/**
* Returns the largest element or null if there are no elements.
*
* @effect terminal
*
* @return mixed|null Sequence<T> -> ?T
*/
public function max();
/**
* Returns the first element yielding the largest value of the given $selector or null if there are no elements.
*
* @effect terminal
*
* @param callable $selector (T) -> R
* @return mixed|null Sequence<T> -> ?T
*/
public function maxBy(callable $selector);
/**
* Returns the first element having the largest value according to the provided $comparator or null if there are
* no elements.
*
* @effect terminal
*
* @param callable $comparator (T $a, T $b) -> int
* @return mixed|null Sequence<T> -> ?T
*/
public function maxWith(callable $comparator);
/**
* Returns the smallest element or null if there are no elements.
*
* @effect terminal
*
* @return mixed|null Sequence<T> -> ?T
*/
public function min();
/**
* Returns the first element yielding the smallest value of the given $selector or null if there are no elements.
*
* @effect terminal
*
* @param callable $selector (T) -> R
* @return mixed|null Sequence<T> -> ?T
*/
public function minBy(callable $selector);
/**
* Returns the first element having the smallest value according to the provided $comparator or null if there are
* no elements.
*
* @effect terminal
*
* @param callable $comparator (T $a, T $b) -> int
* @return mixed|null Sequence<T> -> ?T
*/
public function minWith(callable $comparator);
/**
* Returns a sequence containing all elements of original sequence except the given $elements.
*
* If the provided $elements is an Iterator, the resulting sequence will be constrained to being iterated
* only once. See {@see Sequence::constrainOnce()}.
*
* @effect intermediate
* @state stateful
*
* @param iterable $elements iterable<B>
* @return Sequence Sequence<A|B> -> Sequence<A>
*/
public function minus(iterable $elements): Sequence;
/**
* Returns `true` if no element matches the given $predicate. If no $predicate is specified, returns `true` if
* this sequence contains no elements.
*
* @effect terminal
*
* @param callable|null $predicate (T) -> bool
* @return bool Sequence<T> -> bool
*/
public function none(?callable $predicate = null): bool;
/**
* Returns a sequence which performs the given $action on each element of the original sequence.
*
* @effect intermediate
* @state stateless
*
* @param callable $action (T) -> void
* @return Sequence Sequence<T> -> Sequence<T>
*/
public function onEach(callable $action): Sequence;
/**
* Returns a sequence containing all elements of original sequence and then all elements of the given $elements.
*
* If the provided $elements is an Iterator, the resulting sequence will be constrained to being iterated
* only once. See {@see Sequence::constrainOnce()}.
*
* @effect intermediate
* @state stateless
*
* @param iterable $elements iterable<B>
* @return Sequence Sequence<A> -> Sequence<A|B>
*/
public function plus(iterable $elements): Sequence;
/**
* Accumulates value starting with the first element and applying $operation from left to right to current
* accumulator value and each element with its index in the original sequence.
*
* @effect terminal
*
* @param callable $operation (R $acc, T $element[, int $index]) -> R
* @return mixed Sequence<T> -> R
*/
public function reduce(callable $operation);
/**
* Returns a sequence which validates each element matches the given $predicate.
* {@see InvalidArgumentException} will be thrown if an element does not match the given $predicate.
*
* @effect intermediate
* @state stateless
*
* @param callable $predicate (T) -> bool
* @return Sequence Sequence<T> -> Sequence<T>
*/
public function require(callable $predicate): Sequence;
/**
* Returns a sequence which validates each element does not match the given $predicate.
* {@see InvalidArgumentException} will be thrown if an element matches the given $predicate.
*
* @effect intermediate
* @state stateless
*
* @param callable $predicate (T) -> bool
* @return Sequence Sequence<T> -> Sequence<T>
*/
public function requireNot(callable $predicate): Sequence;
/**
* Returns a sequence that yields elements of this sequence sorted according to their natural sort order.
*
* @effect intermediate
* @effect stateful
*
* @return Sequence Sequence<T> -> Sequence<T>
*/
public function sorted(): Sequence;
/**
* Returns a sequence that yields elements of this sequence sorted according to natural sort order of the
* value returned by specified $selector function.
*
* @effect intermediate
* @effect stateful
*
* @param callable $selector (T) -> K
* @return Sequence Sequence<T> -> Sequence<T>
*/
public function sortedBy(callable $selector): Sequence;
/**
* Returns a sequence that yields elements of this sequence sorted descending according to natural sort order of the
* value returned by specified $selector function.
*
* @effect intermediate
* @effect stateful
*
* @param callable $selector (T) -> K
* @return Sequence Sequence<T> -> Sequence<T>
*/
public function sortedByDescending(callable $selector): Sequence;
/**
* Returns a sequence that yields elements of this sequence sorted descending according to their natural sort order.
*
* @effect intermediate
* @effect stateful
*
* @return Sequence Sequence<T> -> Sequence<T>
*/
public function sortedDescending(): Sequence;
/**
* Returns a sequence that yields elements of this sequence sorted according to the specified $comparator.
*
* @effect intermediate
* @effect stateful
*
* @param callable $comparator (T $a, T $b) -> int
* @return Sequence Sequence<T> -> Sequence<T>
*
* @see usort() for an example of how the $comparator must behave
*/
public function sortedWith(callable $comparator): Sequence;
/**
* Returns a sequence that yields elements of this sequence sorted descending according to the
* specified $comparator.
*
* @effect intermediate
* @effect stateful
*
* @param callable $comparator (T $a, T $b) -> int
* @return Sequence Sequence<T> -> Sequence<T>
*
* @see usort() for an example of how the $comparator must behave
*/
public function sortedWithDescending(callable $comparator): Sequence;
/**
* Returns the sum of all elements in this sequence.
*
* @effect terminal
*
* @return int|float Sequence<T> -> int|float
*/
public function sum();
/**
* Returns the sum of all values produced by $selector function applied to each element in this sequence.
*
* @effect terminal
*
* @param callable $selector (T) -> int|float
* @return int|float Sequence<T> -> int|float
*/
public function sumBy(callable $selector);
/**
* Returns a sequence containing first $count elements.
*
* @effect intermediate
* @state stateless
*
* @return Sequence
*/
public function take(int $count): Sequence;
/**
* Returns a sequence containing first elements satisfying the given $predicate.
*
* @effect intermediate
* @state stateless
*
* @param callable $predicate (T) -> bool
* @return Sequence
*/
public function takeWhile(callable $predicate): Sequence;
/**
* Returns a sequence of results of applying the given $transform function to arrays that represent a window
* of the given $size sliding along this sequence with the given $step.
*
* @effect intermediate
* @state stateful
*
* @param int $size the number of elements to take in each window, must be positive
* @param int $step the number of elements to move the window forward by on each step, must be positive
* @param bool $partialWindows whether or not to keep partial windows in the end, if any
* @param callable|null $transform (T[] $window) -> R defaults to returning the window as an array
* @return Sequence Sequence<T> -> Sequence<R>
*
* @see SequenceOptions::INCLUDE_PARTIAL_WINDOWS
* @see SequenceOptions::NO_PARTIAL_WINDOWS
*/
public function windowed(
int $size,
int $step = 1,
bool $partialWindows = SequenceOptions::NO_PARTIAL_WINDOWS,
?callable $transform = null
): Sequence;
}