-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: Add Reduce operation. * tests: Add tests. * docs: Add documentation. * refactor: Use Reduce operation in Reverse operation. * refactor: Use Reduce instead of FoldLeft for better performance. * Minor additions Co-authored-by: AlexandruGG <alex.gidei@goodlord.co>
- Loading branch information
Showing
16 changed files
with
246 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
/** | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
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::empty() | ||
->reduce($callback); // [] | ||
|
||
$collection = Collection::fromIterable(range(1, 5)) | ||
->reduce($callback, 0); // [4 => 15] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
/** | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace loophp\collection\Contract\Operation; | ||
|
||
use Iterator; | ||
use loophp\collection\Contract\Collection; | ||
|
||
/** | ||
* @template TKey | ||
* @template T | ||
*/ | ||
interface Reduceable | ||
{ | ||
/** | ||
* Reduce a collection of items through a given callback. | ||
* | ||
* @template V | ||
* | ||
* @param callable(V, T, TKey, Iterator<TKey, T>): V $callback | ||
* @param V $initial | ||
* | ||
* @return Collection<TKey, V> | ||
*/ | ||
public function reduce(callable $callback, $initial = null): Collection; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
/** | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace loophp\collection\Operation; | ||
|
||
use Closure; | ||
use Generator; | ||
use Iterator; | ||
|
||
/** | ||
* @immutable | ||
* | ||
* @template TKey | ||
* @template T | ||
* | ||
* phpcs:disable Generic.Files.LineLength.TooLong | ||
*/ | ||
final class Reduce extends AbstractOperation | ||
{ | ||
/** | ||
* @pure | ||
* | ||
* @template V | ||
* | ||
* @return Closure(callable(V, T, TKey, Iterator<TKey, T>): V): Closure (V): Closure(Iterator<TKey, T>): Generator<TKey, V> | ||
*/ | ||
public function __invoke(): Closure | ||
{ | ||
return | ||
/** | ||
* @param callable(V, T, TKey, Iterator<TKey, T>): V $callback | ||
* | ||
* @return Closure(V): Closure(Iterator<TKey, T>): Generator<TKey, V> | ||
*/ | ||
static fn (callable $callback): Closure => | ||
/** | ||
* @param V $initial | ||
* | ||
* @return Closure(Iterator<TKey, T>): Generator<TKey, V> | ||
*/ | ||
static function ($initial) use ($callback): Closure { | ||
/** @var Closure(Iterator<TKey, T>): Generator<TKey, V> $pipe */ | ||
$pipe = Pipe::of()( | ||
Reduction::of()($callback)($initial), | ||
Last::of(), | ||
); | ||
|
||
// Point free style. | ||
return $pipe; | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.