Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Pass $key to closure in Collection and LazyCollection's reduce method as well #35878

Merged
merged 11 commits into from Jan 15, 2021
30 changes: 0 additions & 30 deletions src/Illuminate/Collections/Collection.php
Expand Up @@ -872,36 +872,6 @@ public function random($number = null)
return new static(Arr::random($this->items, $number));
}

/**
* Reduce the collection to a single value.
*
* @param callable $callback
* @param mixed $initial
* @return mixed
*/
public function reduce(callable $callback, $initial = null)
{
return array_reduce($this->items, $callback, $initial);
}

/**
* Reduce an associative collection to a single value.
*
* @param callable $callback
* @param mixed $initial
* @return mixed
*/
public function reduceWithKeys(callable $callback, $initial = null)
{
$result = $initial;

foreach ($this->items as $key => $value) {
$result = $callback($result, $value, $key);
}

return $result;
}

/**
* Replace the collection items with the given items.
*
Expand Down
36 changes: 0 additions & 36 deletions src/Illuminate/Collections/LazyCollection.php
Expand Up @@ -827,42 +827,6 @@ public function random($number = null)
return is_null($number) ? $result : new static($result);
}

/**
* Reduce the collection to a single value.
*
* @param callable $callback
* @param mixed $initial
* @return mixed
*/
public function reduce(callable $callback, $initial = null)
{
$result = $initial;

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

return $result;
}

/**
* Reduce an associative collection to a single value.
*
* @param callable $callback
* @param mixed $initial
* @return mixed
*/
public function reduceWithKeys(callable $callback, $initial = null)
{
$result = $initial;

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

return $result;
}

/**
* Replace the collection items with the given items.
*
Expand Down
36 changes: 36 additions & 0 deletions src/Illuminate/Collections/Traits/EnumeratesValues.php
Expand Up @@ -716,6 +716,42 @@ public function tap(callable $callback)
return $this;
}

/**
* Reduce the collection to a single value.
*
* @param callable $callback
* @param mixed $initial
* @return mixed
*/
public function reduce(callable $callback, $initial = null)
{
$result = $initial;

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

return $result;
}

/**
* Reduce an associative collection to a single value.
*
* @param callable $callback
* @param mixed $initial
* @return mixed
*/
public function reduceWithKeys(callable $callback, $initial = null)
{
$result = $initial;

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

return $result;
}

/**
* Create a collection of all elements that do not pass a given truth test.
*
Expand Down
8 changes: 8 additions & 0 deletions tests/Support/SupportCollectionTest.php
Expand Up @@ -3590,6 +3590,14 @@ public function testReduce($collection)
$this->assertEquals(6, $data->reduce(function ($carry, $element) {
return $carry += $element;
}));

$data = new $collection([
'foo' => 'bar',
'baz' => 'qux',
]);
$this->assertEquals('foobarbazqux', $data->reduce(function ($carry, $element, $key) {
return $carry .= $key.$element;
}));
}

/**
Expand Down