-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implements reduce function for aggregating values in collections.
- Loading branch information
1 parent
3a4c315
commit 562be3e
Showing
12 changed files
with
301 additions
and
61 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
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,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TinyBlocks\Collection\Internal\Operations\Aggregate; | ||
|
||
use Closure; | ||
use TinyBlocks\Collection\Internal\Operations\NonApplicableOperation; | ||
|
||
final readonly class Reduce implements NonApplicableOperation | ||
{ | ||
public function __construct(private iterable $elements) | ||
{ | ||
} | ||
|
||
public static function from(iterable $elements): Reduce | ||
{ | ||
return new Reduce(elements: $elements); | ||
} | ||
|
||
public function execute(Closure $aggregator, mixed $initial): mixed | ||
{ | ||
$carry = $initial; | ||
|
||
foreach ($this->elements as $element) { | ||
$carry = $aggregator($carry, $element); | ||
} | ||
|
||
return $carry; | ||
} | ||
} |
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.