From 31dbc46a75d18bdccc6183994e68f1cbd90953ca Mon Sep 17 00:00:00 2001 From: Mohammad Sadeq Khoshnazar Date: Sun, 10 Jan 2021 12:57:45 +0330 Subject: [PATCH 1/9] add reduce with keys to collections --- src/Illuminate/Collections/Collection.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/Illuminate/Collections/Collection.php b/src/Illuminate/Collections/Collection.php index f4c7b4007a91..fd51e5de5ed8 100644 --- a/src/Illuminate/Collections/Collection.php +++ b/src/Illuminate/Collections/Collection.php @@ -884,6 +884,24 @@ 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. * From 4777a5415758f05049ee12765b66f71980bc0681 Mon Sep 17 00:00:00 2001 From: Mohammad Sadeq Khoshnazar Date: Sun, 10 Jan 2021 12:58:03 +0330 Subject: [PATCH 2/9] add reduce with keys to lazy collections --- src/Illuminate/Collections/LazyCollection.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/Illuminate/Collections/LazyCollection.php b/src/Illuminate/Collections/LazyCollection.php index 2384948f951a..4474ab863db5 100644 --- a/src/Illuminate/Collections/LazyCollection.php +++ b/src/Illuminate/Collections/LazyCollection.php @@ -845,6 +845,24 @@ public function reduce(callable $callback, $initial = null) 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. * From 1b788e795621cba583803d36b2ef9d92674ba330 Mon Sep 17 00:00:00 2001 From: Mohammad Sadeq Khoshnazar Date: Sun, 10 Jan 2021 12:58:21 +0330 Subject: [PATCH 3/9] add test for reduce with keys --- tests/Support/SupportCollectionTest.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index 6faf65a1a3c2..a75d555c0749 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -3592,6 +3592,20 @@ public function testReduce($collection) })); } + /** + * @dataProvider collectionClassProvider + */ + public function testReduceWithKeys($collection) + { + $data = new $collection([ + 'foo' => 'bar', + 'baz' => 'qux', + ]); + $this->assertEquals('foobarbazqux', $data->reduceWithKeys(function ($carry, $element, $key) { + return $carry .= $key . $element; + })); + } + /** * @dataProvider collectionClassProvider */ From b84aaecb413b35ad1990c4210e2498df494a3895 Mon Sep 17 00:00:00 2001 From: Mohammad Sadeq Khoshnazar Date: Sun, 10 Jan 2021 13:15:19 +0330 Subject: [PATCH 4/9] fix style --- src/Illuminate/Collections/Collection.php | 2 +- src/Illuminate/Collections/LazyCollection.php | 2 +- tests/Support/SupportCollectionTest.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Collections/Collection.php b/src/Illuminate/Collections/Collection.php index fd51e5de5ed8..6a8ab88818aa 100644 --- a/src/Illuminate/Collections/Collection.php +++ b/src/Illuminate/Collections/Collection.php @@ -895,7 +895,7 @@ public function reduceWithKeys(callable $callback, $initial = null) { $result = $initial; - foreach($this->items as $key => $value) { + foreach ($this->items as $key => $value) { $result = $callback($result, $value, $key); } diff --git a/src/Illuminate/Collections/LazyCollection.php b/src/Illuminate/Collections/LazyCollection.php index 4474ab863db5..ca51626b071e 100644 --- a/src/Illuminate/Collections/LazyCollection.php +++ b/src/Illuminate/Collections/LazyCollection.php @@ -856,7 +856,7 @@ public function reduceWithKeys(callable $callback, $initial = null) { $result = $initial; - foreach($this as $key => $value) { + foreach ($this as $key => $value) { $result = $callback($result, $value, $key); } diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index a75d555c0749..b622884fe6c1 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -3602,7 +3602,7 @@ public function testReduceWithKeys($collection) 'baz' => 'qux', ]); $this->assertEquals('foobarbazqux', $data->reduceWithKeys(function ($carry, $element, $key) { - return $carry .= $key . $element; + return $carry .= $key.$element; })); } From cdbc409a6242038a3775e54d3f23a2e1ea118c65 Mon Sep 17 00:00:00 2001 From: Mohammad Sadeq Khoshnazar Date: Thu, 14 Jan 2021 10:19:52 +0330 Subject: [PATCH 5/9] merge reduceWithKeys into the existing reduce method --- src/Illuminate/Collections/Collection.php | 12 ------------ src/Illuminate/Collections/LazyCollection.php | 18 ------------------ tests/Support/SupportCollectionTest.php | 2 +- 3 files changed, 1 insertion(+), 31 deletions(-) diff --git a/src/Illuminate/Collections/Collection.php b/src/Illuminate/Collections/Collection.php index 6a8ab88818aa..7796651e4612 100644 --- a/src/Illuminate/Collections/Collection.php +++ b/src/Illuminate/Collections/Collection.php @@ -880,18 +880,6 @@ public function random($number = null) * @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; diff --git a/src/Illuminate/Collections/LazyCollection.php b/src/Illuminate/Collections/LazyCollection.php index ca51626b071e..6af7ecc765ba 100644 --- a/src/Illuminate/Collections/LazyCollection.php +++ b/src/Illuminate/Collections/LazyCollection.php @@ -838,24 +838,6 @@ 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); } diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index b622884fe6c1..c024bc8db15b 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -3601,7 +3601,7 @@ public function testReduceWithKeys($collection) 'foo' => 'bar', 'baz' => 'qux', ]); - $this->assertEquals('foobarbazqux', $data->reduceWithKeys(function ($carry, $element, $key) { + $this->assertEquals('foobarbazqux', $data->reduce(function ($carry, $element, $key) { return $carry .= $key.$element; })); } From fd7213f22770303ca8c2246c586475feca16c4ff Mon Sep 17 00:00:00 2001 From: Mohammad Sadeq Khoshnazar Date: Fri, 15 Jan 2021 09:57:58 +0330 Subject: [PATCH 6/9] remove reduce and reduceWithKeys from Collection --- src/Illuminate/Collections/Collection.php | 36 ----------------------- 1 file changed, 36 deletions(-) diff --git a/src/Illuminate/Collections/Collection.php b/src/Illuminate/Collections/Collection.php index 5846f761dabd..e4549d03ed11 100644 --- a/src/Illuminate/Collections/Collection.php +++ b/src/Illuminate/Collections/Collection.php @@ -872,42 +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) - { - $result = $initial; - - foreach ($this->items 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->items as $key => $value) { - $result = $callback($result, $value, $key); - } - - return $result; - } - /** * Replace the collection items with the given items. * From 4ff9136aa1ebadb213ee5065b7880dfb8fb021bb Mon Sep 17 00:00:00 2001 From: Mohammad Sadeq Khoshnazar Date: Fri, 15 Jan 2021 09:58:24 +0330 Subject: [PATCH 7/9] remove reduce and reduceWithKeys from LazyCollection --- src/Illuminate/Collections/LazyCollection.php | 36 ------------------- 1 file changed, 36 deletions(-) diff --git a/src/Illuminate/Collections/LazyCollection.php b/src/Illuminate/Collections/LazyCollection.php index 502ba46e4990..650213133ced 100644 --- a/src/Illuminate/Collections/LazyCollection.php +++ b/src/Illuminate/Collections/LazyCollection.php @@ -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 $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; - } - /** * Replace the collection items with the given items. * From a94adebf49489016e41ba85163f217392f207447 Mon Sep 17 00:00:00 2001 From: Mohammad Sadeq Khoshnazar Date: Fri, 15 Jan 2021 09:58:47 +0330 Subject: [PATCH 8/9] add reduce and reduceWithKeys to EnumeratesValues --- .../Collections/Traits/EnumeratesValues.php | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/Illuminate/Collections/Traits/EnumeratesValues.php b/src/Illuminate/Collections/Traits/EnumeratesValues.php index 3355a9b2da29..0c0c7ce3c3ab 100644 --- a/src/Illuminate/Collections/Traits/EnumeratesValues.php +++ b/src/Illuminate/Collections/Traits/EnumeratesValues.php @@ -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. * From 11bace24071db06a5c8990a2cec49ef3e9d4df00 Mon Sep 17 00:00:00 2001 From: Mohammad Sadeq Khoshnazar Date: Fri, 15 Jan 2021 09:59:02 +0330 Subject: [PATCH 9/9] add test for reduce with keys and reduceWithKeys --- tests/Support/SupportCollectionTest.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index c024bc8db15b..1902589e181e 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -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; + })); } /** @@ -3601,7 +3609,7 @@ public function testReduceWithKeys($collection) 'foo' => 'bar', 'baz' => 'qux', ]); - $this->assertEquals('foobarbazqux', $data->reduce(function ($carry, $element, $key) { + $this->assertEquals('foobarbazqux', $data->reduceWithKeys(function ($carry, $element, $key) { return $carry .= $key.$element; })); }