subtraction negate single argument

This commit is contained in:
2023-06-14 07:03:20 -06:00
parent d46e386972
commit f6027cc00c
3 changed files with 10 additions and 5 deletions

View File

@@ -103,6 +103,10 @@ class Prelude {
public static var add:Function = makeVarArgsWithArrayCheck(_add, "+");
static function _subtract(values:Array<Dynamic>):Dynamic {
// with one argument, just negate:
if (values.length == 1)
return -values[0];
var difference:Float = values[0];
for (value in values.slice(1))
difference -= value;

View File

@@ -72,11 +72,11 @@ class BasicTestCase extends Test {
Assert.equals(6, BasicTestCase.mySum);
}
function testVariadicSubtract() {
Assert.equals(-2, BasicTestCase.myDifference);
function testSubtraction() {
_testSubtraction();
}
function testVariadicMultiply() {
function testMultiplication() {
_testMultiplication();
}

View File

@@ -46,10 +46,11 @@
(function _testCollect []
(Assert.equals "[0,1,2]" (Std.string (collect (range 3)))))
// Variadic math uses haxe's Lambda.fold under the hood
(var mySum (+ 1 2 3))
(var myDifference (- 5 4 3))
(function _testSubtraction []
(Assert.equals -2 (- 5 4 3))
(Assert.equals -2 (- 2)))
(function _testMultiplication []
(Assert.equals 60 (* 2 5 6))