Get the average value of a given key.

@param  callable|string|null $callback
@return mixed
@throws \InvalidArgumentException
439735
public function avg($callback = null): ?float
    {
        $sum = 0;
        $count = 0;
        foreach ($this->generator as $key => $item) {
            if ($callback) {
                $item = $this->accessKey($item, $callback, $key);
            }

            $sum += $item;
            $count++;
        }

        // Avoid division by 0
        if (!$count) {
            return null;
        }

        return $sum / $count;
    }
327198
public function get($callback = null)
    {
        $result = $this->acquire();

        if ($result && is_callable($callback)) {
            try {
                return $callback();
            } finally {
                $this->release();
            }
        }

        return $result;
    }
932211
public function map($callback) {

      if (!is_callable($callback)) {
        throw new \InvalidArgumentException('Invalid callback function');
      }

      foreach ($this->getItems() as $index => $item) {
        call_user_func_array($callback, array($item, $index, $this));
      }

      $this->rewind();

      return $this;
    }
1233064
public function select($callback)
    {
        $hash = $this->create();

        foreach ($this as $key => $value) {
            if ($callback($value, $key) == true) {
                $hash[$key] = $value;
            }
        }

        return $hash;
    }
914176
public function getRawValue($key)
    {
        if (!array_key_exists($key, $this->data())) {
            throw new \InvalidArgumentException(sprintf('key [%s] is not present in response array.', $key));
        }

        return $this->data()[$key];
    }
671832
public function find(callable $callback)
    {
        $array = $this->getArrayCopy();
        foreach ($array as $key => $value) {
            if ($callback($value, $key, $array) === true) {
                return $value;
            }
        }
    }
1256269
public function generatedValue(callable $callback = null)
    {
        $generatedValue = new GeneratedValue($this->fieldBuilder, $this->classMetadata);

        if ($callback) {
            $callback($generatedValue);
        }

        $generatedValue->build();

        return $this;
    }
119265
public function indexBy($callback)
    {
        $keys = array_map($callback, $this->value);
        $value = array_combine($keys, $this->value);
        return $this->setValue($value);
    }
298859
protected function getCallbackPlaceholder( $callback )
    {
        $index = count( $this->callbacks );
        $uid = spl_object_hash( $this );
        $key = sprintf( '%s_%s', $uid, $index );

        $this->callbacks[$key] = $callback;

        return $key;
    }
836233
public static function getValue($key, $data, $allowNull = false)
    {
        if (is_object($key)) {
            $key = get_class($key);
        }

        $value = self::getViaPath($key, $data);

        if (is_null($value)) {
            if ($allowNull) {
                return null;
            }
            throw new \InvalidArgumentException(sprintf('%s does not exist in the haystack', $key));
        }

        return $value;
    }
845659
public function sum(callable $callback = null)
    {

        if ($callback === null)
        {
            $callback = function ($time) {

                return $time->getSeconds();
            };

            return $this->createTime(0, 0, parent::sum($callback));
        }

        return parent::sum($callback);
    }
1193242
public function extract(callable $callback = null)
    {
        $this->response = Di::getDefault()->get('response');

        if ($callback === null) {
            $this->response->setContent(json_encode($this->result->toArray()));
        }

        else {
            $this->response->setContent($callback(
                json_encode($this->result->toArray())
            ));
        }

        $this->response->send();

    }
408910
protected function extractParametersFromClass($callback)
    {
        $reflection = new ReflectionClass($callback);

        if (! $reflection->hasMethod('join')) {
            throw new Exception('Class based channel must define a "join" method.');
        }

        return $reflection->getMethod('join')->getParameters();
    }
195830
public function getCallbackObject($callback)
    {
        foreach ($this->callbacks as $object) {
            if ($this->callbacks[$object] === $callback) {
                return $object;
            }
        }
    }
1049950
public function groupBy(callable $callback)
    {
        $results = [];

        foreach ($this->items as $key => $value) {
            $groupKey = $callback($value, $key, $this->items);

            if (! isset($results[$groupKey])) {
                $results[$groupKey] = [];
            }

            $results[$groupKey][$key] = $value;
        }

        return new static($results);
    }
530439
public function indexOf(callable $callback) {
    foreach ($this->nodes as $i => $node) {
      if ($callback($node)) {
        return $i;
      }
    }
    return -1;
  }
5845
private function _serializeCallback( $callback )
    {
        $retVal = $callback;

        if ( is_array( $callback ) && 2 === count( $callback ) )
        {
            $retVal = get_class( $callback[ 0 ] ) . '/' . $callback[ 1 ];
        }

        return $retVal;
    }