Skip to content

Commit

Permalink
fix: tails operation improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Dec 28, 2022
1 parent 5e46af0 commit 58c840b
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions src/Operation/Tails.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,25 @@ public function __invoke(): Closure
* @return Generator<int, list<T>>
*/
static function (iterable $iterable): Generator {
$generator = iterator_to_array((new IterableIteratorAggregate($iterable))->getIterator());
// We could use a value such as `false` or `0`, but it would
// be too complex to deal with S.A. annotations.
array_unshift($generator, current($generator));
$generator = iterator_to_array(
(new IterableIteratorAggregate($iterable))->getIterator()
);

yield from new NormalizeIterableAggregate(new LimitIterableAggregate(new ReductionIterableAggregate(
$generator,
$reduction =
/**
* @param list<T> $stack
*
* @return list<T>
*/
static fn (array $stack): array => array_slice($stack, 1),
$generator
), 1));
* @param list<T> $stack
*
* @return list<T>
*/
static fn (array $stack): array => array_slice($stack, 1);

/** @var Closure(iterable<TKey, T>): Generator<int, list<T>> $pipe */
$pipe = (new Pipe)()(
(new Reduction)()($reduction)($generator),
(new Normalize)(),
);

yield from $pipe($iterable);
};
}
}

0 comments on commit 58c840b

Please sign in to comment.