Skip to content

Commit

Permalink
fix: foldleft operation
Browse files Browse the repository at this point in the history
Simplify
Documentation update
  • Loading branch information
drupol committed Jan 1, 2023
1 parent 507175d commit c3a536b
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 43 deletions.
25 changes: 9 additions & 16 deletions docs/pages/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
~~~~~~~~~
Expand Down Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion docs/pages/code/operations/compare.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
17 changes: 17 additions & 0 deletions docs/pages/code/operations/foldleft.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace App;

use loophp\collection\Collection;

include __DIR__ . '/../../../../vendor/autoload.php';

$callback = static fn (int $carry, int $item): int => $carry + $item;

$collection = Collection::fromIterable(range(1, 5))
->foldLeft($callback, 0); // 15

$collection = Collection::empty()
->foldLeft($callback, 'foo'); // 'foo'
9 changes: 6 additions & 3 deletions docs/pages/code/operations/reduce.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions src/Contract/Operation/FoldLeftable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<TKey, T>): W $callback
* @param callable(V, T, TKey, iterable<TKey, T>): V $callback
* @param V $initial
*
* @return V|W
* @return V
*/
public function foldLeft(callable $callback, mixed $initial): mixed;
}
27 changes: 12 additions & 15 deletions src/Operation/FoldLeft.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Closure;
use Generator;
use loophp\iterators\ReduceIterableAggregate;

/**
* @immutable
Expand All @@ -17,33 +18,29 @@ final class FoldLeft extends AbstractOperation
{
/**
* @template V
* @template W
*
* @return Closure(callable((V|W), T, TKey, iterable<TKey, T>): W): Closure(V): Closure(iterable<TKey, T>): Generator<TKey, V|W>
* @return Closure(callable(V, T, TKey, iterable<TKey, T>): V): Closure(V): Closure(iterable<TKey, T>): Generator<int, V>
*/
public function __invoke(): Closure
{
return
/**
* @param callable((V|W), T, TKey, iterable<TKey, T>): W $callback
* @param callable(V, T, TKey, iterable<TKey, T>): V $callback
*
* @return Closure(V): Closure(iterable<TKey, T>): Generator<TKey, V|W>
* @return Closure(V): Closure(iterable<TKey, T>): Generator<int, V>
*/
static fn (callable $callback): Closure =>
/**
* @param V $initial
*
* @return Closure(iterable<TKey, T>): Generator<TKey, V|W>
* @return Closure(iterable<TKey, T>): Generator<int, V>
*/
static function (mixed $initial) use ($callback): Closure {
/** @var Closure(iterable<TKey, T>): Generator<TKey, V|W> $pipe */
$pipe = (new Pipe())()(
(new ScanLeft())()($callback)($initial),
(new Last())()
);

// Point free style.
return $pipe;
};
static fn (mixed $initial): Closure =>
/**
* @param iterable<TKey, T> $iterable
*
* @return Generator<int, V>
*/
static fn (iterable $iterable): Generator => yield from new ReduceIterableAggregate($iterable, Closure::fromCallable($callback), $initial);
}
}

0 comments on commit c3a536b

Please sign in to comment.