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