From c3a536b42e91be1b3be9d9eda9638d61f598f1ce Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Sun, 1 Jan 2023 22:23:21 +0100 Subject: [PATCH] fix: `foldleft` operation Simplify Documentation update --- docs/pages/api.rst | 25 +++++++++-------------- docs/pages/code/operations/compare.php | 7 ++++++- docs/pages/code/operations/foldleft.php | 17 ++++++++++++++++ docs/pages/code/operations/reduce.php | 9 ++++++--- phpstan-baseline.neon | 5 ----- src/Contract/Operation/FoldLeftable.php | 5 ++--- src/Operation/FoldLeft.php | 27 +++++++++++-------------- 7 files changed, 52 insertions(+), 43 deletions(-) create mode 100644 docs/pages/code/operations/foldleft.php diff --git a/docs/pages/api.rst b/docs/pages/api.rst index edc89dc4b..507d13ec7 100644 --- a/docs/pages/api.rst +++ b/docs/pages/api.rst @@ -944,25 +944,18 @@ This example will return ``['a', 'b', 'c', 'd', 'a']``. foldLeft ~~~~~~~~ -Takes the initial value and the first item of the list and applies the function -to them, then feeds the function with this result and the second argument and so -on. See ``scanLeft`` for intermediate results. - -Interface: `FoldLeftable`_ +Takes the initial value and the first item of the collection and applies the +function to them, then feeds the function with this result and the second +argument and so on. See ``scanLeft`` for intermediate results. -Signature: ``Collection::foldLeft(callable $callback, $initial): mixed;`` +When the collection is empty, the initial value is returned. -.. code-block:: php +Interface: `FoldLeftable`_ - Collection::fromIterable(range('A', 'C')) - ->foldLeft( - static function (string $carry, string $item): string { - $carry .= $item; +Signature: ``Collection::foldLeft(callable $callback, mixed $initial): mixed;`` - return $carry; - }, - '' - ); // 'ABC' +.. literalinclude:: code/operations/foldleft.php + :language: php foldLeft1 ~~~~~~~~~ @@ -1800,7 +1793,7 @@ When the collection is empty, the initial value is returned. Interface: `Reduceable`_ -Signature: ``Collection::reduce(callable $callback, $initial = null): mixed;`` +Signature: ``Collection::reduce(callable $callback, mixed $initial = null): mixed;`` .. literalinclude:: code/operations/reduce.php :language: php diff --git a/docs/pages/code/operations/compare.php b/docs/pages/code/operations/compare.php index f273577cd..4e849f481 100644 --- a/docs/pages/code/operations/compare.php +++ b/docs/pages/code/operations/compare.php @@ -15,7 +15,12 @@ ? $left : $right; -$result = Collection::fromIterable([(object) ['id' => 2, 'age' => 5], (object) ['id' => 1, 'age' => 10]]) +$input = [ + (object) ['id' => 2, 'age' => 5], + (object) ['id' => 1, 'age' => 10], +]; + +$result = Collection::fromIterable($input) ->compare($callback); // (object) ['id' => 1, 'age' => 10] $result = Collection::empty() diff --git a/docs/pages/code/operations/foldleft.php b/docs/pages/code/operations/foldleft.php new file mode 100644 index 000000000..b42817418 --- /dev/null +++ b/docs/pages/code/operations/foldleft.php @@ -0,0 +1,17 @@ + $carry + $item; + +$collection = Collection::fromIterable(range(1, 5)) + ->foldLeft($callback, 0); // 15 + +$collection = Collection::empty() + ->foldLeft($callback, 'foo'); // 'foo' diff --git a/docs/pages/code/operations/reduce.php b/docs/pages/code/operations/reduce.php index cc6feb638..8ae6de702 100644 --- a/docs/pages/code/operations/reduce.php +++ b/docs/pages/code/operations/reduce.php @@ -10,8 +10,11 @@ $callback = static fn (int $carry, int $item): int => $carry + $item; -$collection = Collection::empty() - ->reduce($callback); // null - $collection = Collection::fromIterable(range(1, 5)) ->reduce($callback, 0); // 15 + +$collection = Collection::empty() + ->reduce($callback, 'foo'); // 'foo' + +$collection = Collection::empty() + ->reduce($callback); // null diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 1028137d2..43fd487d4 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -200,11 +200,6 @@ parameters: count: 1 path: src/Operation/FoldLeft.php - - - message: "#^Template type W of method loophp\\\\collection\\\\Operation\\\\FoldLeft\\:\\:__invoke\\(\\) is not referenced in a parameter\\.$#" - count: 1 - path: src/Operation/FoldLeft.php - - message: "#^Template type V of method loophp\\\\collection\\\\Operation\\\\FoldLeft1\\:\\:__invoke\\(\\) is not referenced in a parameter\\.$#" count: 1 diff --git a/src/Contract/Operation/FoldLeftable.php b/src/Contract/Operation/FoldLeftable.php index e4f635b1e..a4c11de3e 100644 --- a/src/Contract/Operation/FoldLeftable.php +++ b/src/Contract/Operation/FoldLeftable.php @@ -17,12 +17,11 @@ interface FoldLeftable * @see https://loophp-collection.readthedocs.io/en/stable/pages/api.html#foldleft * * @template V - * @template W * - * @param callable((V|W), T, TKey, iterable): W $callback + * @param callable(V, T, TKey, iterable): V $callback * @param V $initial * - * @return V|W + * @return V */ public function foldLeft(callable $callback, mixed $initial): mixed; } diff --git a/src/Operation/FoldLeft.php b/src/Operation/FoldLeft.php index cbb5af44c..992c21f56 100644 --- a/src/Operation/FoldLeft.php +++ b/src/Operation/FoldLeft.php @@ -6,6 +6,7 @@ use Closure; use Generator; +use loophp\iterators\ReduceIterableAggregate; /** * @immutable @@ -17,33 +18,29 @@ final class FoldLeft extends AbstractOperation { /** * @template V - * @template W * - * @return Closure(callable((V|W), T, TKey, iterable): W): Closure(V): Closure(iterable): Generator + * @return Closure(callable(V, T, TKey, iterable): V): Closure(V): Closure(iterable): Generator */ public function __invoke(): Closure { return /** - * @param callable((V|W), T, TKey, iterable): W $callback + * @param callable(V, T, TKey, iterable): V $callback * - * @return Closure(V): Closure(iterable): Generator + * @return Closure(V): Closure(iterable): Generator */ static fn (callable $callback): Closure => /** * @param V $initial * - * @return Closure(iterable): Generator + * @return Closure(iterable): Generator */ - static function (mixed $initial) use ($callback): Closure { - /** @var Closure(iterable): Generator $pipe */ - $pipe = (new Pipe())()( - (new ScanLeft())()($callback)($initial), - (new Last())() - ); - - // Point free style. - return $pipe; - }; + static fn (mixed $initial): Closure => + /** + * @param iterable $iterable + * + * @return Generator + */ + static fn (iterable $iterable): Generator => yield from new ReduceIterableAggregate($iterable, Closure::fromCallable($callback), $initial); } }