Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/update Span and Partition in point-free #175

Merged
merged 5 commits into from
Aug 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Operation/DropWhile.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ final class DropWhile extends AbstractOperation
/**
* @pure
*
* @return Closure(callable(T, TKey): bool ...): Closure (Iterator<TKey, T>): Generator<TKey, T>
* @return Closure(callable(T, TKey, Iterator<TKey, T>): bool ...): Closure(Iterator<TKey, T>): Generator<TKey, T>
*/
public function __invoke(): Closure
{
return
/**
* @param callable(T, TKey):bool ...$callbacks
* @param callable(T, TKey, Iterator<TKey, T>):bool ...$callbacks
*
* @return Closure(Iterator<TKey, T>): Generator<TKey, T>
*/
Expand Down
38 changes: 23 additions & 15 deletions src/Operation/Partition.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,33 @@ final class Partition extends AbstractOperation
public function __invoke(): Closure
{
return
/**
* @param callable(T, TKey, Iterator<TKey, T>): bool ...$callbacks
*
* @return Closure(Iterator<TKey, T>): Generator<int, array{0: Closure(Iterator<TKey, T>): Generator<TKey, T>, 1: array{0: Iterator<TKey, T>}}>
AlexandruGG marked this conversation as resolved.
Show resolved Hide resolved
*/
static fn (callable ...$callbacks): Closure =>
(
/**
* @param array{0:(Closure(callable(T, TKey, Iterator<TKey, T>): bool ...): (Closure(Iterator<TKey, T>): Generator<TKey, T>)), 1:(Closure(callable(T, TKey, Iterator<TKey, T>): bool ...): (Closure(Iterator<TKey, T>): Generator<TKey, T>))} $operations
*
* @return Closure(callable(T, TKey, Iterator<TKey, T>): bool ...): Closure(Iterator<TKey, T>): Generator<int, array{0: Closure(Iterator<TKey, T>): Generator<TKey, T>, 1: array{0: Iterator<TKey, T>}}>
*/
static fn (array $operations): Closure =>
/**
* @param callable(T, TKey, Iterator<TKey, T>): bool ...$callbacks
*
* @return Closure(Iterator<TKey, T>): Generator<int, array{0: Closure(Iterator<TKey, T>): Generator<TKey, T>, 1: array{0: Iterator<TKey, T>}}>
*/
static fn (callable ...$callbacks): Closure =>
/**
* @param Iterator<TKey, T> $iterator
*
* @return Generator<int, array{0: Closure(Iterator<TKey, T>): Generator<TKey, T>, 1: array{0: Iterator<TKey, T>}}>
*/
static function (Iterator $iterator) use ($callbacks): Generator {
/** @var array{0: Closure(Iterator<TKey, T>): Generator<TKey, T>, 1: array{0: Iterator<TKey, T>}} $filter */
$filter = [Filter::of()(...$callbacks), [$iterator]];

/** @var array{0: Closure(Iterator<TKey, T>): Generator<TKey, T>, 1: array{0: Iterator<TKey, T>}} $reject */
$reject = [Reject::of()(...$callbacks), [$iterator]];

return yield from [$filter, $reject];
};
static fn (Iterator $iterator): Generator => yield from array_map(
/**
* @param Closure(callable(T, TKey, Iterator<TKey, T>): bool ...): (Closure(Iterator<TKey, T>): Generator<TKey, T>) $callable
*
* @return array{0: (Closure(Iterator<TKey, T>): Generator<TKey, T>), 1: (array{0: Iterator<TKey, T>})}
*/
static fn (callable $callable): array => [$callable(...$callbacks), [$iterator]],
$operations
)
)([(new Filter())(), (new Reject())()]);
}
}
38 changes: 23 additions & 15 deletions src/Operation/Span.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,33 @@ final class Span extends AbstractOperation
public function __invoke(): Closure
{
return
/**
* @param callable(T, TKey, Iterator<TKey, T>): bool ...$callbacks
*
* @return Closure(Iterator<TKey, T>): Generator<int, array{0: Closure(Iterator<TKey, T>): Generator<TKey, T>, 1: array{0: Iterator<TKey, T>}}>
AlexandruGG marked this conversation as resolved.
Show resolved Hide resolved
*/
static fn (callable ...$callbacks): Closure =>
(
/**
* @param array{0:(Closure(callable(T, TKey, Iterator<TKey, T>): bool ...): (Closure(Iterator<TKey, T>): Generator<TKey, T>)), 1:(Closure(callable(T, TKey, Iterator<TKey, T>): bool ...): (Closure(Iterator<TKey, T>): Generator<TKey, T>))} $operations
*
* @return Closure(callable(T, TKey, Iterator<TKey, T>): bool ...): Closure(Iterator<TKey, T>): Generator<int, array{0: Closure(Iterator<TKey, T>): Generator<TKey, T>, 1: array{0: Iterator<TKey, T>}}>
*/
static fn (array $operations): Closure =>
/**
* @param callable(T, TKey, Iterator<TKey, T>): bool ...$callbacks
*
* @return Closure(Iterator<TKey, T>): Generator<int, array{0: Closure(Iterator<TKey, T>): Generator<TKey, T>, 1: array{0: Iterator<TKey, T>}}>
*/
static fn (callable ...$callbacks): Closure =>
/**
* @param Iterator<TKey, T> $iterator
*
* @return Generator<int, array{0: Closure(Iterator<TKey, T>): Generator<TKey, T>, 1: array{0: Iterator<TKey, T>}}>
*/
static function (Iterator $iterator) use ($callbacks): Generator {
/** @var array{0: Closure(Iterator<TKey, T>): Generator<TKey, T>, 1: array{0: Iterator<TKey, T>}} $takeWhile */
$takeWhile = [TakeWhile::of()(...$callbacks), [$iterator]];

/** @var array{0: Closure(Iterator<TKey, T>): Generator<TKey, T>, 1: array{0: Iterator<TKey, T>}} $dropWhile */
$dropWhile = [DropWhile::of()(...$callbacks), [$iterator]];

return yield from [$takeWhile, $dropWhile];
};
static fn (Iterator $iterator): Generator => yield from array_map(
/**
* @param Closure(callable(T, TKey, Iterator<TKey, T>): bool ...): (Closure(Iterator<TKey, T>): Generator<TKey, T>) $callable
*
* @return array{0: (Closure(Iterator<TKey, T>): Generator<TKey, T>), 1: (array{0: Iterator<TKey, T>})}
*/
static fn (callable $callable): array => [$callable(...$callbacks), [$iterator]],
$operations
)
)([(new TakeWhile())(), (new DropWhile())()]);
}
}
2 changes: 1 addition & 1 deletion src/Operation/TakeWhile.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ final class TakeWhile extends AbstractOperation
/**
* @pure
*
* @return Closure(callable(T, TKey, Iterator<TKey, T>): bool):Closure (Iterator<TKey, T>): Generator<TKey, T>
* @return Closure(callable(T, TKey, Iterator<TKey, T>): bool ...): Closure(Iterator<TKey, T>): Generator<TKey, T>
*/
public function __invoke(): Closure
{
Expand Down