Fusonic\Linq\Linq::average PHP Method

average() public method

Computes the average of all numeric values. Uses $func to obtain the value on each element.
public average ( callable $func = null ) : double
$func callable A func that returns any numeric type (int, float etc.)
return double Average of items
    public function average(callable $func = null)
    {
        $resultTotal = 0;
        $itemCount = 0;
        $source = $this->getSelectIteratorOrInnerIterator($func);
        foreach ($source as $item) {
            if (!is_numeric($item)) {
                throw new UnexpectedValueException("Cannot calculate an average on a none numeric value");
            }
            $resultTotal += $item;
            $itemCount++;
        }
        return $itemCount == 0 ? 0 : $resultTotal / $itemCount;
    }