Skip to content

Commit ee975c5

Browse files
committed
Merge branch 'develop'
* develop: specify next release fix psalm errors add Sequence::chunk()
2 parents 22dce2b + 99aa7f8 commit ee975c5

File tree

4 files changed

+85
-1
lines changed

4 files changed

+85
-1
lines changed

CHANGELOG.md

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

3+
## 5.9.0 - 2024-07-05
4+
5+
### Added
6+
7+
- `Innmind\Immutable\Sequence::chunk()`
8+
39
## 5.8.0 - 2024-06-27
410

511
### Added

docs/structures/sequence.md

+24
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,30 @@ $lines; // ['foo', 'bar', 'baz', '']
271271
!!! note ""
272272
The `flatMap` is here in case there is only one chunk in the sequence, in which case the `aggregate` is not called
273273

274+
### `->chunk()`
275+
276+
This is a shortcut over [`aggregate`](#-aggregate). The same example can be shortened:
277+
278+
```php
279+
// let's pretend this comes from a stream
280+
$chunks = ['fo', "o\n", 'ba', "r\n", 'ba', "z\n"];
281+
$lines = Sequence::of(...$chunks)
282+
->map(Str::of(...))
283+
->map(
284+
static fn($chunk) => $chunk
285+
->toEncoding(Str\Encoding::ascii)
286+
->split(),
287+
)
288+
->chunk(4)
289+
->map(static fn($chars) => $chars->dropEnd(1)) // to remove "\n"
290+
->map(Str::of('')->join(...))
291+
->map(static fn($line) => $line->toString())
292+
->toList();
293+
$lines; // ['foo', 'bar', 'baz', '']
294+
```
295+
296+
This better accomodates to the case where the initial `Sequence` only contains a single value.
297+
274298
### `->indices()`
275299

276300
Create a new sequence of integers representing the indices of the original sequence.

proofs/sequence.php

+40-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
<?php
22
declare(strict_types = 1);
33

4-
use Innmind\Immutable\Sequence;
4+
use Innmind\Immutable\{
5+
Sequence,
6+
Str,
7+
Monoid\Concat,
8+
};
59
use Innmind\BlackBox\Set;
610

711
return static function() {
@@ -58,4 +62,39 @@ static function($assert, $first, $second) {
5862
);
5963
},
6064
);
65+
66+
yield proof(
67+
'Sequence::chunk()',
68+
given(
69+
Set\Strings::atLeast(100),
70+
Set\Integers::between(1, 50),
71+
),
72+
static function($assert, $string, $chunk) {
73+
$chunks = Str::of($string, Str\Encoding::ascii)
74+
->split()
75+
->chunk($chunk);
76+
77+
$chunks->foreach(
78+
static fn($chars) => $assert
79+
->number($chars->size())
80+
->lessThanOrEqual($chunk),
81+
);
82+
$chunks
83+
->dropEnd(1)
84+
->foreach(
85+
static fn($chars) => $assert->same(
86+
$chunk,
87+
$chars->size(),
88+
),
89+
);
90+
91+
$assert->same(
92+
$string,
93+
$chunks
94+
->flatMap(static fn($chars) => $chars)
95+
->fold(new Concat)
96+
->toString(),
97+
);
98+
},
99+
);
61100
};

src/Sequence.php

+15
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,21 @@ public function aggregate(callable $map): self
743743
return new self($this->implementation->aggregate($map, $exfiltrate));
744744
}
745745

746+
/**
747+
* @param positive-int $size
748+
*
749+
* @return self<self<T>>
750+
*/
751+
public function chunk(int $size): self
752+
{
753+
return $this
754+
->map(static fn($value) => self::of($value))
755+
->aggregate(static fn(Sequence $a, $b) => match ($a->size()) {
756+
$size => self::of($a, $b),
757+
default => self::of($a->append($b)),
758+
});
759+
}
760+
746761
/**
747762
* Force to load all values into memory (only useful for deferred and lazy Sequence)
748763
*

0 commit comments

Comments
 (0)