Skip to content

Commit

Permalink
Simplify every operation (#247)
Browse files Browse the repository at this point in the history
* feat: Simplify `Every` operation.

* refactor: Refactor operations depending on `Every`.
  • Loading branch information
drupol authored Mar 27, 2022
1 parent 8f8ae04 commit 5f9c32c
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 40 deletions.
2 changes: 1 addition & 1 deletion src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ public function equals(iterable $other): bool

public function every(callable ...$callbacks): bool
{
return (new Every())()(static fn (int $index, $value, $key, iterable $iterable) => CallbacksArrayReducer::or()($callbacks)($value, $key, $iterable))(static fn (bool $r): bool => $r)($this)
return (new Every())()(static fn (int $index, $value, $key, iterable $iterable) => CallbacksArrayReducer::or()($callbacks)($value, $key, $iterable))($this)
->current();
}

Expand Down
12 changes: 4 additions & 8 deletions src/Operation/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,15 @@ public function __invoke(): Closure
static function ($column): Closure {
$filterCallbackBuilder =
/**
* @param mixed $column
* @param T $value
* @param TKey $key
*/
static fn ($column): Closure =>
/**
* @param T $value
* @param TKey $key
*/
static fn ($value, $key): bool => $key === $column;
static fn ($value, $key): bool => $key === $column;

/** @var Closure(iterable<TKey, T>): Generator<int, mixed> $pipe */
$pipe = (new Pipe())()(
(new Transpose())(),
(new Filter())()($filterCallbackBuilder($column)),
(new Filter())()($filterCallbackBuilder),
(new Head())(),
(new Flatten())()(1)
);
Expand Down
4 changes: 2 additions & 2 deletions src/Operation/DropWhile.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ static function (iterable $iterable) use ($callbacks): Generator {
* @param iterable<TKey, T> $iterable
*/
static fn (int $index, $value, $key, iterable $iterable): bool => CallbacksArrayReducer::or()($callbacks)($value, $key, $iterable)
)(static fn (bool $r, int $index): int => $index)($iteratorAggregate);
)($iteratorAggregate);

$offset = (true === $current = $every->current()) ? 0 : $current;
$offset = (true === $every->current()) ? 0 : $every->key();

yield from (new Limit())()(-1)($offset)($iteratorAggregate);
};
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Equals.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ static function (iterable $other): Closure {
*/
static fn (int $index, $current): bool => (new Contains())()($current)($otherAggregate)->current();

yield from (new Every())()($containsCallback)(static fn (bool $i): bool => $i)($iteratorAggregate);
yield from (new Every())()($containsCallback)($iteratorAggregate);
};
};
}
Expand Down
34 changes: 13 additions & 21 deletions src/Operation/Every.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,41 +24,33 @@
final class Every extends AbstractOperation
{
/**
* @return Closure(callable(int=, T=, TKey=, iterable<TKey, T>=): bool): Closure(callable(bool, int, T, TKey, iterable<TKey, T>...): mixed): Closure(iterable<TKey, T>): Generator<int, mixed>
* @return Closure(...callable(int=, T=, TKey=, iterable<TKey, T>=): bool): Closure(iterable<TKey, T>): Generator<int, bool>
*/
public function __invoke(): Closure
{
return
/**
* @param callable(int=, T=, TKey=, iterable<TKey, T>=): bool $predicate
* @param callable(int=, T=, TKey=, iterable<TKey, T>=): bool ...$predicates
*
* @return Closure(callable(bool, int, T=, TKey=, iterable<TKey, T>=): mixed): Closure(iterable<TKey, T>): Generator<int, mixed>
* @return Closure(iterable<TKey, T>): Generator<int, bool>
*/
static function (callable ...$predicates): Closure {
return
/**
* @param callable(bool, int, T=, TKey=, iterable<TKey, T>=): mixed $return
* @param iterable<TKey, T> $iterable
*
* @return Closure(iterable<TKey, T>): Generator<int, mixed>
* @return Generator<int, bool>
*/
static function (callable $return) use ($predicates): Closure {
return
/**
* @param iterable<TKey, T> $iterable
*
* @return Generator<int, mixed>
*/
static function (iterable $iterable) use ($return, $predicates): Generator {
$predicate = CallbacksArrayReducer::or()($predicates);
static function (iterable $iterable) use ($predicates): Generator {
$predicate = CallbacksArrayReducer::or()($predicates);

foreach ((new Pack())()($iterable) as $index => [$key, $value]) {
if (false === $predicate($index, $value, $key, $iterable)) {
return yield $return(false, $index, $value, $key, $iterable);
}
}
foreach ((new Pack())()($iterable) as $index => [$key, $value]) {
if (false === $predicate($index, $value, $key, $iterable)) {
return yield $index => false;
}
}

return yield true;
};
yield true;
};
};
}
Expand Down
2 changes: 0 additions & 2 deletions src/Operation/MatchOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ static function (callable ...$callbacks) use ($matchers): Closure {
* @param TKey $key
*/
static fn (int $index, $value, $key, iterable $iterable): bool => $callback($value, $key, $iterable) !== $matcher($value, $key, $iterable)
)(
static fn (bool $i): bool => $i
),
(new Head())(),
(new Map())()(
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/ScanLeft1.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ static function (iterable $iterable) use ($callback): Generator {
(new Prepend())()($initial)
);

yield from $pipe($iteratorAggregate->getIterator());
yield from $pipe($iteratorAggregate);
};
}
}
4 changes: 2 additions & 2 deletions src/Operation/TakeWhile.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ static function (iterable $iterable) use ($callbacks): Generator {
* @param iterable<TKey, T> $iterable
*/
static fn (int $index, $value, $key, iterable $iterable): bool => CallbacksArrayReducer::or()($callbacks)($value, $key, $iterable)
)(static fn (bool $r, int $index): int => $index)($iteratorAggregate);
)($iteratorAggregate);

$size = (true === $current = $every->current()) ? -1 : $current;
$size = (true === $every->current()) ? -1 : $every->key();

yield from (new Limit())()($size)(0)($iteratorAggregate);
};
Expand Down
4 changes: 2 additions & 2 deletions src/Operation/Until.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ static function (iterable $iterable) use ($callbacks): Generator {
* @param iterable<TKey, T> $iterable
*/
static fn (int $index, $value, $key, iterable $iterable): bool => !CallbacksArrayReducer::or()($callbacks)($value, $key, $iterable)
)(static fn (bool $r, int $index): int => $index)($iteratorAggregate);
)($iteratorAggregate);

$size = (true === $current = $every->current()) ? -1 : $current + 1;
$size = (true === $every->current()) ? -1 : $every->key() + 1;

yield from (new Limit())()($size)(0)($iteratorAggregate);
};
Expand Down

0 comments on commit 5f9c32c

Please sign in to comment.