add functions min and max

This commit is contained in:
Pekka Laiho 2021-06-01 17:21:53 +07:00
parent 1be4e17711
commit ce74a2ff82

View File

@ -9,6 +9,8 @@ namespace MadLisp\Lib;
use MadLisp\CoreFunc; use MadLisp\CoreFunc;
use MadLisp\Env; use MadLisp\Env;
use MadLisp\MadLispException;
use MadLisp\Seq;
class Math implements ILib class Math implements ILib
{ {
@ -118,6 +120,34 @@ class Math implements ILib
fn ($a) => intval(ceil($a)) 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, $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) fn ($a, $b) => pow($a, $b)
)); ));