Illuminate\Support\Collection::reduce PHP Method

reduce() public method

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

Usage Example

Exemplo n.º 1
0
 /**
  * Determines a winner from the list of buzzes accumulated.
  *
  * Note: This method is destructive, and will clear the list of buzzes on completion.
  * @return BuzzerResolution
  */
 public function resolve()
 {
     if ($this->isEmpty()) {
         return BuzzerResolution::createFailure();
     }
     /** @var BuzzReceivedEvent $winner */
     $winner = $this->buzzes->reduce(function (BuzzReceivedEvent $carry, BuzzReceivedEvent $event) {
         if ($event->getDifference() < $carry->getDifference()) {
             return $event;
         }
         return $carry;
     }, $this->buzzes->first());
     $resolution = BuzzerResolution::createSuccess($winner->getContestant(), $winner->getDifference());
     $this->buzzes = new Collection();
     return $resolution;
 }
All Usage Examples Of Illuminate\Support\Collection::reduce