1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Innmind\Immutable; |
5
|
|
|
|
6
|
|
|
use Innmind\Immutable\Exception\{ |
7
|
|
|
LogicException, |
8
|
|
|
CannotGroupEmptyStructure, |
9
|
|
|
ElementNotFound, |
10
|
|
|
OutOfBoundException, |
11
|
|
|
}; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @template T |
15
|
|
|
*/ |
16
|
|
|
final class Sequence implements \Countable |
17
|
|
|
{ |
18
|
|
|
private string $type; |
19
|
|
|
private ValidateArgument $validate; |
20
|
|
|
private Sequence\Implementation $implementation; |
21
|
|
|
|
22
|
123 |
|
private function __construct(string $type, Sequence\Implementation $implementation) |
23
|
|
|
{ |
24
|
123 |
|
$this->type = $type; |
25
|
123 |
|
$this->validate = Type::of($type); |
26
|
123 |
|
$this->implementation = $implementation; |
27
|
123 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param T $values |
31
|
|
|
* |
32
|
|
|
* @return self<T> |
33
|
|
|
*/ |
34
|
114 |
|
public static function of(string $type, ...$values): self |
35
|
|
|
{ |
36
|
114 |
|
$self = new self($type, new Sequence\Primitive($type, ...$values)); |
|
|
|
|
37
|
114 |
|
$self->implementation->reduce( |
38
|
114 |
|
1, |
|
|
|
|
39
|
|
|
static function(int $position, $element) use ($self): int { |
40
|
30 |
|
($self->validate)($element, $position); |
41
|
|
|
|
42
|
30 |
|
return ++$position; |
43
|
114 |
|
} |
44
|
|
|
); |
45
|
|
|
|
46
|
114 |
|
return $self; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* It will load the values inside the generator only upon the first use |
51
|
|
|
* of the sequence |
52
|
|
|
* |
53
|
|
|
* Use this mode when the amount of data may not fit in memory |
54
|
|
|
* |
55
|
|
|
* @param \Generator<T> $generator |
56
|
|
|
* |
57
|
|
|
* @return self<T> |
58
|
|
|
*/ |
59
|
4 |
|
public static function defer(string $type, \Generator $generator): self |
60
|
|
|
{ |
61
|
4 |
|
return new self($type, new Sequence\Defer($type, $generator)); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param mixed $values |
66
|
|
|
* |
67
|
|
|
* @return self<mixed> |
68
|
|
|
*/ |
69
|
1 |
|
public static function mixed(...$values): self |
70
|
|
|
{ |
71
|
|
|
/** @var self<mixed> */ |
72
|
1 |
|
$self = new self('mixed', new Sequence\Primitive('mixed', ...$values)); |
73
|
|
|
|
74
|
1 |
|
return $self; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return self<int> |
79
|
|
|
*/ |
80
|
6 |
|
public static function ints(int ...$values): self |
81
|
|
|
{ |
82
|
|
|
/** @var self<int> */ |
83
|
6 |
|
$self = new self('int', new Sequence\Primitive('int', ...$values)); |
84
|
|
|
|
85
|
6 |
|
return $self; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return self<float> |
90
|
|
|
*/ |
91
|
1 |
|
public static function floats(float ...$values): self |
92
|
|
|
{ |
93
|
|
|
/** @var self<float> */ |
94
|
1 |
|
$self = new self('float', new Sequence\Primitive('float', ...$values)); |
95
|
|
|
|
96
|
1 |
|
return $self; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return self<string> |
101
|
|
|
*/ |
102
|
1 |
|
public static function strings(string ...$values): self |
103
|
|
|
{ |
104
|
|
|
/** @var self<string> */ |
105
|
1 |
|
$self = new self('string', new Sequence\Primitive('string', ...$values)); |
106
|
|
|
|
107
|
1 |
|
return $self; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return self<object> |
112
|
|
|
*/ |
113
|
1 |
|
public static function objects(object ...$values): self |
114
|
|
|
{ |
115
|
|
|
/** @var self<object> */ |
116
|
1 |
|
$self = new self('object', new Sequence\Primitive('object', ...$values)); |
117
|
|
|
|
118
|
1 |
|
return $self; |
119
|
|
|
} |
120
|
|
|
|
121
|
26 |
|
public function isOfType(string $type): bool |
122
|
|
|
{ |
123
|
26 |
|
return $this->type === $type; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Type of the elements |
128
|
|
|
*/ |
129
|
39 |
|
public function type(): string |
130
|
|
|
{ |
131
|
39 |
|
return $this->type; |
132
|
|
|
} |
133
|
|
|
|
134
|
1 |
|
public function size(): int |
135
|
|
|
{ |
136
|
1 |
|
return $this->implementation->size(); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* {@inheritdoc} |
141
|
|
|
*/ |
142
|
5 |
|
public function count(): int |
143
|
|
|
{ |
144
|
5 |
|
return $this->implementation->size(); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Return the element at the given index |
149
|
|
|
* |
150
|
|
|
* @throws OutOfBoundException |
151
|
|
|
* |
152
|
|
|
* @return T |
153
|
|
|
*/ |
154
|
9 |
|
public function get(int $index) |
155
|
|
|
{ |
156
|
|
|
/** @var T */ |
157
|
9 |
|
return $this->implementation->get($index); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Return the diff between this sequence and another |
162
|
|
|
* |
163
|
|
|
* @param self<T> $sequence |
164
|
|
|
* |
165
|
|
|
* @return self<T> |
166
|
|
|
*/ |
167
|
1 |
|
public function diff(self $sequence): self |
168
|
|
|
{ |
169
|
1 |
|
assertSequence($this->type, $sequence, 1); |
170
|
|
|
|
171
|
1 |
|
$self = clone $this; |
172
|
1 |
|
$self->implementation = $this->implementation->diff( |
173
|
1 |
|
$sequence->implementation |
174
|
|
|
); |
175
|
|
|
|
176
|
1 |
|
return $self; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Remove all duplicates from the sequence |
181
|
|
|
* |
182
|
|
|
* @return self<T> |
183
|
|
|
*/ |
184
|
1 |
|
public function distinct(): self |
185
|
|
|
{ |
186
|
1 |
|
$self = clone $this; |
187
|
1 |
|
$self->implementation = $this->implementation->distinct(); |
188
|
|
|
|
189
|
1 |
|
return $self; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Remove the n first elements |
194
|
|
|
* |
195
|
|
|
* @return self<T> |
196
|
|
|
*/ |
197
|
1 |
|
public function drop(int $size): self |
198
|
|
|
{ |
199
|
1 |
|
$self = clone $this; |
200
|
1 |
|
$self->implementation = $this->implementation->drop($size); |
201
|
|
|
|
202
|
1 |
|
return $self; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Remove the n last elements |
207
|
|
|
* |
208
|
|
|
* @return self<T> |
209
|
|
|
*/ |
210
|
1 |
|
public function dropEnd(int $size): self |
211
|
|
|
{ |
212
|
1 |
|
$self = clone $this; |
213
|
1 |
|
$self->implementation = $this->implementation->dropEnd($size); |
214
|
|
|
|
215
|
1 |
|
return $self; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Check if the two sequences are equal |
220
|
|
|
* |
221
|
|
|
* @param self<T> $sequence |
222
|
|
|
*/ |
223
|
7 |
|
public function equals(self $sequence): bool |
224
|
|
|
{ |
225
|
7 |
|
assertSequence($this->type, $sequence, 1); |
226
|
|
|
|
227
|
6 |
|
return $this->implementation->equals( |
228
|
6 |
|
$sequence->implementation |
229
|
|
|
); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Return all elements that satisfy the given predicate |
234
|
|
|
* |
235
|
|
|
* @param callable(T): bool $predicate |
236
|
|
|
* |
237
|
|
|
* @return self<T> |
238
|
|
|
*/ |
239
|
1 |
|
public function filter(callable $predicate): self |
240
|
|
|
{ |
241
|
1 |
|
$self = clone $this; |
242
|
1 |
|
$self->implementation = $this->implementation->filter($predicate); |
243
|
|
|
|
244
|
1 |
|
return $self; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Apply the given function to all elements of the sequence |
249
|
|
|
* |
250
|
|
|
* @param callable(T): void $function |
251
|
|
|
*/ |
252
|
1 |
|
public function foreach(callable $function): void |
253
|
|
|
{ |
254
|
1 |
|
$this->implementation->foreach($function); |
255
|
1 |
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Return a new map of pairs grouped by keys determined with the given |
259
|
|
|
* discriminator function |
260
|
|
|
* |
261
|
|
|
* @template D |
262
|
|
|
* @param callable(T): D $discriminator |
263
|
|
|
* |
264
|
|
|
* @throws CannotGroupEmptyStructure |
265
|
|
|
* |
266
|
|
|
* @return Map<D, self<T>> |
267
|
|
|
*/ |
268
|
2 |
|
public function groupBy(callable $discriminator): Map |
269
|
|
|
{ |
270
|
2 |
|
return $this->implementation->groupBy($discriminator); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Return the first element |
275
|
|
|
* |
276
|
|
|
* @return T |
277
|
|
|
*/ |
278
|
4 |
|
public function first() |
279
|
|
|
{ |
280
|
|
|
/** @var T */ |
281
|
4 |
|
return $this->implementation->first(); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Return the last element |
286
|
|
|
* |
287
|
|
|
* @return T |
288
|
|
|
*/ |
289
|
4 |
|
public function last() |
290
|
|
|
{ |
291
|
|
|
/** @var T */ |
292
|
4 |
|
return $this->implementation->last(); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Check if the sequence contains the given element |
297
|
|
|
* |
298
|
|
|
* @param T $element |
299
|
|
|
*/ |
300
|
1 |
|
public function contains($element): bool |
301
|
|
|
{ |
302
|
1 |
|
($this->validate)($element, 1); |
303
|
|
|
|
304
|
1 |
|
return $this->implementation->contains($element); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* Return the index for the given element |
309
|
|
|
* |
310
|
|
|
* @param T $element |
311
|
|
|
* |
312
|
|
|
* @throws ElementNotFound |
313
|
|
|
*/ |
314
|
1 |
|
public function indexOf($element): int |
315
|
|
|
{ |
316
|
1 |
|
($this->validate)($element, 1); |
317
|
|
|
|
318
|
1 |
|
return $this->implementation->indexOf($element); |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* Return the list of indices |
323
|
|
|
* |
324
|
|
|
* @return self<int> |
325
|
|
|
*/ |
326
|
2 |
|
public function indices(): self |
327
|
|
|
{ |
328
|
|
|
/** @var self<int> */ |
329
|
2 |
|
$self = new self('int', $this->implementation->indices()); |
330
|
|
|
|
331
|
2 |
|
return $self; |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* Return a new sequence by applying the given function to all elements |
336
|
|
|
* |
337
|
|
|
* @param callable(T): T $function |
338
|
|
|
* |
339
|
|
|
* @return self<T> |
340
|
|
|
*/ |
341
|
3 |
|
public function map(callable $function): self |
342
|
|
|
{ |
343
|
3 |
|
$self = clone $this; |
344
|
3 |
|
$self->implementation = $this->implementation->map($function); |
345
|
|
|
|
346
|
2 |
|
return $self; |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* Pad the sequence to a defined size with the given element |
351
|
|
|
* |
352
|
|
|
* @param T $element |
353
|
|
|
* |
354
|
|
|
* @return self<T> |
355
|
|
|
*/ |
356
|
2 |
|
public function pad(int $size, $element): self |
357
|
|
|
{ |
358
|
2 |
|
($this->validate)($element, 2); |
359
|
|
|
|
360
|
1 |
|
$self = clone $this; |
361
|
1 |
|
$self->implementation = $this->implementation->pad($size, $element); |
362
|
|
|
|
363
|
1 |
|
return $self; |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* Return a sequence of 2 sequences partitioned according to the given predicate |
368
|
|
|
* |
369
|
|
|
* @param callable(T): bool $predicate |
370
|
|
|
* |
371
|
|
|
* @return Map<bool, self<T>> |
372
|
|
|
*/ |
373
|
1 |
|
public function partition(callable $predicate): Map |
374
|
|
|
{ |
375
|
1 |
|
return $this->implementation->partition($predicate); |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
/** |
379
|
|
|
* Slice the sequence |
380
|
|
|
* |
381
|
|
|
* @return self<T> |
382
|
|
|
*/ |
383
|
1 |
|
public function slice(int $from, int $until): self |
384
|
|
|
{ |
385
|
1 |
|
$self = clone $this; |
386
|
1 |
|
$self->implementation = $this->implementation->slice($from, $until); |
387
|
|
|
|
388
|
1 |
|
return $self; |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
/** |
392
|
|
|
* Split the sequence in a sequence of 2 sequences splitted at the given position |
393
|
|
|
* |
394
|
|
|
* @throws OutOfBoundException |
395
|
|
|
* |
396
|
|
|
* @return self<self<T>> |
397
|
|
|
*/ |
398
|
1 |
|
public function splitAt(int $position): self |
399
|
|
|
{ |
400
|
1 |
|
return $this->implementation->splitAt($position); |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
/** |
404
|
|
|
* Return a sequence with the n first elements |
405
|
|
|
* |
406
|
|
|
* @return self<T> |
407
|
|
|
*/ |
408
|
1 |
|
public function take(int $size): self |
409
|
|
|
{ |
410
|
1 |
|
$self = clone $this; |
411
|
1 |
|
$self->implementation = $this->implementation->take($size); |
412
|
|
|
|
413
|
1 |
|
return $self; |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
/** |
417
|
|
|
* Return a sequence with the n last elements |
418
|
|
|
* |
419
|
|
|
* @return self<T> |
420
|
|
|
*/ |
421
|
1 |
|
public function takeEnd(int $size): self |
422
|
|
|
{ |
423
|
1 |
|
$self = clone $this; |
424
|
1 |
|
$self->implementation = $this->implementation->takeEnd($size); |
425
|
|
|
|
426
|
1 |
|
return $self; |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
/** |
430
|
|
|
* Append the given sequence to the current one |
431
|
|
|
* |
432
|
|
|
* @param self<T> $sequence |
433
|
|
|
* |
434
|
|
|
* @return self<T> |
435
|
|
|
*/ |
436
|
2 |
|
public function append(self $sequence): self |
437
|
|
|
{ |
438
|
2 |
|
assertSequence($this->type, $sequence, 1); |
439
|
|
|
|
440
|
1 |
|
$self = clone $this; |
441
|
1 |
|
$self->implementation = $this->implementation->append( |
442
|
1 |
|
$sequence->implementation |
443
|
|
|
); |
444
|
|
|
|
445
|
1 |
|
return $self; |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
/** |
449
|
|
|
* Return a sequence with all elements from the current one that exist |
450
|
|
|
* in the given one |
451
|
|
|
* |
452
|
|
|
* @param self<T> $sequence |
453
|
|
|
* |
454
|
|
|
* @return self<T> |
455
|
|
|
*/ |
456
|
2 |
|
public function intersect(self $sequence): self |
457
|
|
|
{ |
458
|
2 |
|
assertSequence($this->type, $sequence, 1); |
459
|
|
|
|
460
|
1 |
|
$self = clone $this; |
461
|
1 |
|
$self->implementation = $this->implementation->intersect( |
462
|
1 |
|
$sequence->implementation |
463
|
|
|
); |
464
|
|
|
|
465
|
1 |
|
return $self; |
466
|
|
|
} |
467
|
|
|
|
468
|
|
|
/** |
469
|
|
|
* Add the given element at the end of the sequence |
470
|
|
|
* |
471
|
|
|
* @param T $element |
472
|
|
|
* |
473
|
|
|
* @return self<T> |
474
|
|
|
*/ |
475
|
82 |
|
public function add($element): self |
476
|
|
|
{ |
477
|
82 |
|
($this->validate)($element, 1); |
478
|
|
|
|
479
|
81 |
|
$self = clone $this; |
480
|
81 |
|
$self->implementation = $this->implementation->add($element); |
481
|
|
|
|
482
|
81 |
|
return $self; |
483
|
|
|
} |
484
|
|
|
|
485
|
|
|
/** |
486
|
|
|
* Alias for add method in order to have a syntax similar to a true tuple |
487
|
|
|
* when constructing the sequence |
488
|
|
|
* |
489
|
|
|
* Example: |
490
|
|
|
* <code> |
491
|
|
|
* Sequence::of('int')(1)(3) |
492
|
|
|
* </code> |
493
|
|
|
* |
494
|
|
|
* @param T $element |
495
|
|
|
* |
496
|
|
|
* @return self<T> |
497
|
|
|
*/ |
498
|
51 |
|
public function __invoke($element): self |
499
|
|
|
{ |
500
|
51 |
|
return $this->add($element); |
501
|
|
|
} |
502
|
|
|
|
503
|
|
|
/** |
504
|
|
|
* Sort the sequence in a different order |
505
|
|
|
* |
506
|
|
|
* @param callable(T, T): int $function |
507
|
|
|
* |
508
|
|
|
* @return self<T> |
509
|
|
|
*/ |
510
|
1 |
|
public function sort(callable $function): self |
511
|
|
|
{ |
512
|
1 |
|
$self = clone $this; |
513
|
1 |
|
$self->implementation = $this->implementation->sort($function); |
514
|
|
|
|
515
|
1 |
|
return $self; |
516
|
|
|
} |
517
|
|
|
|
518
|
|
|
/** |
519
|
|
|
* Reduce the sequence to a single value |
520
|
|
|
* |
521
|
|
|
* @template R |
522
|
|
|
* @param R $carry |
523
|
|
|
* @param callable(R, T): R $reducer |
524
|
|
|
* |
525
|
|
|
* @return R |
526
|
|
|
*/ |
527
|
87 |
|
public function reduce($carry, callable $reducer) |
528
|
|
|
{ |
529
|
87 |
|
return $this->implementation->reduce($carry, $reducer); |
530
|
|
|
} |
531
|
|
|
|
532
|
|
|
/** |
533
|
|
|
* Return a set of the same type but without any value |
534
|
|
|
* |
535
|
|
|
* @return self<T> |
536
|
|
|
*/ |
537
|
1 |
|
public function clear(): self |
538
|
|
|
{ |
539
|
1 |
|
$self = clone $this; |
540
|
1 |
|
$self->implementation = new Sequence\Primitive($this->type); |
541
|
|
|
|
542
|
1 |
|
return $self; |
543
|
|
|
} |
544
|
|
|
|
545
|
|
|
/** |
546
|
|
|
* Return the same sequence but in reverse order |
547
|
|
|
* |
548
|
|
|
* @return self<T> |
549
|
|
|
*/ |
550
|
3 |
|
public function reverse(): self |
551
|
|
|
{ |
552
|
3 |
|
$self = clone $this; |
553
|
3 |
|
$self->implementation = $this->implementation->reverse(); |
554
|
|
|
|
555
|
3 |
|
return $self; |
556
|
|
|
} |
557
|
|
|
|
558
|
1 |
|
public function empty(): bool |
559
|
|
|
{ |
560
|
1 |
|
return $this->implementation->empty(); |
561
|
|
|
} |
562
|
|
|
|
563
|
|
|
/** |
564
|
|
|
* @template ST |
565
|
|
|
* |
566
|
|
|
* @param null|callable(T): \Generator<ST> $mapper |
567
|
|
|
* |
568
|
|
|
* @return self<ST> |
569
|
|
|
*/ |
570
|
8 |
|
public function toSequenceOf(string $type, callable $mapper = null): self |
571
|
|
|
{ |
572
|
8 |
|
return $this->implementation->toSequenceOf($type, $mapper); |
573
|
|
|
} |
574
|
|
|
|
575
|
|
|
/** |
576
|
|
|
* @template ST |
577
|
|
|
* |
578
|
|
|
* @param null|callable(T): \Generator<ST> $mapper |
579
|
|
|
* |
580
|
|
|
* @return Set<ST> |
581
|
|
|
*/ |
582
|
7 |
|
public function toSetOf(string $type, callable $mapper = null): Set |
583
|
|
|
{ |
584
|
7 |
|
return $this->implementation->toSetOf($type, $mapper); |
585
|
|
|
} |
586
|
|
|
|
587
|
|
|
|
588
|
|
|
|
589
|
|
|
/** |
590
|
|
|
* @template MT |
591
|
|
|
* @template MS |
592
|
|
|
* |
593
|
|
|
* @param callable(T): \Generator<MT, MS> $mapper |
594
|
|
|
* |
595
|
|
|
* @return Map<MT, MS> |
596
|
|
|
*/ |
597
|
1 |
|
public function toMapOf(string $key, string $value, callable $mapper): Map |
598
|
|
|
{ |
599
|
1 |
|
return $this->implementation->toMapOf($key, $value, $mapper); |
600
|
|
|
} |
601
|
|
|
} |
602
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths