Skip to content

Commit

Permalink
[8.x] Pass $key to closure in Collection and LazyCollection's reduce …
Browse files Browse the repository at this point in the history
…method as well (#35878)

* add reduce with keys to collections

* add reduce with keys to lazy collections

* add test for reduce with keys

* fix style

* merge reduceWithKeys into the existing reduce method

* remove reduce and reduceWithKeys from Collection

* remove reduce and reduceWithKeys from LazyCollection

* add reduce and reduceWithKeys to EnumeratesValues

* add test for reduce with keys and reduceWithKeys
  • Loading branch information
mokhosh committed Jan 15, 2021
1 parent ab79d0c commit e1d3158
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 66 deletions.
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

0 comments on commit e1d3158

Please sign in to comment.