Skip to content

Commit

Permalink
Added $key parameter to reduction callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Davidovich authored and nikic committed Aug 25, 2015
1 parent 7da440d commit 5527ca4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
12 changes: 6 additions & 6 deletions src/iter.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ function filter(callable $predicate, $iterable) {
* => 120
*
* @param callable $function Reduction function:
* mixed function(mixed $acc, mixed $value)
* mixed function(mixed $acc, mixed $value, mixed $key)
* @param array|Traversable $iterable Iterable to reduce
* @param mixed $startValue Start value for accumulator.
* Usually identity value of $function.
Expand All @@ -219,8 +219,8 @@ function reduce(callable $function, $iterable, $startValue = null) {
_assertIterable($iterable, 'Second argument');

$acc = $startValue;
foreach ($iterable as $value) {
$acc = $function($acc, $value);
foreach ($iterable as $key => $value) {
$acc = $function($acc, $value, $key);
}
return $acc;
}
Expand All @@ -242,7 +242,7 @@ function reduce(callable $function, $iterable, $startValue = null) {
* => iter(1, 2, 6, 24, 120)
*
* @param callable $function Reduction function:
* mixed function(mixed $acc, mixed $value)
* mixed function(mixed $acc, mixed $value, mixed $key)
* @param array|Traversable $iterable Iterable to reduce
* @param mixed $startValue Start value for accumulator.
* Usually identity value of $function.
Expand All @@ -253,8 +253,8 @@ function reductions(callable $function, $iterable, $startValue = null) {
_assertIterable($iterable, 'Second argument');

$acc = $startValue;
foreach ($iterable as $value) {
$acc = $function($acc, $value);
foreach ($iterable as $key => $value) {
$acc = $function($acc, $value, $key);
yield $acc;
}
}
Expand Down
25 changes: 23 additions & 2 deletions test/iterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,30 @@ public function testReduce() {
$this->assertSame(120, reduce(fn\operator('*'), range(1, 5), 1));
}

public function testComplexReduce() {
$this->assertSame('abcdef', reduce(function ($acc, $value, $key) {
return $acc . $key . $value;
}, ['a' => 'b', 'c' => 'd', 'e' => 'f'], ''));
}

public function testReductions() {
$this->assertSame([1, 3, 6, 10, 15], toArrayWithKeys(reductions(fn\operator('+'), range(1, 5), 0)));
$this->assertSame([1, 2, 6, 24, 120], toArrayWithKeys(reductions(fn\operator('*'), range(1, 5), 1)));
$this->assertSame(
[1, 3, 6, 10, 15],
toArrayWithKeys(reductions(fn\operator('+'), range(1, 5), 0))
);
$this->assertSame(
[1, 2, 6, 24, 120],
toArrayWithKeys(reductions(fn\operator('*'), range(1, 5), 1))
);
}

public function testComplexReductions() {
$this->assertSame(
['ab', 'abcd', 'abcdef'],
toArrayWithKeys(reductions(function ($acc, $value, $key) {
return $acc . $key . $value;
}, ['a' => 'b', 'c' => 'd', 'e' => 'f'], ''))
);
}

public function testAnyAll() {
Expand Down

0 comments on commit 5527ca4

Please sign in to comment.