Skip to content

Commit

Permalink
refactor: Refactor operations using their parent operation counterpart.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Sep 21, 2020
1 parent 6ce7b13 commit 0835f2d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/Operation/FoldLeft.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ static function ($initial = null) use ($callback): Closure {
*/
static function (Iterator $iterator) use ($callback, $initial): Generator {
/** @psalm-var Generator<TKey, T> $iterator */
$iterator = Last::of()(Reduction::of()($callback)($initial)($iterator));
$iterator = Last::of()(ScanLeft::of()($callback)($initial)($iterator));

/** @psalm-var Generator<int, TKey> $key */
$key = (Key::of()(0)($iterator));
Expand Down
15 changes: 7 additions & 8 deletions src/Operation/FoldLeft1.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,15 @@ static function (callable $callback): Closure {
* @psalm-return Generator<int|TKey, null|T>
*/
static function (Iterator $iterator) use ($callback): Generator {
$initial = $iterator->current();
/** @psalm-var Generator<TKey, T> $iterator */
$iterator = Last::of()(ScanLeft1::of()($callback)($iterator));

/** @psalm-var Iterator<TKey, T> $iterator */
$iterator = Drop::of()(1)($iterator);
/** @psalm-var Generator<int, TKey> $key */
$key = (Key::of()(0)($iterator));
/** @psalm-var Generator<int, T> $current */
$current = (Current::of()(0)($iterator));

if (false === $iterator->valid()) {
return yield $initial;
}

return yield from FoldLeft::of()($callback)($initial)($iterator);
return yield $key->current() => $current->current();
};
};
}
Expand Down
14 changes: 8 additions & 6 deletions src/Operation/FoldRight.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,15 @@ static function ($initial = null) use ($callback): Closure {
* @psalm-return Generator<TKey, T>
*/
static function (Iterator $iterator) use ($callback, $initial): Generator {
/** @psalm-var callable(Iterator<TKey, T>): Generator<TKey, T> $foldRight */
$foldRight = Compose::of()(
Reverse::of(),
FoldLeft::of()($callback)($initial)
);
/** @psalm-var Generator<TKey, T> $iterator */
$iterator = ScanRight::of()($callback)($initial)($iterator);

return yield from $foldRight($iterator);
/** @psalm-var Generator<int, TKey> $key */
$key = (Key::of()(0)($iterator));
/** @psalm-var Generator<int, T> $current */
$current = (Current::of()(0)($iterator));

return yield $key->current() => $current->current();
};
};
};
Expand Down
14 changes: 8 additions & 6 deletions src/Operation/FoldRight1.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ static function (callable $callback): Closure {
* @psalm-return Generator<TKey, T>
*/
static function (Iterator $iterator) use ($callback): Generator {
/** @psalm-var callable(Iterator<TKey, T>): Generator<TKey, T> $foldRight1 */
$foldRight1 = Compose::of()(
Reverse::of(),
FoldLeft1::of()($callback)
);
/** @psalm-var Generator<TKey, T> $iterator */
$iterator = ScanRight1::of()($callback)(Reverse::of()($iterator));

return yield from $foldRight1($iterator);
/** @psalm-var Generator<int, TKey> $key */
$key = (Key::of()(0)($iterator));
/** @psalm-var Generator<int, T> $current */
$current = (Current::of()(0)($iterator));

return yield $key->current() => $current->current();
};
};
}
Expand Down

0 comments on commit 0835f2d

Please sign in to comment.