loophp /
collection
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace loophp\collection\Operation; |
||
| 6 | |||
| 7 | use Closure; |
||
| 8 | use Generator; |
||
| 9 | |||
| 10 | /** |
||
| 11 | * @immutable |
||
| 12 | * |
||
| 13 | * @template TKey |
||
| 14 | * @template T |
||
| 15 | */ |
||
| 16 | final class Unzip extends AbstractOperation |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @return Closure(iterable<TKey, list<T>>): Generator<int, list<T>> |
||
| 20 | */ |
||
| 21 | 2 | public function __invoke(): Closure |
|
| 22 | { |
||
| 23 | 2 | $reduceCallback = |
|
| 24 | /** |
||
| 25 | * @param array<int, list<T>> $carry |
||
|
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||
| 26 | * @param iterable<TKey, T> $value |
||
| 27 | * |
||
| 28 | * @return array<int, list<T>> |
||
|
0 ignored issues
–
show
|
|||
| 29 | */ |
||
| 30 | 2 | static function (array $carry, iterable $value): array { |
|
| 31 | 2 | $index = 0; |
|
|
0 ignored issues
–
show
|
|||
| 32 | |||
| 33 | 2 | foreach ($value as $carry[$index++][]); |
|
| 34 | |||
| 35 | 2 | return $carry; |
|
| 36 | 2 | }; |
|
| 37 | |||
| 38 | /** @var Closure(iterable<TKey, list<T>>): Generator<int, list<T>> $pipe */ |
||
| 39 | 2 | $pipe = (new Pipe())()( |
|
| 40 | 2 | (new Reduce())()($reduceCallback)([]), |
|
| 41 | 2 | (new Flatten())()(1) |
|
| 42 | 2 | ); |
|
| 43 | |||
| 44 | // Point free style. |
||
| 45 | 2 | return $pipe; |
|
| 46 | } |
||
| 47 | } |
||
| 48 |