Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 0 additions & 30 deletions src/Illuminate/Collections/Collection.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
28 changes: 23 additions & 5 deletions src/Illuminate/Collections/Traits/EnumeratesValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -704,16 +704,21 @@ public function pipeInto($class)
}

/**
* Pass the collection to the given callback and then return it.
* Reduce the collection to a single value.
*
* @param callable $callback
* @return $this
* @param mixed $initial
* @return mixed
*/
public function tap(callable $callback)
public function reduce(callable $callback, $initial = null)
{
$callback(clone $this);
$result = $initial;

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

return $result;
}

/**
Expand All @@ -733,6 +738,19 @@ public function reject($callback = true)
});
}

/**
* Pass the collection to the given callback and then return it.
*
* @param callable $callback
* @return $this
*/
public function tap(callable $callback)
{
$callback(clone $this);

return $this;
}

/**
* Return only unique items from the collection array.
*
Expand Down
12 changes: 3 additions & 9 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3590,18 +3590,12 @@ public function testReduce($collection)
$this->assertEquals(6, $data->reduce(function ($carry, $element) {
return $carry += $element;
}));
}

/**
* @dataProvider collectionClassProvider
*/
public function testReduceWithKeys($collection)
{
$data = new $collection([
'foo' => 'bar',
'baz' => 'qux',
'foo' => 'bar', 'foo' => 'bar',
'baz' => 'qux', 'baz' => 'qux',
]);
$this->assertEquals('foobarbazqux', $data->reduceWithKeys(function ($carry, $element, $key) {
$this->assertEquals('foobarbazqux', $data->reduce(function ($carry, $element, $key) {
return $carry .= $key.$element;
}));
}
Expand Down