1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Collections\Iterator; |
4
|
|
|
|
5
|
|
|
use Collections\Immutable\ImmArrayList; |
6
|
|
|
use Collections\Immutable\ImmDictionary; |
7
|
|
|
use Collections\Immutable\ImmSet; |
8
|
|
|
use Collections\Map; |
9
|
|
|
use Collections\Pair; |
10
|
|
|
use Collections\Set; |
11
|
|
|
use Collections\Vector; |
12
|
|
|
use Collections\VectorInterface; |
13
|
|
|
|
14
|
|
|
trait LazyKeyedIterableTrait |
15
|
|
|
{ |
16
|
|
|
public function toArray() |
17
|
|
|
{ |
18
|
|
|
$arr = []; |
19
|
|
|
foreach ($this as $k => $v) { |
|
|
|
|
20
|
|
|
$arr[$k] = $v; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
return $arr; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function toValuesArray() |
27
|
|
|
{ |
28
|
|
|
$arr = []; |
29
|
|
|
foreach ($this as $v) { |
|
|
|
|
30
|
|
|
$arr[] = $v; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
return $arr; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function toKeysArray() |
37
|
|
|
{ |
38
|
|
|
$arr = []; |
39
|
|
|
foreach ($this as $k => $_) { |
|
|
|
|
40
|
|
|
$arr[] = $k; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return $arr; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function toVector() |
47
|
|
|
{ |
48
|
|
|
return new Vector($this); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function toImmVector() |
52
|
|
|
{ |
53
|
|
|
return new ImmArrayList($this); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function toMap() |
57
|
|
|
{ |
58
|
|
|
return new Map($this); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function toImmMap() |
62
|
|
|
{ |
63
|
|
|
return new ImmDictionary($this); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function toSet() |
67
|
|
|
{ |
68
|
|
|
return new Set($this); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function toImmSet() |
72
|
|
|
{ |
73
|
|
|
return new ImmSet($this); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function lazy() |
77
|
|
|
{ |
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function values() |
82
|
|
|
{ |
83
|
|
|
return new LazyValuesIterable($this); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function keys() |
87
|
|
|
{ |
88
|
|
|
return new LazyKeysIterable($this); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function map($callback) |
92
|
|
|
{ |
93
|
|
|
return new LazyMapKeyedIterable($this, $callback); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function mapWithKey($callback) |
97
|
|
|
{ |
98
|
|
|
return new LazyMapWithKeyIterable($this, $callback); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function filter($callback) |
102
|
|
|
{ |
103
|
|
|
return new LazyFilterKeyedIterable($this, $callback); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function filterWithKey($callback) |
107
|
|
|
{ |
108
|
|
|
return new LazyFilterWithKeyIterable($this, $callback); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function zip($traversable) |
112
|
|
|
{ |
113
|
|
|
if (is_array($traversable)) { |
114
|
|
|
$traversable = new ImmDictionary($traversable); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return new LazyZipKeyedIterable($this, $traversable); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function take($n) |
121
|
|
|
{ |
122
|
|
|
return new LazyTakeKeyedIterable($this, $n); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function takeWhile($fn) |
126
|
|
|
{ |
127
|
|
|
return new LazyTakeWhileKeyedIterable($this, $fn); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function skip($n) |
131
|
|
|
{ |
132
|
|
|
return new LazySkipKeyedIterable($this, $n); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function skipWhile($fn) |
136
|
|
|
{ |
137
|
|
|
return new LazySkipWhileKeyedIterable($this, $fn); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function slice($start, $len) |
141
|
|
|
{ |
142
|
|
|
return new LazySliceKeyedIterable($this, $start, $len); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function concat($iterable) |
146
|
|
|
{ |
147
|
|
|
if (is_array($iterable)) { |
148
|
|
|
$iterable = new ImmDictionary($iterable); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return new LazyConcatIterable($this, $iterable); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function first() |
155
|
|
|
{ |
156
|
|
|
foreach ($this as $v) { |
|
|
|
|
157
|
|
|
return $v; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return null; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function firstKey() |
164
|
|
|
{ |
165
|
|
|
foreach ($this as $k => $_) { |
|
|
|
|
166
|
|
|
return $k; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return null; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function last() |
173
|
|
|
{ |
174
|
|
|
$v = null; |
175
|
|
|
foreach ($this as $v) { |
|
|
|
|
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return $v; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function lastKey() |
182
|
|
|
{ |
183
|
|
|
$k = null; |
184
|
|
|
foreach ($this as $k => $_) { |
|
|
|
|
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
return $k; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* {@inheritDoc} |
192
|
|
|
* @return $this |
193
|
|
|
*/ |
194
|
|
|
public function each(callable $callable) |
195
|
|
|
{ |
196
|
|
|
foreach ($this as $v) { |
|
|
|
|
197
|
|
|
$callable($v); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
return $this; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* {@inheritdoc} |
205
|
|
|
*/ |
206
|
|
|
public function exists(callable $fn) |
207
|
|
|
{ |
208
|
|
|
foreach ($this as $element) { |
|
|
|
|
209
|
|
|
if ($fn($element)) { |
210
|
|
|
return true; |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
return false; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
View Code Duplication |
public function concatAll() |
|
|
|
|
218
|
|
|
{ |
219
|
|
|
/** @var VectorInterface $results */ |
220
|
|
|
$results = new static(); |
221
|
|
|
$this->each(function ($subArray) use ($results) { |
222
|
|
|
foreach ($subArray as $item) { |
223
|
|
|
$results->add($item); |
224
|
|
|
} |
225
|
|
|
}); |
226
|
|
|
|
227
|
|
|
return $results; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* {@inheritDoc} |
232
|
|
|
* @return $this |
233
|
|
|
*/ |
234
|
|
View Code Duplication |
public function groupBy($callback) |
|
|
|
|
235
|
|
|
{ |
236
|
|
|
$group = new Map(); |
237
|
|
|
foreach ($this as $value) { |
|
|
|
|
238
|
|
|
$key = $callback($value); |
239
|
|
|
if (!$group->containsKey($key)) { |
240
|
|
|
$element = $this instanceof VectorInterface ? new static([$value]) : new Vector([$value]); |
|
|
|
|
241
|
|
|
$group->add(new Pair($key, $element)); |
242
|
|
|
} else { |
243
|
|
|
$value = $group->get($key)->add($value); |
244
|
|
|
$group->set($key, $value); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
return $group; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* {@inheritDoc} |
253
|
|
|
* @return $this |
254
|
|
|
*/ |
255
|
|
View Code Duplication |
public function indexBy($callback) |
|
|
|
|
256
|
|
|
{ |
257
|
|
|
$group = new Map(); |
258
|
|
|
foreach ($this as $value) { |
|
|
|
|
259
|
|
|
$key = $callback($value); |
260
|
|
|
$group->set($key, $value); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
return $group; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* {@inheritdoc} |
268
|
|
|
*/ |
269
|
|
|
public function reduce(callable $callback, $initial = null) |
270
|
|
|
{ |
271
|
|
|
foreach ($this as $element) { |
|
|
|
|
272
|
|
|
$initial = $callback($initial, $element); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
return $initial; |
276
|
|
|
} |
277
|
|
|
} |