Skip to content

Commit e1d3158

Browse files
authored
[8.x] Pass $key to closure in Collection and LazyCollection's reduce 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
1 parent ab79d0c commit e1d3158

File tree

4 files changed

+44
-66
lines changed

4 files changed

+44
-66
lines changed

src/Illuminate/Collections/Collection.php

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -872,36 +872,6 @@ public function random($number = null)
872872
return new static(Arr::random($this->items, $number));
873873
}
874874

875-
/**
876-
* Reduce the collection to a single value.
877-
*
878-
* @param callable $callback
879-
* @param mixed $initial
880-
* @return mixed
881-
*/
882-
public function reduce(callable $callback, $initial = null)
883-
{
884-
return array_reduce($this->items, $callback, $initial);
885-
}
886-
887-
/**
888-
* Reduce an associative collection to a single value.
889-
*
890-
* @param callable $callback
891-
* @param mixed $initial
892-
* @return mixed
893-
*/
894-
public function reduceWithKeys(callable $callback, $initial = null)
895-
{
896-
$result = $initial;
897-
898-
foreach ($this->items as $key => $value) {
899-
$result = $callback($result, $value, $key);
900-
}
901-
902-
return $result;
903-
}
904-
905875
/**
906876
* Replace the collection items with the given items.
907877
*

src/Illuminate/Collections/LazyCollection.php

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -827,42 +827,6 @@ public function random($number = null)
827827
return is_null($number) ? $result : new static($result);
828828
}
829829

830-
/**
831-
* Reduce the collection to a single value.
832-
*
833-
* @param callable $callback
834-
* @param mixed $initial
835-
* @return mixed
836-
*/
837-
public function reduce(callable $callback, $initial = null)
838-
{
839-
$result = $initial;
840-
841-
foreach ($this as $value) {
842-
$result = $callback($result, $value);
843-
}
844-
845-
return $result;
846-
}
847-
848-
/**
849-
* Reduce an associative collection to a single value.
850-
*
851-
* @param callable $callback
852-
* @param mixed $initial
853-
* @return mixed
854-
*/
855-
public function reduceWithKeys(callable $callback, $initial = null)
856-
{
857-
$result = $initial;
858-
859-
foreach ($this as $key => $value) {
860-
$result = $callback($result, $value, $key);
861-
}
862-
863-
return $result;
864-
}
865-
866830
/**
867831
* Replace the collection items with the given items.
868832
*

src/Illuminate/Collections/Traits/EnumeratesValues.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,42 @@ public function tap(callable $callback)
716716
return $this;
717717
}
718718

719+
/**
720+
* Reduce the collection to a single value.
721+
*
722+
* @param callable $callback
723+
* @param mixed $initial
724+
* @return mixed
725+
*/
726+
public function reduce(callable $callback, $initial = null)
727+
{
728+
$result = $initial;
729+
730+
foreach ($this as $key => $value) {
731+
$result = $callback($result, $value, $key);
732+
}
733+
734+
return $result;
735+
}
736+
737+
/**
738+
* Reduce an associative collection to a single value.
739+
*
740+
* @param callable $callback
741+
* @param mixed $initial
742+
* @return mixed
743+
*/
744+
public function reduceWithKeys(callable $callback, $initial = null)
745+
{
746+
$result = $initial;
747+
748+
foreach ($this as $key => $value) {
749+
$result = $callback($result, $value, $key);
750+
}
751+
752+
return $result;
753+
}
754+
719755
/**
720756
* Create a collection of all elements that do not pass a given truth test.
721757
*

tests/Support/SupportCollectionTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3590,6 +3590,14 @@ public function testReduce($collection)
35903590
$this->assertEquals(6, $data->reduce(function ($carry, $element) {
35913591
return $carry += $element;
35923592
}));
3593+
3594+
$data = new $collection([
3595+
'foo' => 'bar',
3596+
'baz' => 'qux',
3597+
]);
3598+
$this->assertEquals('foobarbazqux', $data->reduce(function ($carry, $element, $key) {
3599+
return $carry .= $key.$element;
3600+
}));
35933601
}
35943602

35953603
/**

0 commit comments

Comments
 (0)