Skip to content

Commit

Permalink
refactor: Minor optimizations here and there.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Apr 10, 2022
1 parent 8d851a1 commit e9c4460
Show file tree
Hide file tree
Showing 16 changed files with 45 additions and 112 deletions.
5 changes: 1 addition & 4 deletions src/Operation/Compact.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,13 @@ static function (...$values): Closure {
*/
static fn ($value): bool => !in_array($value, $values, true);

$filter = (new Filter())()(
return (new Filter())()(
$filterCallback(
[] === $values ?
Nullsy::VALUES :
$values
)
);

// Point free style.
return $filter;
};
}
}
19 changes: 6 additions & 13 deletions src/Operation/Contains.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,11 @@ public function __invoke(): Closure
*
* @return Closure(iterable<TKey, T>): Generator<TKey, bool>
*/
static function (...$values): Closure {
$matchWhen =
/**
* @param T $value
*/
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)($matchWhen);

// Point free style.
return $matchOne;
};
static fn (...$values): Closure => (new MatchOne())()(static fn (): bool => true)(
/**
* @param T $value
*/
static fn ($value): bool => in_array($value, $values, true)
);
}
}
14 changes: 6 additions & 8 deletions src/Operation/Diff.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,11 @@ public function __invoke(): Closure
*
* @return Closure(iterable<TKey, T>): Generator<TKey, T>
*/
static function (...$values): Closure {
return (new Filter())()(
/**
* @param T $value
*/
static fn ($value): bool => !in_array($value, $values, true)
);
};
static fn (...$values): Closure => (new Filter())()(
/**
* @param T $value
*/
static fn ($value): bool => !in_array($value, $values, true)
);
}
}
16 changes: 7 additions & 9 deletions src/Operation/DiffKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,12 @@ public function __invoke(): Closure
*
* @return Closure(iterable<TKey, T>): Generator<TKey, T>
*/
static function (...$keys): Closure {
return (new Filter())()(
/**
* @param T $value
* @param TKey $key
*/
static fn ($value, $key): bool => !in_array($key, $keys, true)
);
};
static fn (...$keys): Closure => (new Filter())()(
/**
* @param T $value
* @param TKey $key
*/
static fn ($value, $key): bool => !in_array($key, $keys, true)
);
}
}
5 changes: 1 addition & 4 deletions src/Operation/Distinct.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ static function (callable $accessorCallback) use ($comparatorCallback): Closure
/** @var ArrayIterator<int, array{0: TKey, 1: T}> $stack */
$stack = new ArrayIterator();

$filter = (new Filter())()(
return (new Filter())()(
/**
* @param T $value
* @param TKey $key
Expand All @@ -68,9 +68,6 @@ static function ($value, $key) use ($comparatorCallback, $accessorCallback, $sta
return true;
}
);

// Point free style.
return $filter;
};
}
}
6 changes: 1 addition & 5 deletions src/Operation/Falsy.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,11 @@ final class Falsy extends AbstractOperation
*/
public function __invoke(): Closure
{
/** @var Closure(iterable<TKey, T>): Generator<int, bool> $every */
$every = (new Every())()(
return (new Every())()(
/**
* @param T $value
*/
static fn (int $index, $value): bool => !(bool) $value
);

// Point free style.
return $every;
}
}
6 changes: 1 addition & 5 deletions src/Operation/First.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ final class First extends AbstractOperation
*/
public function __invoke(): Closure
{
/** @var Closure(iterable<TKey, T>): Generator<TKey, T> $head */
$head = (new Head())();

// Point free style.
return $head;
return (new Head())();
}
}
16 changes: 5 additions & 11 deletions src/Operation/Flip.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,22 @@ final class Flip extends AbstractOperation
*/
public function __invoke(): Closure
{
$callbackForKeys =
return (new Associate())()(
/**
* @param TKey $key
* @param T $value
*
* @return T
*/
static fn ($key, $value) => $value;

$callbackForValues =
static fn ($key, $value) => $value
)(
/**
* @param T $value
* @param TKey $key
*
* @return TKey
*/
static fn ($value, $key) => $key;

/** @var Closure(iterable<TKey, T>): Generator<T, TKey> $associate */
$associate = (new Associate())()($callbackForKeys)($callbackForValues);

// Point free style.
return $associate;
static fn ($value, $key) => $key
);
}
}
18 changes: 5 additions & 13 deletions src/Operation/Forget.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,13 @@ public function __invoke(): Closure
* @return Closure(iterable<TKey, T>): Generator<TKey, T>
*/
static function (...$keys): Closure {
$filterCallbackFactory =
return (new Filter())()(
/**
* @param list<TKey> $keys
* @param T $value
* @param TKey $key
*/
static fn (array $keys): Closure =>
/**
* @param T $value
* @param TKey $key
*/
static fn ($value, $key): bool => !in_array($key, $keys, true);

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

// Point free style.
return $filter;
static fn ($value, $key): bool => !in_array($key, $keys, true)
);
};
}
}
17 changes: 6 additions & 11 deletions src/Operation/Intersect.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,11 @@ public function __invoke(): Closure
*
* @return Closure(iterable<TKey, T>): Generator<TKey, T>
*/
static function (...$values): Closure {
$filter = (new Filter())()(
/**
* @param T $value
*/
static fn ($value): bool => in_array($value, $values, true)
);

// Point free style.
return $filter;
};
static fn (...$values): Closure => (new Filter())()(
/**
* @param T $value
*/
static fn ($value): bool => in_array($value, $values, true)
);
}
}
6 changes: 1 addition & 5 deletions src/Operation/Nullsy.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,11 @@ final class Nullsy extends AbstractOperation
*/
public function __invoke(): Closure
{
/** @var Closure(iterable<TKey, T>): Generator<int, bool> $every */
$every = (new Every())()(
return (new Every())()(
/**
* @param T $value
*/
static fn (int $index, $value): bool => in_array((bool) $value, self::VALUES, true)
);

// Point free style.
return $every;
}
}
5 changes: 1 addition & 4 deletions src/Operation/Reject.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,14 @@ static function (callable ...$callbacks): Closure {
[] === $callbacks ? [$defaultCallback] : $callbacks
);

$reject = (new Filter())()(
return (new Filter())()(
/**
* @param T $current
* @param TKey $key
* @param iterable<TKey, T> $iterable
*/
static fn ($current, $key, iterable $iterable): bool => !$callback($current, $key, $iterable)
);

// Point free style.
return $reject;
};
}
}
6 changes: 1 addition & 5 deletions src/Operation/Tail.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ final class Tail extends AbstractOperation
*/
public function __invoke(): Closure
{
/** @var Closure(iterable<TKey, T>): Generator<TKey, T> $drop */
$drop = (new Limit())()(-1)(1);

// Point free style.
return $drop;
return (new Limit())()(-1)(1);
}
}
6 changes: 1 addition & 5 deletions src/Operation/Truthy.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,11 @@ final class Truthy extends AbstractOperation
*/
public function __invoke(): Closure
{
/** @var Closure(iterable<TKey, T>): Generator<int, bool> $every */
$every = (new Every())()(
return (new Every())()(
/**
* @param T $value
*/
static fn (int $index, $value): bool => (bool) $value
);

// Point free style.
return $every;
}
}
6 changes: 1 addition & 5 deletions src/Operation/Unlines.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ final class Unlines extends AbstractOperation
*/
public function __invoke(): Closure
{
/** @var Closure(iterable<TKey, (T|string)>):Generator<TKey, string> $implode */
$implode = (new Implode())()(PHP_EOL);

// Point free style.
return $implode;
return (new Implode())()(PHP_EOL);
}
}
6 changes: 1 addition & 5 deletions src/Operation/Unwords.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ final class Unwords extends AbstractOperation
*/
public function __invoke(): Closure
{
/** @var Closure(iterable<TKey, (T|string)>): Generator<TKey, string> $implode */
$implode = (new Implode())()(' ');

// Point free style.
return $implode;
return (new Implode())()(' ');
}
}

0 comments on commit e9c4460

Please sign in to comment.