Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
* develop:
  specify next release
  fix psalm errors
  add Sequence::chunk()
  • Loading branch information
Baptouuuu committed Jul 5, 2024
2 parents 22dce2b + 99aa7f8 commit ee975c5
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 5.9.0 - 2024-07-05

### Added

- `Innmind\Immutable\Sequence::chunk()`

## 5.8.0 - 2024-06-27

### Added
Expand Down
24 changes: 24 additions & 0 deletions docs/structures/sequence.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
41 changes: 40 additions & 1 deletion proofs/sequence.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<?php
declare(strict_types = 1);

use Innmind\Immutable\Sequence;
use Innmind\Immutable\{
Sequence,
Str,
Monoid\Concat,
};
use Innmind\BlackBox\Set;

return static function() {
Expand Down Expand Up @@ -58,4 +62,39 @@ static function($assert, $first, $second) {
);
},
);

yield proof(
'Sequence::chunk()',
given(
Set\Strings::atLeast(100),
Set\Integers::between(1, 50),
),
static function($assert, $string, $chunk) {
$chunks = Str::of($string, Str\Encoding::ascii)
->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(),
);
},
);
};
15 changes: 15 additions & 0 deletions src/Sequence.php
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,21 @@ public function aggregate(callable $map): self
return new self($this->implementation->aggregate($map, $exfiltrate));
}

/**
* @param positive-int $size
*
* @return self<self<T>>
*/
public function chunk(int $size): self
{
return $this
->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)),
});
}

/**
* Force to load all values into memory (only useful for deferred and lazy Sequence)
*
Expand Down

0 comments on commit ee975c5

Please sign in to comment.