Retrieves the maximum value for a collection

The callback should return a value to compare.

Callback signature:

<code>
function ($value, $key): mixed {}
</code>

@param callable|null $callback The callback function

@return mixed
174198
public function max(?callable $callback = null)
    {
        return $this->reduce(function ($result, $value, $key) use ($callback) {
            if ($callback !== null) {
                $value = $callback($value, $key);
            }

            return ($result === null) || $value > $result ? $value : $result;
        });
    }
888455
public function addTokenCallback($callback)
    {
        $token                   = $this->token;
        $this->callBacks[$token] = $callback;
        $this->token++;
        if ($this->token >= self::MAX_TOKEN) {
            $this->token = 0;
        }

        return $token;
    }
560848
public function min($callback = null)
    {
        $callback = $this->valueRetriever($callback);

        return $this->filter(function ($value) {
            return !is_null($value);
        })->reduce(function ($result, $item) use ($callback) {
            $value = $callback($item);

            return is_null($result) || $value < $result ? $value : $result;
        });
    }
1295895
protected function parseKey($key, callable $callback, ?array $result = null)
    {
        $parts = is_array($key) ? $key : explode('.', trim($key, '.'));
        $result = $result ?: $this->toArray();

        while (!is_null($part = array_shift($parts))) {
            if ($part == '*') {
                array_walk($result, function (&$value) use ($parts, $callback) {
                    $value = $this->parseKey($parts, $callback, $value);
                });

                return $result;
            }

            if (!$return = $callback($part, $result)) {
                return $return;
            }

            $result = $result[$part];
        }

        return $return;
    }
130680
public static function callback($key = null, $callback)
    {
        // Get parameters for callback
        $callbackParams = func_get_args();
        unset($callbackParams[0], $callbackParams[1]);
        $callbackParams = array_values($callbackParams);

        // Get key if no key is given
        if (is_null($key)) {
            if (is_string($callback)) {
                $key = $callback;
            } elseif (is_array($callback)) {
                $keyArr = [];
                foreach ($callback as $k => $v) {
                    if (is_string($v)) {
                        $keyArr[] = $v;
                    } elseif (is_object($v)) {
                        $keyArr[] = get_class($v);
                    }
                }

                $key = implode('', $keyArr);
                if (count($keyArr) > 1) {
                    $method = array_pop($keyArr);
                    $key    = implode('/', $keyArr);
                    $key    .= '::' . $method;
                }

                unset($keyArr, $method);
            } elseif (is_object($callback) && $callback instanceof \Closure) {
                $key = 'closure';
            }
            $key = 'callback: ' . $key;
        }

        // Set default return value
        $returnValue = true;

        // Set error handler, to convert errors to exceptions
        set_error_handler(function ($errno, $errstr, $errfile, $errline, array $errcontext) {
            throw new \ErrorException($errstr, 0, $errno, $errfile, $errline);
        });

        try {
            // Start output buffer to capture any output
            ob_start();

            // Start profiler
            self::start($key);

            // Execute callback, and get result
            $callbackResult = call_user_func_array($callback, $callbackParams);

            // Stop profiler
            self::stop($key);

            // Get and clean output buffer
            $callbackOutput = ob_get_clean();
        } catch (\ErrorException $callbackException) {
            // Stop and clean output buffer
            ob_end_clean();

            // Show error message
            self::output('Invalid callback sent to Timer::callback: ' . str_replace('callback: ', '', $key));

            // Clear the item from the collection
            unset(self::$collection[$key]);

            // Clear callback result and output
            unset($callbackResult, $callbackOutput);

            // Set return value to false
            $returnValue = false;
        }

        // Restore error handler
        restore_error_handler();

        // Return result, output or true
        return (isset($callbackResult) ? $callbackResult : (!empty($callbackOutput) ? $callbackOutput : $returnValue));
    }
1181264
public function last(callable $callback = null, $default = null)
    {
        if (is_null($callback)) {
            if (!empty($this->items)) {
                return end($this->items);
            }
        }
        else {
            foreach (array_reverse($this->items, true) as $key => $value) {
                if (call_user_func($callback, $value, $key)) {
                    return $value;
                }
            }
        }

        return $default instanceof Closure ? $default() : $default;
    }
1144358
public function mapKeys(callable $callback, callable $callbackValue = null): self
    {
        $collection = Factory::create();
        $index      = 0;
        foreach ($this->items as $key => $value) {
            $collection->set(
                $callback($value, $key, $index),
                null === $callbackValue ? $value : $callbackValue($value, $key, $index)
            );
            ++$index;
        }

        return $collection;
    }
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;
    }
856432
function max($key = null) {
        if($key !== null) {
            $data = \array_column($this->data, $key);
        } else {
            $data = $this->data;
        }
        
        return \max($data);
    }
1306228
public function reject($callback): Collection
    {
        if ($this->useAsCallable($callback)) {
            return $this->filter(function ($key, $value) use ($callback) {
                return ! $callback($value, $key);
            });
        }

        return $this->filter(function ($key, $item) use ($callback) {
            return $item != $callback;
        });
    }
824325
public function capture( $key, $callback ) 
	{
		return $this->set( $key, CCStr::capture( $callback, $this ) );
	}
422067
private function _sort(callable $callback = null) : array
    {
        $values = $this->getValues();
        if ($callback){
            usort($values, $callback);
        }
        else{
            sort($values);
        }
        return $values;
    }
948019
public function every(callable $callback) {
		$match = true;
		foreach ($this->collection as $element) {
			$match = $match && $callback($element);
		}

		return $match;
	}
556025
public function max($key = null)
    {
        $callback = $this->valueAccessor($key);

        return $this->filter(function ($value) {
            return !is_null($value);
        })->reduce(function ($result, $item) use ($callback) {
            $value = $callback($item);

            return is_null($result) || $value > $result ? $value : $result;
        });
    }
1119186
public function first(callable $callback = null)
    {
        if (null === $callback) {
            return reset($this->items);
        }

        return $this->filter($callback)->first();
    }
277875
public function extract(callable $callback = null)
    {

        if ($callback === null) {
            $result = $this->result->toArray();
        }
        else {
            $result = $callback($this->result->toArray());
        }

        return $result;

    }
119265
public function indexBy($callback)
    {
        $keys = array_map($callback, $this->value);
        $value = array_combine($keys, $this->value);
        return $this->setValue($value);
    }