Skip to content

Commit

Permalink
refactor: Update MatchOne operation.
Browse files Browse the repository at this point in the history
BREAKING CHANGE: no
  • Loading branch information
drupol committed Apr 11, 2021
1 parent 9a72f7d commit 287b158
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 24 deletions.
7 changes: 0 additions & 7 deletions spec/loophp/collection/CollectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -1802,13 +1802,6 @@ static function (int $value): bool {
)
->shouldIterateAs([0 => false]);

$this::fromIterable($input)
->match(
static fn (int $value): string => 17 === $value ? 'foo' : 'bar',
static fn (): string => 'foo'
)
->shouldIterateAs([0 => false]);

$this::fromIterable($input)
->match(
static fn (int $value): bool => 5 !== $value,
Expand Down
44 changes: 27 additions & 17 deletions src/Operation/MatchOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,24 @@
final class MatchOne extends AbstractOperation
{
/**
* @psalm-return Closure(callable(T, TKey, Iterator<TKey, T>): T): Closure(callable(T, TKey, Iterator<TKey, T>): bool): Closure(Iterator<TKey, T>): Generator<int, bool>
* @psalm-return Closure(callable(T, TKey, Iterator<TKey, T>): T): Closure(callable(T, TKey, Iterator<TKey, T>): bool): Closure(Iterator<TKey, T>): Generator<TKey|int, bool>
*/
public function __invoke(): Closure
{
return
/**
* @psalm-param callable(T, TKey, Iterator<TKey, T>): T $matcher
*
* @psalm-return Closure(callable(T, TKey, Iterator<TKey, T>): bool): Closure(Iterator<TKey, T>): Generator<int, bool>
* @psalm-return Closure(callable(T, TKey, Iterator<TKey, T>): bool): Closure(Iterator<TKey, T>): Generator<TKey|int, bool>
*/
static function (callable $matcher): Closure {
static function (callable ...$matchers): Closure {
return
/**
* @psalm-param callable(T, TKey, Iterator<TKey, T>): bool ...$callbacks
*
* @psalm-return Closure(Iterator<TKey, T>): Generator<int, bool>
* @psalm-return Closure(Iterator<TKey, T>): Generator<TKey|int, bool>
*/
static function (callable ...$callbacks) use ($matcher): Closure {
static function (callable ...$callbacks) use ($matchers): Closure {
$callbackReducer =
/**
* @psalm-param list<callable(T, TKey, Iterator<TKey, T>): bool> $callbacks
Expand All @@ -58,30 +58,40 @@ static function (callable ...$callbacks) use ($matcher): Closure {
false
);

$callback = $callbackReducer($callbacks);

$mapCallback =
/**
* @param mixed $value
* @psalm-param T $value
*
* @param mixed $key
* @psalm-param TKey $key
* @psalm-param callable(T, TKey, Iterator<TKey, T>) $reducer1
*
* @psalm-param Iterator<TKey, T> $iterator
* @psalm-return Closure(callable(T, TKey, Iterator<TKey, T>)): Closure(T, TKey, Iterator<TKey, T>): bool
*/
static fn ($value, $key, Iterator $iterator): bool => $matcher($value, $key, $iterator) === $callback($value, $key, $iterator);
static fn (callable $reducer1): Closure =>
/**
* @psalm-param callable(T, TKey, Iterator<TKey, T>) $reducer2
*
* @psalm-return Closure(T, TKey, Iterator<TKey, T>): bool
*/
static fn (callable $reducer2): Closure =>
/**
* @param mixed $value
* @psalm-param T $value
*
* @param mixed $key
* @psalm-param TKey $key
*
* @psalm-param Iterator<TKey, T> $iterator
*/
static fn ($value, $key, Iterator $iterator): bool => $reducer1($value, $key, $iterator) === $reducer2($value, $key, $iterator);

$dropWhileCallback =
/**
* @param mixed $value
* @psalm-param T $value
*/
static fn ($value): bool => !(bool) $value;
static fn (bool $value): bool => false === $value;

/** @psalm-var Closure(Iterator<TKey, T>): Generator<int, bool> $pipe */
/** @psalm-var Closure(Iterator<TKey, T>): Generator<TKey|int, bool> $pipe */
$pipe = Pipe::of()(
Map::of()($mapCallback),
Map::of()($mapCallback($callbackReducer($callbacks))($callbackReducer($matchers))),
DropWhile::of()($dropWhileCallback),
Append::of()(false),
Head::of()
Expand Down

0 comments on commit 287b158

Please sign in to comment.