Skip to content

Commit

Permalink
refactor: Optimize Filter operation calls by calling regular constr…
Browse files Browse the repository at this point in the history
…uctor.

Special treatment for `Collapse` operation.
  • Loading branch information
drupol committed Aug 25, 2021
1 parent 977b7bc commit 4cab31c
Show file tree
Hide file tree
Showing 14 changed files with 49 additions and 36 deletions.
6 changes: 4 additions & 2 deletions src/Operation/Collapse.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ final class Collapse extends AbstractOperation
/**
* @pure
*
* @psalm-suppress ImpureFunctionCall - using Filter and Flatten as an internal tools, not returned.
*
* @return Closure(Iterator<TKey, (T|iterable<TKey, T>)>): Generator<TKey, T>
*/
public function __invoke(): Closure
Expand All @@ -36,8 +38,8 @@ public function __invoke(): Closure

/** @var Closure(Iterator<TKey, (T|iterable<TKey, T>)>): Generator<TKey, T> $pipe */
$pipe = Pipe::of()(
Filter::of()($filterCallback),
Flatten::of()(1),
(new Filter())()($filterCallback),
(new Flatten())()(1),
);

// Point free style.
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ static function ($column): Closure {
/** @var Closure(Iterator<TKey, T>): Generator<int, mixed> $pipe */
$pipe = Pipe::of()(
Transpose::of(),
Filter::of()($filterCallbackBuilder($column)),
(new Filter())()($filterCallbackBuilder($column)),
Head::of(),
Flatten::of()(1)
);
Expand Down
13 changes: 10 additions & 3 deletions src/Operation/Compact.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,17 @@ public function __invoke(): Closure
* @return Closure(Iterator<TKey, T>): Generator<TKey, T>
*/
static function (...$values): Closure {
$filterCallback = static fn (array $values): Closure => static fn ($value): bool => !in_array($value, $values, true);
$filterCallback =
/**
* @param non-empty-array<int, null|array|int|bool|string|T> $values
*/
static fn (array $values): Closure =>
/**
* @param T $value
*/
static fn ($value): bool => !in_array($value, $values, true);

/** @var Closure(Iterator<TKey, T>): Generator<TKey, T> $filter */
$filter = Filter::of()(
$filter = (new Filter())()(
$filterCallback(
[] === $values ?
Nullsy::VALUES :
Expand Down
13 changes: 8 additions & 5 deletions src/Operation/Diff.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,17 @@ public function __invoke(): Closure
* @return Closure(Iterator<TKey, T>): Generator<TKey, T>
*/
static function (...$values): Closure {
$filterCallbackFactory = static fn (array $values): Closure =>
$filterCallbackFactory =
/**
* @param T $value
* @param list<T> $values
*/
static fn ($value): bool => false === in_array($value, $values, true);
static fn (array $values): Closure =>
/**
* @param T $value
*/
static fn ($value): bool => false === in_array($value, $values, true);

/** @var Closure(Iterator<TKey, T>): Generator<TKey, T> $filter */
$filter = Filter::of()($filterCallbackFactory($values));
$filter = (new Filter())()($filterCallbackFactory($values));

// Point free style.
return $filter;
Expand Down
3 changes: 1 addition & 2 deletions src/Operation/DiffKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ static function (...$keys): Closure {
*/
static fn ($value, $key): bool => false === in_array($key, $keys, true);

/** @var Closure(Iterator<TKey, T>): Generator<TKey, T> $filter */
$filter = Filter::of()($filterCallbackFactory($keys));
$filter = (new Filter())()($filterCallbackFactory($keys));

// Point free style.
return $filter;
Expand Down
17 changes: 10 additions & 7 deletions src/Operation/Forget.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,18 @@ public function __invoke(): Closure
* @return Closure(Iterator<TKey, T>): Generator<TKey, T>
*/
static function (...$keys): Closure {
$filterCallbackFactory = static fn (array $keys): Closure =>
$filterCallbackFactory =
/**
* @param T $value
* @param TKey $key
* @param list<TKey> $keys
*/
static fn ($value, $key): bool => false === in_array($key, $keys, true);

/** @var Closure(Iterator<TKey, T>): Generator<TKey, T> $filter */
$filter = Filter::of()($filterCallbackFactory($keys));
static fn (array $keys): Closure =>
/**
* @param T $value
* @param TKey $key
*/
static fn ($value, $key): bool => false === in_array($key, $keys, true);

$filter = (new Filter())()($filterCallbackFactory($keys));

// Point free style.
return $filter;
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Get.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ static function ($default) use ($keyToGet): Closure {

/** @var Closure(Iterator<TKey, T>): (Generator<TKey, T|null>) $pipe */
$pipe = Pipe::of()(
Filter::of()($filterCallback),
(new Filter())()($filterCallback),
Append::of()($default),
Head::of()
);
Expand Down
13 changes: 8 additions & 5 deletions src/Operation/Intersect.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,17 @@ public function __invoke(): Closure
* @return Closure(Iterator<TKey, T>): Generator<TKey, T>
*/
static function (...$values): Closure {
$filterCallbackFactory = static fn (array $values): Closure =>
$filterCallbackFactory =
/**
* @param T $value
* @param list<T> $values
*/
static fn ($value): bool => in_array($value, $values, true);
static fn (array $values): Closure =>
/**
* @param T $value
*/
static fn ($value): bool => in_array($value, $values, true);

/** @var Closure(Iterator<TKey, T>): Generator<TKey, T> $filter */
$filter = Filter::of()($filterCallbackFactory($values));
$filter = (new Filter())()($filterCallbackFactory($values));

// Point free style.
return $filter;
Expand Down
3 changes: 1 addition & 2 deletions src/Operation/IntersectKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ static function (...$keys): Closure {
*/
static fn ($value, $key): bool => in_array($key, $keys, true);

/** @var Closure(Iterator<TKey, T>): Generator<TKey, T> $filter */
$filter = Filter::of()($filterCallbackFactory($keys));
$filter = (new Filter())()($filterCallbackFactory($keys));

// Point free style.
return $filter;
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Matching.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ static function (Criteria $criteria): Closure {
$pipes = [];

if (null !== $expr) {
$pipes[] = Filter::of()((new ClosureExpressionVisitor())->dispatch($expr));
$pipes[] = (new Filter())()((new ClosureExpressionVisitor())->dispatch($expr));
}

$orderings = $criteria->getOrderings();
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Nth.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ static function (int $offset) use ($step): Closure {
/** @var Closure(Iterator<TKey, T>): Generator<TKey, T> $pipe */
$pipe = Pipe::of()(
Pack::of(),
Filter::of()($filterCallback),
(new Filter())()($filterCallback),
Unpack::of()
);

Expand Down
3 changes: 1 addition & 2 deletions src/Operation/RSample.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ public function __invoke(): Closure
static function (float $probability): Closure {
$callback = static fn (): bool => (mt_rand() / mt_getrandmax()) < $probability;

/** @var Closure(Iterator<TKey, T>): Generator<TKey, T> $filter */
$filter = Filter::of()($callback);
$filter = (new Filter())()($callback);

// Point free style.
return $filter;
Expand Down
3 changes: 1 addition & 2 deletions src/Operation/Reject.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ static function (callable ...$callbacks): Closure {
[$defaultCallback] :
$callbacks;

/** @var Closure(Iterator<TKey, T>): Generator<TKey, T> $reject */
$reject = Filter::of()(
$reject = (new Filter())()(
/**
* @param T $current
* @param TKey $key
Expand Down
3 changes: 1 addition & 2 deletions src/Operation/Scale.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ static function ($v) use ($lowerBound, $upperBound, $wantedLowerBound, $wantedUp
}
);

/** @var callable(Iterator<TKey, (float | int)>):(Generator<TKey, float|int>) $filter */
$filter = Filter::of()(
$filter = (new Filter())()(
/**
* @param float|int $item
*/
Expand Down

0 comments on commit 4cab31c

Please sign in to comment.