Skip to content

Commit

Permalink
refactor: Simplify operations.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Dec 30, 2020
1 parent c8d66c1 commit e1d91ec
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 22 deletions.
6 changes: 3 additions & 3 deletions src/Operation/Drop.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ public function __invoke(): Closure
*
* @psalm-return Generator<TKey, T>
*/
static function (Iterator $iterator) use ($offsets): Generator {
if (!$iterator->valid()) {
static function (Iterator $iterator) use ($offsets): Iterator {
if (false === $iterator->valid()) {
return new EmptyIterator();
}

return yield from new LimitIterator($iterator, array_sum($offsets));
return new LimitIterator($iterator, array_sum($offsets));
};
}
}
27 changes: 8 additions & 19 deletions src/Operation/ScanLeft.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,14 @@ public function __invoke(): Closure
*
* @psalm-return Closure(Iterator<TKey, T>): Generator<int|TKey, T|null>
*/
static fn ($initial = null): Closure =>
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Generator<int|TKey, T|null>
*/
static function (Iterator $iterator) use ($callback, $initial): Generator {
// @todo: See if we cannot find a better way to do this.
if (false === $iterator->valid()) {
return yield from [];
}
static function ($initial = null) use ($callback): Closure {
/** @psalm-var Closure(Iterator<TKey, T>): Generator<int|TKey, T|null> $pipe */
$pipe = Pipe::of()(
Reduction::of()($callback)($initial),
Prepend::of()($initial)
);

/** @psalm-var Closure(Iterator<TKey, T>): Generator<int|TKey, T|null> $pipe */
$pipe = Pipe::of()(
Reduction::of()($callback)($initial),
Prepend::of()($initial)
);

return yield from $pipe($iterator);
};
return $pipe;
};
}
}

0 comments on commit e1d91ec

Please sign in to comment.