diff --git a/src/Lib/Math.php b/src/Lib/Math.php index 1b8842c..661ddb3 100644 --- a/src/Lib/Math.php +++ b/src/Lib/Math.php @@ -9,6 +9,8 @@ namespace MadLisp\Lib; use MadLisp\CoreFunc; use MadLisp\Env; +use MadLisp\MadLispException; +use MadLisp\Seq; class Math implements ILib { @@ -118,6 +120,34 @@ class Math implements ILib fn ($a) => intval(ceil($a)) )); + $env->set('max', new CoreFunc('max', 'Find the largest of arguments.', 1, -1, + function (...$args) { + if (count($args) == 1) { + $seq = $args[0]; + if (!($seq instanceof Seq)) { + throw new MadLispException('single argument to max is not sequence'); + } + return max($seq->getData()); + } else { + return max(...$args); + } + } + )); + + $env->set('min', new CoreFunc('min', 'Find the smallest of arguments.', 1, -1, + function (...$args) { + if (count($args) == 1) { + $seq = $args[0]; + if (!($seq instanceof Seq)) { + throw new MadLispException('single argument to min is not sequence'); + } + return min($seq->getData()); + } else { + return min(...$args); + } + } + )); + $env->set('pow', new CoreFunc('pow', 'Return the first argument raised to the power of second argument.', 2, 2, fn ($a, $b) => pow($a, $b) ));