Skip to content

Commit

Permalink
misc: Further optimizations with Every operations.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Mar 28, 2022
1 parent de1edb3 commit f523cdb
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 15 deletions.
13 changes: 5 additions & 8 deletions src/Operation/Contains.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Closure;
use Generator;
use function in_array;

/**
* @immutable
Expand All @@ -32,18 +33,14 @@ public function __invoke(): Closure
* @return Closure(iterable<TKey, T>): Generator<TKey, bool>
*/
static function (...$values): Closure {
$callback =
$matchWhen =
/**
* @param T $left
* @param T $value
*/
static fn ($left): Closure =>
/**
* @param T $right
*/
static fn ($right): bool => $left === $right;
static fn ($value): bool => in_array($value, $values, true);

/** @var Closure(iterable<TKey, T>): Generator<TKey, bool> $matchOne */
$matchOne = (new MatchOne())()(static fn (): bool => true)(...array_map($callback, $values));
$matchOne = (new MatchOne())()(static fn (): bool => true)($matchWhen);

// Point free style.
return $matchOne;
Expand Down
7 changes: 3 additions & 4 deletions src/Operation/Duplicate.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,15 @@ static function (callable $accessorCallback) use ($comparatorCallback): Closure
* @param TKey $key
*/
static function ($value, $key) use ($comparatorCallback, $accessorCallback, $stack): bool {
$matchWhenNot = static fn (): bool => true;
$matcher =
/**
* @param array{0: TKey, 1: T} $item
*/
static fn (array $item): bool => $comparatorCallback($accessorCallback($value, $key))($accessorCallback($item[1], $item[0]));
static fn (int $index, array $item): bool => !$comparatorCallback($accessorCallback($value, $key))($accessorCallback($item[1], $item[0]));

$matchFalse = (new MatchOne())()($matchWhenNot)($matcher)($stack);
$every = (new Every())()($matcher)($stack)->current();

if ($matchFalse->current()) {
if (false === $every) {
return true;
}

Expand Down
6 changes: 3 additions & 3 deletions src/Operation/Has.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public function __invoke(): Closure
* @return Closure(iterable<TKey, T>): Generator<TKey, bool>
*/
static function (callable ...$callbacks): Closure {
/** @var Closure(iterable<TKey, T>): Generator<TKey, bool> $pipe */
$pipe = (new MatchOne())()(static fn (): bool => true)(
/** @var Closure(iterable<TKey, T>): Generator<TKey, bool> $matchOne */
$matchOne = (new MatchOne())()(static fn (): bool => true)(
...array_map(
static fn (callable $callback): callable =>
/**
Expand All @@ -49,7 +49,7 @@ static function (callable ...$callbacks): Closure {
);

// Point free style.
return $pipe;
return $matchOne;
};
}
}

0 comments on commit f523cdb

Please sign in to comment.