1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Innmind\Immutable; |
5
|
|
|
|
6
|
|
|
use Innmind\Immutable\{ |
7
|
|
|
Exception\OutOfBoundException, |
8
|
|
|
Exception\LogicException, |
9
|
|
|
Exception\ElementNotFoundException, |
10
|
|
|
Exception\GroupEmptySequenceException |
11
|
|
|
}; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* A defined set of ordered elements |
15
|
|
|
*/ |
16
|
|
|
class Sequence implements SequenceInterface |
17
|
|
|
{ |
18
|
|
|
private $values; |
19
|
|
|
private $size; |
20
|
|
|
|
21
|
92 |
|
public function __construct(...$values) |
22
|
|
|
{ |
23
|
92 |
|
$this->values = $values; |
24
|
92 |
|
$this->size = count($values); |
25
|
92 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
29 |
|
public function size(): int |
31
|
|
|
{ |
32
|
29 |
|
return $this->size; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
1 |
|
public function count(): int |
39
|
|
|
{ |
40
|
1 |
|
return $this->size(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
10 |
|
public function current() |
47
|
|
|
{ |
48
|
10 |
|
return current($this->values); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
12 |
|
public function key() |
55
|
|
|
{ |
56
|
12 |
|
return key($this->values); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
12 |
|
public function next() |
63
|
|
|
{ |
64
|
12 |
|
next($this->values); |
65
|
12 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
10 |
|
public function rewind() |
71
|
|
|
{ |
72
|
10 |
|
reset($this->values); |
73
|
10 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
10 |
|
public function valid(): bool |
79
|
|
|
{ |
80
|
10 |
|
return $this->key() !== null; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
1 |
|
public function offsetExists($offset): bool |
87
|
|
|
{ |
88
|
1 |
|
return array_key_exists($offset, $this->values); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
1 |
|
public function offsetGet($offset) |
95
|
|
|
{ |
96
|
1 |
|
if (!$this->offsetExists($offset)) { |
97
|
|
|
throw new OutOfBoundException(sprintf( |
98
|
|
|
'Unknown index %s', |
99
|
|
|
$offset |
100
|
|
|
)); |
101
|
|
|
} |
102
|
|
|
|
103
|
1 |
|
return $this->values[$offset]; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritdoc} |
108
|
|
|
*/ |
109
|
1 |
|
public function offsetSet($offset, $value) |
110
|
|
|
{ |
111
|
1 |
|
throw new LogicException('You can\'t modify a sequence'); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* {@inheritdoc} |
116
|
|
|
*/ |
117
|
1 |
|
public function offsetUnset($offset) |
118
|
|
|
{ |
119
|
1 |
|
throw new LogicException('You can\'t modify a sequence'); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritdoc} |
124
|
|
|
*/ |
125
|
44 |
|
public function toPrimitive() |
126
|
|
|
{ |
127
|
44 |
|
return $this->values; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* {@inheritdoc} |
132
|
|
|
*/ |
133
|
14 |
|
public function get(int $index) |
134
|
|
|
{ |
135
|
14 |
|
if ($index >= $this->size()) { |
136
|
1 |
|
throw new OutOfBoundException; |
137
|
|
|
} |
138
|
|
|
|
139
|
13 |
|
return $this->values[$index]; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* {@inheritdoc} |
144
|
|
|
*/ |
145
|
2 |
|
public function diff(SequenceInterface $seq): SequenceInterface |
146
|
|
|
{ |
147
|
2 |
|
return new self( |
148
|
2 |
|
...array_diff( |
149
|
2 |
|
$this->values, |
150
|
2 |
|
$seq->toPrimitive() |
151
|
|
|
) |
152
|
|
|
); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* {@inheritdoc} |
157
|
|
|
*/ |
158
|
1 |
|
public function distinct(): SequenceInterface |
159
|
|
|
{ |
160
|
1 |
|
return new self(...array_unique($this->values)); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* {@inheritdoc} |
165
|
|
|
*/ |
166
|
8 |
|
public function drop(int $size): SequenceInterface |
167
|
|
|
{ |
168
|
8 |
|
return new self(...array_slice($this->values, $size)); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* {@inheritdoc} |
173
|
|
|
*/ |
174
|
2 |
|
public function dropEnd(int $size): SequenceInterface |
175
|
|
|
{ |
176
|
2 |
|
return new self(...array_slice($this->values, 0, $this->size() - $size)); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* {@inheritdoc} |
181
|
|
|
*/ |
182
|
6 |
|
public function equals(SequenceInterface $seq): bool |
183
|
|
|
{ |
184
|
6 |
|
return $this->values === $seq->toPrimitive(); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* {@inheritdoc} |
189
|
|
|
*/ |
190
|
2 |
|
public function filter(\Closure $predicate): SequenceInterface |
191
|
|
|
{ |
192
|
2 |
|
return new self(...array_filter( |
193
|
2 |
|
$this->values, |
194
|
|
|
$predicate |
195
|
|
|
)); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* {@inheritdoc} |
200
|
|
|
*/ |
201
|
3 |
|
public function foreach(\Closure $function): SequenceInterface |
202
|
|
|
{ |
203
|
3 |
|
foreach ($this->values as $value) { |
204
|
3 |
|
$function($value); |
205
|
|
|
} |
206
|
|
|
|
207
|
3 |
|
return $this; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* {@inheritdoc} |
212
|
|
|
*/ |
213
|
3 |
View Code Duplication |
public function groupBy(\Closure $discriminator): MapInterface |
214
|
|
|
{ |
215
|
3 |
|
if ($this->size() === 0) { |
216
|
1 |
|
throw new GroupEmptySequenceException; |
217
|
|
|
} |
218
|
|
|
|
219
|
2 |
|
$map = null; |
220
|
|
|
|
221
|
2 |
|
foreach ($this->values as $value) { |
222
|
2 |
|
$key = $discriminator($value); |
223
|
|
|
|
224
|
2 |
|
if ($map === null) { |
225
|
2 |
|
$type = gettype($key); |
226
|
2 |
|
$map = new Map( |
227
|
2 |
|
$type === 'object' ? get_class($key) : $type, |
228
|
2 |
|
SequenceInterface::class |
229
|
|
|
); |
230
|
|
|
} |
231
|
|
|
|
232
|
2 |
|
if ($map->contains($key)) { |
233
|
2 |
|
$map = $map->put( |
234
|
|
|
$key, |
235
|
2 |
|
$map->get($key)->add($value) |
236
|
|
|
); |
237
|
|
|
} else { |
238
|
2 |
|
$map = $map->put($key, new self($value)); |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|
242
|
2 |
|
return $map; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* {@inheritdoc} |
247
|
|
|
*/ |
248
|
2 |
|
public function first() |
249
|
|
|
{ |
250
|
2 |
|
return $this->values[0]; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* {@inheritdoc} |
255
|
|
|
*/ |
256
|
2 |
|
public function last() |
257
|
|
|
{ |
258
|
2 |
|
return $this->values[$this->size() - 1]; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* {@inheritdoc} |
263
|
|
|
*/ |
264
|
45 |
|
public function contains($element): bool |
265
|
|
|
{ |
266
|
45 |
|
return in_array($element, $this->values, true); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* {@inheritdoc} |
271
|
|
|
*/ |
272
|
16 |
|
public function indexOf($element): int |
273
|
|
|
{ |
274
|
16 |
|
$index = array_search($element, $this->values, true); |
275
|
|
|
|
276
|
16 |
|
if ($index === false) { |
277
|
1 |
|
throw new ElementNotFoundException; |
278
|
|
|
} |
279
|
|
|
|
280
|
15 |
|
return $index; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* {@inheritdoc} |
285
|
|
|
*/ |
286
|
1 |
|
public function indices(): SequenceInterface |
287
|
|
|
{ |
288
|
1 |
|
return new self(...array_keys($this->values)); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* {@inheritdoc} |
293
|
|
|
*/ |
294
|
2 |
|
public function map(\Closure $function): SequenceInterface |
295
|
|
|
{ |
296
|
2 |
|
return new self(...array_map($function, $this->values)); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* {@inheritdoc} |
301
|
|
|
*/ |
302
|
1 |
|
public function pad(int $size, $element): SequenceInterface |
303
|
|
|
{ |
304
|
1 |
|
return new self(...array_pad($this->values, $size, $element)); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* {@inheritdoc} |
309
|
|
|
*/ |
310
|
2 |
|
public function partition(\Closure $predicate): SequenceInterface |
311
|
|
|
{ |
312
|
2 |
|
$truthy = []; |
313
|
2 |
|
$falsy = []; |
314
|
|
|
|
315
|
2 |
|
foreach ($this->values as $value) { |
316
|
2 |
|
if ($predicate($value) === true) { |
317
|
2 |
|
$truthy[] = $value; |
318
|
|
|
} else { |
319
|
2 |
|
$falsy[] = $value; |
320
|
|
|
} |
321
|
|
|
} |
322
|
|
|
|
323
|
2 |
|
return new self( |
324
|
2 |
|
new self(...$truthy), |
325
|
2 |
|
new self(...$falsy) |
326
|
|
|
); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* {@inheritdoc} |
331
|
|
|
*/ |
332
|
14 |
|
public function slice(int $from, int $until): SequenceInterface |
333
|
|
|
{ |
334
|
14 |
|
return new self(...array_slice( |
335
|
14 |
|
$this->values, |
336
|
|
|
$from, |
337
|
14 |
|
$until - $from |
338
|
|
|
)); |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* {@inheritdoc} |
343
|
|
|
*/ |
344
|
1 |
|
public function splitAt(int $index): SequenceInterface |
345
|
|
|
{ |
346
|
1 |
|
return new self( |
347
|
1 |
|
$this->slice(0, $index), |
348
|
1 |
|
$this->slice($index, $this->size()) |
349
|
|
|
); |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* {@inheritdoc} |
354
|
|
|
*/ |
355
|
8 |
|
public function take(int $size): SequenceInterface |
356
|
|
|
{ |
357
|
8 |
|
return $this->slice(0, $size); |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* {@inheritdoc} |
362
|
|
|
*/ |
363
|
2 |
|
public function takeEnd(int $size): SequenceInterface |
364
|
|
|
{ |
365
|
2 |
|
return $this->slice($this->size() - $size, $this->size()); |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
/** |
369
|
|
|
* {@inheritdoc} |
370
|
|
|
*/ |
371
|
9 |
|
public function append(SequenceInterface $seq): SequenceInterface |
372
|
|
|
{ |
373
|
9 |
|
return new self(...$this->values, ...$seq->toPrimitive()); |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
/** |
377
|
|
|
* {@inheritdoc} |
378
|
|
|
*/ |
379
|
2 |
|
public function intersect(SequenceInterface $seq): SequenceInterface |
380
|
|
|
{ |
381
|
2 |
|
return new self(...array_intersect($this->values, $seq->toPrimitive())); |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* {@inheritdoc} |
386
|
|
|
*/ |
387
|
3 |
|
public function join(string $separator): StringPrimitive |
388
|
|
|
{ |
389
|
3 |
|
return new StringPrimitive(implode($separator, $this->values)); |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
/** |
393
|
|
|
* {@inheritdoc} |
394
|
|
|
*/ |
395
|
43 |
|
public function add($element): SequenceInterface |
396
|
|
|
{ |
397
|
43 |
|
$values = $this->values; |
398
|
43 |
|
$values[] = $element; |
399
|
|
|
|
400
|
43 |
|
return new self(...$values); |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
/** |
404
|
|
|
* {@inheritdoc} |
405
|
|
|
*/ |
406
|
2 |
|
public function sort(\Closure $function): SequenceInterface |
407
|
|
|
{ |
408
|
2 |
|
$values = $this->values; |
409
|
2 |
|
usort($values, $function); |
410
|
|
|
|
411
|
2 |
|
return new self(...$values); |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
/** |
415
|
|
|
* {@inheritdoc} |
416
|
|
|
*/ |
417
|
2 |
|
public function reduce($carry, \Closure $reducer) |
418
|
|
|
{ |
419
|
2 |
|
return array_reduce($this->values, $reducer, $carry); |
420
|
|
|
} |
421
|
|
|
} |
422
|
|
|
|