Skip to content

Commit

Permalink
Pass the key to the Reduce and Recuction operations.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Sep 9, 2019
1 parent b8a7556 commit ca60af0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
8 changes: 3 additions & 5 deletions src/Operation/Reduction.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,12 @@ public function on(iterable $collection): \Closure
$initial = $this->initial;

return static function () use ($callback, $initial, $collection) {
$result = $initial;
$carry = $initial;

yield $initial;

foreach ($collection as $value) {
$result = $callback($result, $value);

yield $result;
foreach ($collection as $key => $value) {
yield $carry = $callback($carry, $value, $key);
}
};
}
Expand Down
8 changes: 4 additions & 4 deletions src/Transformation/Reduce.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ public function on(iterable $collection)
$callback = $this->callback;
$initial = $this->initial;

$result = $initial;
$carry = $initial;

foreach ($collection as $value) {
$result = $callback($result, $value);
foreach ($collection as $key => $value) {
$carry = $callback($carry, $value, $key);
}

return $result;
return $carry;
}
}

0 comments on commit ca60af0

Please sign in to comment.