mirror of
https://github.com/peklaiho/madlisp.git
synced 2024-11-26 07:04:27 +00:00
Optimization: use normal for-loops in basic math functions
This commit is contained in:
parent
60726f0923
commit
b360e213bf
@ -25,32 +25,52 @@ class Math implements ILib
|
||||
if (count($args) == 1) {
|
||||
return -$args[0];
|
||||
} else {
|
||||
return array_reduce(array_slice($args, 1), fn ($a, $b) => $a - $b, $args[0]);
|
||||
$a = $args[0];
|
||||
for ($i = 1; $i < count($args); $i++) {
|
||||
$a -= $args[$i];
|
||||
}
|
||||
return $a;
|
||||
}
|
||||
}
|
||||
));
|
||||
|
||||
$env->set('*', new CoreFunc('*', 'Multiply the arguments.', 2, -1,
|
||||
function (...$args) {
|
||||
return array_reduce(array_slice($args, 1), fn ($a, $b) => $a * $b, $args[0]);
|
||||
$a = $args[0];
|
||||
for ($i = 1; $i < count($args); $i++) {
|
||||
$a *= $args[$i];
|
||||
}
|
||||
return $a;
|
||||
}
|
||||
));
|
||||
|
||||
$env->set('/', new CoreFunc('/', 'Divide the arguments.', 2, -1,
|
||||
function (...$args) {
|
||||
return array_reduce(array_slice($args, 1), fn ($a, $b) => $a / $b, $args[0]);
|
||||
$a = $args[0];
|
||||
for ($i = 1; $i < count($args); $i++) {
|
||||
$a /= $args[$i];
|
||||
}
|
||||
return $a;
|
||||
}
|
||||
));
|
||||
|
||||
$env->set('//', new CoreFunc('//', 'Divide the arguments using integer division.', 2, -1,
|
||||
function (...$args) {
|
||||
return array_reduce(array_slice($args, 1), fn ($a, $b) => intdiv($a, $b), $args[0]);
|
||||
$a = $args[0];
|
||||
for ($i = 1; $i < count($args); $i++) {
|
||||
$a = intdiv($a, $args[$i]);
|
||||
}
|
||||
return $a;
|
||||
}
|
||||
));
|
||||
|
||||
$env->set('%', new CoreFunc('%', 'Calculate the modulo of arguments.', 2, -1,
|
||||
function (...$args) {
|
||||
return array_reduce(array_slice($args, 1), fn ($a, $b) => $a % $b, $args[0]);
|
||||
$a = $args[0];
|
||||
for ($i = 1; $i < count($args); $i++) {
|
||||
$a %= $args[$i];
|
||||
}
|
||||
return $a;
|
||||
}
|
||||
));
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user