Skip to content

Commit 98c07fe

Browse files
committed
Merge branch 'develop'
* develop: CS add Sequence::lazyStartingWith() named constructor
2 parents 8f88454 + a6620c6 commit 98c07fe

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

CHANGELOG.md

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

3+
## 4.7.0 - 2022-11-26
4+
5+
## Added
6+
7+
- `Innmind\Immutable\Sequence::lazyStartingWith()`
8+
39
## 4.6.0 - 2022-10-08
410

511
## Added

docs/SEQUENCE.md

+10
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ $sequence = Sequence::lazy(function() {
4141

4242
**Important**: since the elements are reloaded each time the immutability responsability is up to you because the source may change or if you generate objects it will generate new objects each time (so if you make strict comparison it will fail).
4343

44+
## `::lazyStartingWith()`
45+
46+
Same as `::lazy()` except you don't need to manually build the generator.
47+
48+
```php
49+
$sequence = Sequence::lazyStartingWith(1, 2, 3);
50+
```
51+
52+
**Note**: this is useful when you know the first items of the sequence and you'll `append` another lazy sequence at the end.
53+
4454
## `::mixed()`
4555

4656
This is a shortcut for `::of(mixed ...$mixed)`.

src/Sequence.php

+18
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,24 @@ public static function lazy(callable $generator): self
9090
return new self(new Sequence\Lazy($generator));
9191
}
9292

93+
/**
94+
* Same as self::lazy() except you don't have to create the generator manually
95+
*
96+
* @template V
97+
* @no-named-arguments
98+
* @psalm-pure
99+
*
100+
* @param V $values
101+
*/
102+
public static function lazyStartingWith(...$values): self
103+
{
104+
return self::lazy(static function() use ($values) {
105+
foreach ($values as $value) {
106+
yield $value;
107+
}
108+
});
109+
}
110+
93111
/**
94112
* @no-named-arguments
95113
* @psalm-pure

tests/SequenceTest.php

+16
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,22 @@ public function testLazy()
6767
$this->assertTrue($loaded);
6868
}
6969

70+
public function testLazyStartingWith()
71+
{
72+
$loaded = false;
73+
$sequence = Sequence::lazyStartingWith(1, 2, 3)->append(
74+
Sequence::lazy(static function() use (&$loaded) {
75+
yield 4;
76+
$loaded = true;
77+
}),
78+
);
79+
80+
$this->assertInstanceOf(Sequence::class, $sequence);
81+
$this->assertFalse($loaded);
82+
$this->assertSame([1, 2, 3, 4], $sequence->toList());
83+
$this->assertTrue($loaded);
84+
}
85+
7086
public function testMixed()
7187
{
7288
$sequence = Sequence::mixed(1, '2', 3);

0 commit comments

Comments
 (0)