diff --git a/docs/pages/api.rst b/docs/pages/api.rst index 21a62affb..dbbdfdc54 100644 --- a/docs/pages/api.rst +++ b/docs/pages/api.rst @@ -500,7 +500,7 @@ first time till the end of the list. Interface: `DropWhileable`_ -Signature: ``Collection::dropWhile(callable $callback);`` +Signature: ``Collection::dropWhile(callable ...$callbacks);`` .. code-block:: php diff --git a/spec/loophp/collection/CollectionSpec.php b/spec/loophp/collection/CollectionSpec.php index ed1429681..9247f73f6 100644 --- a/spec/loophp/collection/CollectionSpec.php +++ b/spec/loophp/collection/CollectionSpec.php @@ -569,12 +569,16 @@ public function it_can_drop(): void public function it_can_dropWhile(): void { - $isSmallerThanThree = static function ($value) { + $isSmallerThanThree = static function (int $value): bool { return 3 > $value; }; + $isGreaterThanFive = static function (int $value): bool { + return 5 > $value; + }; + $this::fromIterable([1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3]) - ->dropWhile($isSmallerThanThree) + ->dropWhile($isGreaterThanFive, $isSmallerThanThree) ->shouldIterateAs([ 2 => 3, 3 => 4, @@ -1876,11 +1880,11 @@ public function it_can_scanright1(): void $result = static function () { yield 0 => 8; - yield 0 => 1; + yield 1 => 1; - yield 1 => 12; + yield 2 => 12; - yield 2 => 2; + yield 0 => 2; }; $this::fromIterable([8, 12, 24, 2]) diff --git a/src/Collection.php b/src/Collection.php index 310df7430..767e3a1a1 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -357,9 +357,9 @@ public function drop(int ...$counts): CollectionInterface return $this->pipe(Drop::of()(...$counts)); } - public function dropWhile(callable $callback): CollectionInterface + public function dropWhile(callable ...$callbacks): CollectionInterface { - return $this->pipe(DropWhile::of()($callback)); + return $this->pipe(DropWhile::of()(...$callbacks)); } public function duplicate(): CollectionInterface diff --git a/src/Contract/Operation/DropWhileable.php b/src/Contract/Operation/DropWhileable.php index 78abd0a37..390d882ac 100644 --- a/src/Contract/Operation/DropWhileable.php +++ b/src/Contract/Operation/DropWhileable.php @@ -16,5 +16,5 @@ interface DropWhileable /** * @psalm-return \loophp\collection\Contract\Collection */ - public function dropWhile(callable $callback): Collection; + public function dropWhile(callable ...$callbacks): Collection; } diff --git a/src/Operation/DropWhile.php b/src/Operation/DropWhile.php index 0d29a42c4..9b726bee9 100644 --- a/src/Operation/DropWhile.php +++ b/src/Operation/DropWhile.php @@ -28,16 +28,26 @@ public function __invoke(): Closure * * @psalm-return Closure(Iterator): Generator */ - static function (callable $callback): Closure { + static function (callable ...$callbacks): Closure { return /** * @psalm-param Iterator $iterator * * @psalm-return Generator */ - static function (Iterator $iterator) use ($callback): Generator { + static function (Iterator $iterator) use ($callbacks): Generator { for (; $iterator->valid(); $iterator->next()) { - if (true === $callback($iterator->current(), $iterator->key(), $iterator)) { + $reduced = array_reduce( + $callbacks, + static function (bool $carry, callable $callback) use ($iterator): bool { + return ($callback($iterator->current(), $iterator->key(), $iterator)) ? + $carry : + false; + }, + true + ); + + if (true === $reduced) { continue; } diff --git a/src/Operation/FoldRight1.php b/src/Operation/FoldRight1.php index ad3d8c598..5cb908373 100644 --- a/src/Operation/FoldRight1.php +++ b/src/Operation/FoldRight1.php @@ -31,7 +31,6 @@ public function __invoke(): Closure static function (callable $callback): Closure { /** @psalm-var Closure(Iterator): Generator $pipe */ $pipe = Pipe::of()( - Reverse::of(), ScanRight1::of()($callback), Head::of() ); diff --git a/src/Operation/Key.php b/src/Operation/Key.php index 674aa2ee0..c44ccd9ec 100644 --- a/src/Operation/Key.php +++ b/src/Operation/Key.php @@ -27,18 +27,14 @@ public function __invoke(): Closure * @psalm-return Closure(Iterator): Generator */ static function (int $index): Closure { - return - /** - * @psalm-param Iterator $iterator - * - * @psalm-return Generator - */ - static function (Iterator $iterator) use ($index): Generator { - for ($i = 0; $i < $index; $i++, $iterator->next()) { - } + /** @psalm-var Closure(Iterator): Generator $pipe */ + $pipe = Pipe::of()( + Limit::of()(1)($index), + Flip::of() + ); - return yield $iterator->key(); - }; + // Point free style. + return $pipe; }; } } diff --git a/src/Operation/ScanLeft.php b/src/Operation/ScanLeft.php index c3e9e20b3..f52045ad1 100644 --- a/src/Operation/ScanLeft.php +++ b/src/Operation/ScanLeft.php @@ -37,17 +37,14 @@ static function (callable $callback): Closure { * @psalm-return Closure(Iterator): Generator */ static function ($initial = null) use ($callback): Closure { - return - /** - * @psalm-param Iterator $iterator - * - * @psalm-return Generator - */ - static function (Iterator $iterator) use ($callback, $initial): Generator { - yield $initial; + /** @psalm-var Closure(Iterator): Generator $pipe */ + $pipe = Pipe::of()( + Reduction::of()($callback)($initial), + Prepend::of()($initial) + ); - return yield from Reduction::of()($callback)($initial)($iterator); - }; + // Point free style. + return $pipe; }; }; } diff --git a/src/Operation/ScanLeft1.php b/src/Operation/ScanLeft1.php index d436f12d9..1ef84406b 100644 --- a/src/Operation/ScanLeft1.php +++ b/src/Operation/ScanLeft1.php @@ -38,12 +38,11 @@ static function (callable $callback): Closure { static function (Iterator $iterator) use ($callback): Generator { $initial = $iterator->current(); - yield $initial; - /** @psalm-var Closure(Iterator): Generator $pipe */ $pipe = Pipe::of()( Tail::of(), - Reduction::of()($callback)($initial) + Reduction::of()($callback)($initial), + Prepend::of()($initial) ); return yield from $pipe($iterator); diff --git a/src/Operation/ScanRight.php b/src/Operation/ScanRight.php index ddbc9e9b3..c97cd66c3 100644 --- a/src/Operation/ScanRight.php +++ b/src/Operation/ScanRight.php @@ -37,24 +37,16 @@ static function (callable $callback): Closure { * @psalm-return Closure(Iterator): Generator */ static function ($initial = null) use ($callback): Closure { - return - /** - * @psalm-param Iterator $iterator - * - * @psalm-return Generator - */ - static function (Iterator $iterator) use ($callback, $initial): Generator { - /** @psalm-var Closure(Iterator): Generator $pipe */ - $pipe = Pipe::of()( - Reverse::of(), - Reduction::of()($callback)($initial), - Reverse::of() - ); + /** @psalm-var Closure(Iterator): Generator $pipe */ + $pipe = Pipe::of()( + Reverse::of(), + Reduction::of()($callback)($initial), + Reverse::of(), + Append::of()($initial) + ); - yield from $pipe($iterator); - - return yield $initial; - }; + // Point free style. + return $pipe; }; }; } diff --git a/src/Operation/ScanRight1.php b/src/Operation/ScanRight1.php index e8c1b03e7..3f2da88b7 100644 --- a/src/Operation/ScanRight1.php +++ b/src/Operation/ScanRight1.php @@ -29,25 +29,15 @@ public function __invoke(): Closure * @psalm-return Closure(Iterator): Generator */ static function (callable $callback): Closure { - return - /** - * @psalm-param Iterator $iterator - * - * @psalm-return Generator - */ - static function (Iterator $iterator) use ($callback): Generator { - $initial = $iterator->current(); + /** @psalm-var Closure(Iterator): Generator $pipe */ + $pipe = Pipe::of()( + Reverse::of(), + ScanLeft1::of()($callback), + Reverse::of() + ); - yield $initial; - - /** @psalm-var Closure(Iterator): Generator $pipe */ - $pipe = Pipe::of()( - Init::of(), - Reduction::of()($callback)($initial) - ); - - return yield from $pipe($iterator); - }; + // Point free style. + return $pipe; }; } }