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