-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathMulti.php
127 lines (115 loc) · 3.95 KB
/
Multi.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
<?php
declare(strict_types=1);
namespace IterTools;
use IterTools\Util\Iterators\JustifyMultipleIterator;
use IterTools\Util\Iterators\StrictMultipleIterator;
use IterTools\Util\NoValueMonad;
class Multi
{
/**
* Iterate multiple iterable collections simultaneously.
*
* Make an iterator that aggregates items from multiple iterators.
* Similar to Python's zip function.
*
* For uneven lengths, iterations stops when the shortest iterable is exhausted.
*
* @param iterable<mixed> ...$iterables
*
* @return \Generator<array<mixed>>
*/
public static function zip(iterable ...$iterables): \Generator
{
$zippedIterator = new \MultipleIterator();
foreach ($iterables as $iterable) {
$zippedIterator->attachIterator(Transform::toIterator($iterable));
}
foreach ($zippedIterator as $values) {
yield $values;
}
}
/**
* Iterate multiple iterable collections simultaneously.
*
* Make an iterator that aggregates items from multiple iterators.
* Similar to Python's zip_longest function
*
* Iteration continues until the longest iterable is exhausted.
* For uneven lengths, the exhausted iterables will produce null for the remaining iterations.
*
* @param iterable<mixed> ...$iterables
*
* @return \Generator<array<mixed>>
*/
public static function zipLongest(iterable ...$iterables): \Generator
{
$zippedIterator = new \MultipleIterator();
foreach ($iterables as $iterable) {
$zippedIterator->attachIterator(Transform::toIterator($iterable));
}
$zippedIterator->setFlags(\MultipleIterator::MIT_NEED_ANY);
foreach ($zippedIterator as $values) {
yield $values;
}
}
/**
* Iterate multiple iterable collections simultaneously, using a default filler value if lengths are not equal
*
* Make an iterator that aggregates items from multiple iterators.
* Similar to Python's zip_longest function
*
* Iteration continues until the longest iterable is exhausted.
* For uneven lengths, the exhausted iterables will produce $filler value for the remaining iterations.
*
* @param mixed $filler
* @param iterable<mixed> ...$iterables
*
* @return \Generator<array<mixed>>
*/
public static function zipFilled($filler, iterable ...$iterables): \Generator
{
$iterator = new JustifyMultipleIterator($filler, ...$iterables);
foreach ($iterator as $values) {
yield $values;
}
}
/**
* Iterate multiple iterable collections of equal lengths simultaneously.
*
* Works like Multi::zip() method but throws \LengthException if lengths not equal,
* i.e., at least one iterator ends before the others.
*
* @param iterable<mixed> ...$iterables
*
* @return \Generator<array<mixed>>
* @throws \LengthException if during iteration one iterable ends before the others, indicating unequal lengths
*/
public static function zipEqual(iterable ...$iterables): \Generator
{
$zippedIterator = new StrictMultipleIterator(\MultipleIterator::MIT_NEED_ALL);
foreach ($iterables as $iterable) {
$zippedIterator->attachIterator(Transform::toIterator($iterable));
}
$zippedIterator->setFlags(\MultipleIterator::MIT_NEED_ALL);
foreach ($zippedIterator as $values) {
yield $values;
}
}
/**
* Chain multiple iterables together into a single iteration.
*
* Makes a single continuous sequence out of multiple sequences.
*
* @param iterable<mixed> ...$iterables
*
* @return \Generator<mixed>
*/
public static function chain(iterable ...$iterables): \Generator
{
foreach ($iterables as $iterable) {
foreach ($iterable as $item) {
yield $item;
}
}
}
}