Retrieve the sum of an array.

@param $items
@param null $callback

@return Amount

@throws \Exception
969976
public static function sum($items, $callback = null)
    {
        $callback = static::valueRetriever($callback);
        $sum = null;

        foreach ($items as $item) {
            if (($amount = $callback($item)) !== null) {
                $sum = $sum ? $sum->add($amount) : $amount;
            }
        }

        return $sum ?: static::zero();
    }
1053815
public function first(callable $callback = null, $default = null)
    {
        if (is_null($callback)) {
            return empty($this->items) ? $default : reset($this->items);
        }

        foreach ($this->items as $key => $value) {
            if (call_user_func($callback, $value, $key)) {
                return $value;
            }
        }

        return $default;
    }
924796
public function addMultiple(array $items)
    {
        foreach ($items as $item) {
            if ($item instanceof RequestException) {
                $this->addException($item);
            } else {
                $this->addResponse($item);
            }
        }
    }
504779
public function getSimilarItems($items, $itemCount = 3)
    {
        if (!is_array($items)) {
            $items = [$items];
        }

        $response = $this->sendQuery(['items' => $items, 'num' => intval($itemCount)]);

        return $response;
    }
385674
public function last(callable $callback = null)
    {
        if (null === $callback) {
            return end($this->items);
        }

        return $this->filter($callback)->last();
    }
1057300
public function setCashItems($items)
    {
        $this->params['cashItems'] = base64_encode(
            json_encode(
                array_map(function ($item) {
                    /** @var CashItem $item */
                    return array(
                        'name'  => $item->getName(),
                        'count' => $item->getCount(),
                        'price' => $item->getPrice()
                    );
                }, $items)));

        return $this;
    }
1019076
public function avg($callback = null)
    {
        $callback = $this->valueRetriever($callback);

        $items = $this->map(function ($value) use ($callback) {
            return $callback($value);
        })->filter(function ($value) {
            return ! is_null($value);
        });

        if ($count = $items->count()) {
            return $items->sum() / $count;
        }
    }
1331846
public function each( callable $callback, $count = 1000 )
	{
		if ( is_null( $this->orders ) && is_null( $this->unionOrders ) )
			throw new RuntimeException( 'You must specify an orderBy clause when using the "each" function.' );

		return $this->chunk( $count, function ( $results ) use ( $callback )
		{
			foreach ( $results as $key => $value )
				if ( $callback( $value, $key ) === false )
					return false;
		} );
	}
39382
public function sum($callback)
    {
        if (is_string($callback)) {
            $callback = $this->valueRetriever($callback);
        }

        return $this->reduce(function($result, $item) use ($callback) {
            return $result += $callback($item);
        }, 0);
    }
92950
public function each(callable $callback = null)
	{
		foreach($this->list as $item)
		{
			if(call_user_func($callback, $item) === false)
				return $this;
		}
		
		return $this;
	}
747204
public function each(callable $callback): ArrayCollection
    {
        foreach ($this->items as $key => $value) {
            $callback($value, $key);
        }

        return $this;
    }
1136831
public function actionIndex($callback, $id)
    {
        $model = NavItemPageBlockItem::findOne($id);
        
        if (!$model) {
            throw new Exception("Unable to find item id.");
        }
        
        $block = $model->block->getObject($model->id, 'callback');

        if (!$block) {
            throw new Exception("Unable to find block object.");
        }
        
        return ObjectHelper::callMethodSanitizeArguments($block, $this->callbackToMethod($callback), Yii::$app->request->get());
    }
762889
protected function getItems( $items )
	{
		if (is_array($items))
		{
			return $items;
		}
		if($items instanceof self)
		{
			return $items->getAll();
		}
		if($items instanceof Arrayable)
		{
			return $items->toArray();
		}
		if($items instanceof Jsonable)
		{
			return json_decode($items->toJson(), true);
		}
		if($items instanceof JsonSerializable)
		{
			return $items->jsonSerialize();
		}
		if($items instanceof Traversable)
		{
			return iterator_to_array($items);
		}

		return (array) $items;
	}
256751
static public function callback($array, $callback)
	{
	    if (is_array($array)) foreach ($array as $k=>$v) {
	        if (is_array($v)) {
	            $array[$k] = static::callback($v, $callback);
	        } else {
	            $array[$k] = $callback($v);
	        }
	    }
	    return $array;
	}
98525
public function sort(callable $callback = null)
    {
        $items = $this->items;

        $callback = $callback ?: function ($a, $b) {
            return $a == $b ? 0 : (($a < $b) ? -1 : 1);

        };

        uasort($items, $callback);

        return new static($items);
    }
611777
public function diffKeysUsing($items, callable $callback)
    {
        return new static(array_diff_ukey($this->items, $this->getArrayableItems($items), $callback));
    }
1104347
public function uksort(callable $callback): Collection
    {
        $items = $this->items;
        $keys = array_combine(array_keys($items), range(1, count($items)));

        uksort($items, function ($a, $b) use ($callback, $keys) {
            $result = call_user_func($callback, $a, $b);

            return $result === 0 ? $keys[$a] - $keys[$b] : $result;
        });

        return new static($items);
    }