Skip to content

Commit

Permalink
Add flatten(), a readable shortcut for unpack() (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
sanmai committed Apr 12, 2023
1 parent 69c2ba3 commit 9260c4d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 13 deletions.
25 changes: 15 additions & 10 deletions README.md
Expand Up @@ -113,7 +113,8 @@ All entry points always return an instance of the pipeline.
| `prepend()` | Appends the contents of an interable to the end of the pipeline. | `array_merge` |
| `unshift()` | Prepends the pipeline with a list of values. | `array_unshift` |
| `zip()` | Takes a number of iterables, transposing them together with the current sequence, if any. | `array_map(null, ...$array)`, Python's `zip()`, transposition |
| `unpack()` | Unpacks arrays into arguments for a callback. Flattens inputs if no callback provided. | `flat_map`, `flatten` |
| `flatten()` | Flattens inputs: `[[1, 2], [3, 4]]` becomes `[1, 2, 3, 4]`. | `flat_map`, `flatten`, `collect_concat` |
| `unpack()` | Unpacks arrays into arguments for a callback. Flattens inputs if no callback provided. | |
| `chunk()` | Chunks the pipeline into arrays of specified length. | `array_chunk` |
| `filter()` | Removes elements unless a callback returns true. Removes falsey values if no callback provided. | `array_filter`, `Where` |
| `slice()` | Extracts a slice from the inputs. Keys are not discarded intentionally. Suppors negative values for both arguments. | `array_slice` |
Expand Down Expand Up @@ -242,6 +243,18 @@ $pipeline->map(function () {
});
```

## `$pipeline->flatten()`

Flatten inputs:

```php
$pipeline->map(function () {
yield [1];
yield [2, 3];
})->unpack()->toArray();
// [1, 2, 3]
```

## `$pipeline->unpack()`

An extra variant of `map` which unpacks arrays into arguments for a callback.
Expand Down Expand Up @@ -269,15 +282,7 @@ $pipeline->unpack(function ($a, array $b, \DateTime ...$dates) {

You can have all kinds of standard type checks with ease too.

With no callback, the default callback for `unpack()` will flatten inputs:

```php
$pipeline->map(function () {
yield [1];
yield [2, 3];
})->unpack()->toArray();
// [1, 2, 3]
```
With no callback, the default callback for `unpack()` will flatten inputs as in `flatten()`.

## `$pipeline->cast()`

Expand Down
18 changes: 15 additions & 3 deletions src/Standard.php
Expand Up @@ -195,6 +195,18 @@ private static function joinYield(iterable $left, iterable $right): iterable
yield from $right;
}

/**
* Flattens inputs: arrays become lists.
*
* @return $this
*/
public function flatten(): self
{
return $this->map(static function (iterable $args = []) {
yield from $args;
});
}

/**
* An extra variant of `map` which unpacks arrays into arguments. Flattens inputs if no callback provided.
*
Expand All @@ -204,9 +216,9 @@ private static function joinYield(iterable $left, iterable $right): iterable
*/
public function unpack(?callable $func = null): self
{
$func = $func ?? static function (...$args) {
yield from $args;
};
if (null === $func) {
return $this->flatten();
}

return $this->map(static function (iterable $args = []) use ($func) {
/** @psalm-suppress InvalidArgument */
Expand Down
12 changes: 12 additions & 0 deletions tests/UnpackTest.php
Expand Up @@ -81,4 +81,16 @@ public function testWithIterator(): void
[3],
])->unpack()->toArray());
}

/**
* @covers \Pipeline\Standard::flatten()
*/
public function testFlatten(): void
{
$this->assertSame([1, 2, 3], fromArray([
new ArrayIterator([1]),
fromArray([2]),
[3],
])->flatten()->toArray());
}
}

0 comments on commit 9260c4d

Please sign in to comment.