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 27, 2022
1 parent 5f9c32c commit de1edb3
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 63 deletions.
12 changes: 8 additions & 4 deletions src/Operation/Distinct.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,20 @@ 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 (array $item): bool => !$comparatorCallback($accessorCallback($value, $key))($accessorCallback($item[1], $item[0]));

$matchFalse = (new MatchOne())()($matchWhenNot)($matcher)($stack);
$every = (new Every())()(
/**
* @param array{0: TKey, 1: T} $keyValuePair
*/
static fn (int $index, array $keyValuePair): bool => $matcher($keyValuePair)
)($stack);

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

Expand Down
26 changes: 5 additions & 21 deletions src/Operation/Falsy.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,15 @@ final class Falsy extends AbstractOperation
*/
public function __invoke(): Closure
{
$matchWhenNot = static fn (): bool => true;
$matcher =
/** @var Closure(iterable<TKey, T>): Generator<int, bool> $every */
$every = (new Every())()(
/**
* @param bool $value
* @param T $value
*/
static fn (bool $value): bool => $value;

/** @var Closure(iterable<TKey, T>): Generator<int, bool> $pipe */
$pipe = (new Pipe())()(
(new Map())()(
/**
* @param T $value
*/
static fn ($value): bool => (bool) $value
),
(new MatchOne())()($matchWhenNot)($matcher),
(new Map())()(
/**
* @param bool $value
*/
static fn (bool $value): bool => !$value
),
static fn (int $index, $value): bool => !(bool) $value
);

// Point free style.
return $pipe;
return $every;
}
}
1 change: 0 additions & 1 deletion src/Operation/MatchOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ static function (callable ...$callbacks) use ($matchers): Closure {
*/
static fn (int $index, $value, $key, iterable $iterable): bool => $callback($value, $key, $iterable) !== $matcher($value, $key, $iterable)
),
(new Head())(),
(new Map())()(
static fn (bool $i): bool => !$i
)
Expand Down
26 changes: 5 additions & 21 deletions src/Operation/Nullsy.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,15 @@ final class Nullsy extends AbstractOperation
*/
public function __invoke(): Closure
{
$matchWhenNot = static fn (): bool => false;
$matcher =
/** @var Closure(iterable<TKey, T>): Generator<int, bool> $every */
$every = (new Every())()(
/**
* @param bool $value
* @param T $value
*/
static fn (bool $value): bool => in_array($value, self::VALUES, true);

/** @var Closure(iterable<TKey, T>): Generator<int, bool> $pipe */
$pipe = (new Pipe())()(
(new Map())()(
/**
* @param T $value
*/
static fn ($value): bool => (bool) $value
),
(new MatchOne())()($matchWhenNot)($matcher),
(new Map())()(
/**
* @param bool $value
*/
static fn (bool $value): bool => !$value
),
static fn (int $index, $value): bool => in_array((bool) $value, self::VALUES, true)
);

// Point free style.
return $pipe;
return $every;
}
}
21 changes: 5 additions & 16 deletions src/Operation/Truthy.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,15 @@ final class Truthy extends AbstractOperation
*/
public function __invoke(): Closure
{
$matchWhenNot = static fn (): bool => true;
$matcher =
/** @var Closure(iterable<TKey, T>): Generator<int, bool> $every */
$every = (new Every())()(
/**
* @param bool $value
* @param T $value
*/
static fn (bool $value): bool => !$value;

/** @var Closure(iterable<TKey, T>): Generator<int, bool> $pipe */
$pipe = (new Pipe())()(
(new Map())()(
/**
* @param T $value
*/
static fn ($value): bool => (bool) $value
),
(new MatchOne())()($matchWhenNot)($matcher),
(new Map())()($matcher),
static fn (int $index, $value): bool => (bool) $value
);

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

0 comments on commit de1edb3

Please sign in to comment.