Skip to content

Commit d861ab3

Browse files
committed
Merge branch 'develop'
* develop: specify next release add Sequence::prepend()
2 parents 2ec917c + 46e5733 commit d861ab3

File tree

8 files changed

+141
-0
lines changed

8 files changed

+141
-0
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## 5.7.0 - 2024-06-25
4+
5+
### Added
6+
7+
- `Innmind\Immutable\Sequence::prepend()`
8+
39
## 5.6.0 - 2024-06-15
410

511
### Added

docs/structures/sequence.md

+9
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,15 @@ $sequence = Sequence::ints(1, 2)->append(Sequence::ints(3, 4));
104104
$sequence->equals(Sequence::ints(1, 2, 3, 4)); // true
105105
```
106106

107+
### `->prepend()`
108+
109+
This is similar to `->append()` except the order is switched.
110+
111+
!!! success ""
112+
The main advantage of this method is when using lazy sequences. If you want to add elements at the beginning of a sequence but the rest may be lazy then you need to create a lazy sequence with your values and then append the other lazy sequence; but this reveals the underlying lazyness of the call and you need to be aware that it could be lazy.
113+
114+
Instead by using this method you no longer have to be aware that the other sequence is lazy or not.
115+
107116
## Access values
108117

109118
### `->size()` :material-memory-arrow-down:

proofs/sequence.php

+39
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
declare(strict_types = 1);
33

44
use Innmind\Immutable\Sequence;
5+
use Innmind\BlackBox\Set;
56

67
return static function() {
78
yield test(
@@ -15,4 +16,42 @@ static function($assert) {
1516
);
1617
},
1718
);
19+
20+
yield proof(
21+
'Sequence::prepend()',
22+
given(
23+
Set\Sequence::of(Set\Type::any()),
24+
Set\Sequence::of(Set\Type::any()),
25+
),
26+
static function($assert, $first, $second) {
27+
$assert->same(
28+
[...$first, ...$second],
29+
Sequence::of(...$second)
30+
->prepend(Sequence::of(...$first))
31+
->toList(),
32+
);
33+
34+
$assert->same(
35+
[...$first, ...$second],
36+
Sequence::defer((static function() use ($second) {
37+
yield from $second;
38+
})())
39+
->prepend(Sequence::defer((static function() use ($first) {
40+
yield from $first;
41+
})()))
42+
->toList(),
43+
);
44+
45+
$assert->same(
46+
[...$first, ...$second],
47+
Sequence::lazy(static function() use ($second) {
48+
yield from $second;
49+
})
50+
->prepend(Sequence::lazy(static function() use ($first) {
51+
yield from $first;
52+
}))
53+
->toList(),
54+
);
55+
},
56+
);
1857
};

src/Sequence.php

+14
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,20 @@ public function append(self $sequence): self
489489
));
490490
}
491491

492+
/**
493+
* Prepend the given sequence to the current one
494+
*
495+
* @param self<T> $sequence
496+
*
497+
* @return self<T>
498+
*/
499+
public function prepend(self $sequence): self
500+
{
501+
return new self($this->implementation->prepend(
502+
$sequence->implementation,
503+
));
504+
}
505+
492506
/**
493507
* Return a sequence with all elements from the current one that exist
494508
* in the given one

src/Sequence/Defer.php

+23
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,29 @@ public function append(Implementation $sequence): Implementation
483483
);
484484
}
485485

486+
/**
487+
* @param Implementation<T> $sequence
488+
*
489+
* @return Implementation<T>
490+
*/
491+
public function prepend(Implementation $sequence): Implementation
492+
{
493+
/** @psalm-suppress ImpureFunctionCall */
494+
return new self(
495+
(static function(\Iterator $values, Implementation $sequence): \Generator {
496+
/** @var T $value */
497+
foreach ($sequence->iterator() as $value) {
498+
yield $value;
499+
}
500+
501+
/** @var T $value */
502+
foreach ($values as $value) {
503+
yield $value;
504+
}
505+
})($this->values, $sequence),
506+
);
507+
}
508+
486509
/**
487510
* @param Implementation<T> $sequence
488511
*

src/Sequence/Implementation.php

+9
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,15 @@ public function takeEnd(int $size): self;
228228
*/
229229
public function append(self $sequence): self;
230230

231+
/**
232+
* Prepend the given sequence to the current one
233+
*
234+
* @param self<T> $sequence
235+
*
236+
* @return self<T>
237+
*/
238+
public function prepend(self $sequence): self;
239+
231240
/**
232241
* Return a sequence with all elements from the current one that exist
233242
* in the given one

src/Sequence/Lazy.php

+25
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,31 @@ static function(RegisterCleanup $registerCleanup) use ($values, $sequence): \Gen
517517
);
518518
}
519519

520+
/**
521+
* @param Implementation<T> $sequence
522+
*
523+
* @return Implementation<T>
524+
*/
525+
public function prepend(Implementation $sequence): Implementation
526+
{
527+
$values = $this->values;
528+
529+
return new self(
530+
static function(RegisterCleanup $registerCleanup) use ($values, $sequence): \Generator {
531+
/** @var \Iterator<int, T> */
532+
$generator = self::open($sequence, $registerCleanup);
533+
534+
foreach ($generator as $value) {
535+
yield $value;
536+
}
537+
538+
foreach ($values($registerCleanup) as $value) {
539+
yield $value;
540+
}
541+
},
542+
);
543+
}
544+
520545
/**
521546
* @param Implementation<T> $sequence
522547
*

src/Sequence/Primitive.php

+16
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,22 @@ public function append(Implementation $sequence): self
363363
return new self(\array_merge($this->values, $other));
364364
}
365365

366+
/**
367+
* @param Implementation<T> $sequence
368+
*
369+
* @return self<T>
370+
*/
371+
public function prepend(Implementation $sequence): self
372+
{
373+
$other = [];
374+
375+
foreach ($sequence->iterator() as $value) {
376+
$other[] = $value;
377+
}
378+
379+
return new self(\array_merge($other, $this->values));
380+
}
381+
366382
/**
367383
* @param Implementation<T> $sequence
368384
*

0 commit comments

Comments
 (0)