From efc63b524c2578b2e693417747ea89913a0ef09d Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Fri, 5 Jul 2024 17:54:21 +0200 Subject: [PATCH 1/3] add Sequence::chunk() --- CHANGELOG.md | 6 ++++++ docs/structures/sequence.md | 24 ++++++++++++++++++++++ proofs/sequence.php | 41 ++++++++++++++++++++++++++++++++++++- src/Sequence.php | 15 ++++++++++++++ 4 files changed, 85 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ceccc18..3c43fa8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## [Unreleased] + +### Added + +- `Innmind\Immutable\Sequence::chunk()` + ## 5.8.0 - 2024-06-27 ### Added diff --git a/docs/structures/sequence.md b/docs/structures/sequence.md index daac35c..cfb799e 100644 --- a/docs/structures/sequence.md +++ b/docs/structures/sequence.md @@ -271,6 +271,30 @@ $lines; // ['foo', 'bar', 'baz', ''] !!! note "" The `flatMap` is here in case there is only one chunk in the sequence, in which case the `aggregate` is not called +### `->chunk()` + +This is a shortcut over [`aggregate`](#-aggregate). The same example can be shortened: + +```php +// let's pretend this comes from a stream +$chunks = ['fo', "o\n", 'ba', "r\n", 'ba', "z\n"]; +$lines = Sequence::of(...$chunks) + ->map(Str::of(...)) + ->map( + static fn($chunk) => $chunk + ->toEncoding(Str\Encoding::ascii) + ->split(), + ) + ->chunk(4) + ->map(static fn($chars) => $chars->dropEnd(1)) // to remove "\n" + ->map(Str::of('')->join(...)) + ->map(static fn($line) => $line->toString()) + ->toList(); +$lines; // ['foo', 'bar', 'baz', ''] +``` + +This better accomodates to the case where the initial `Sequence` only contains a single value. + ### `->indices()` Create a new sequence of integers representing the indices of the original sequence. diff --git a/proofs/sequence.php b/proofs/sequence.php index 3e6aab1..606515f 100644 --- a/proofs/sequence.php +++ b/proofs/sequence.php @@ -1,7 +1,11 @@ split() + ->chunk($chunk); + + $chunks->foreach( + static fn($chars) => $assert + ->number($chars->size()) + ->lessThanOrEqual($chunk), + ); + $chunks + ->dropEnd(1) + ->foreach( + static fn($chars) => $assert->same( + $chunk, + $chars->size(), + ), + ); + + $assert->same( + $string, + $chunks + ->flatMap(static fn($chars) => $chars) + ->fold(new Concat) + ->toString(), + ); + }, + ); }; diff --git a/src/Sequence.php b/src/Sequence.php index e5c92b1..af4dced 100644 --- a/src/Sequence.php +++ b/src/Sequence.php @@ -743,6 +743,21 @@ public function aggregate(callable $map): self return new self($this->implementation->aggregate($map, $exfiltrate)); } + /** + * @param positive-int $size + * + * @return self> + */ + public function chunk(int $size): self + { + return $this + ->map(self::of(...)) + ->aggregate(static fn($a, $b) => match ($a->size()) { + $size => self::of($a, $b), + default => self::of($a->append($b)), + }); + } + /** * Force to load all values into memory (only useful for deferred and lazy Sequence) * From d4d52545ab8cf8b50c14afc0a4b0c1958eab6a2f Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Fri, 5 Jul 2024 18:00:02 +0200 Subject: [PATCH 2/3] fix psalm errors --- src/Sequence.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Sequence.php b/src/Sequence.php index af4dced..61f244d 100644 --- a/src/Sequence.php +++ b/src/Sequence.php @@ -751,8 +751,8 @@ public function aggregate(callable $map): self public function chunk(int $size): self { return $this - ->map(self::of(...)) - ->aggregate(static fn($a, $b) => match ($a->size()) { + ->map(static fn($value) => self::of($value)) + ->aggregate(static fn(Sequence $a, $b) => match ($a->size()) { $size => self::of($a, $b), default => self::of($a->append($b)), }); From 99aa7f88c0e9313b49eb1597f7403120f624a71e Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Fri, 5 Jul 2024 18:04:58 +0200 Subject: [PATCH 3/3] specify next release --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3c43fa8..9bdaac1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # Changelog -## [Unreleased] +## 5.9.0 - 2024-07-05 ### Added