variadic min, max

This commit is contained in:
2020-11-14 17:33:27 -07:00
parent fadd9d32e2
commit 0fe8cbd28b
4 changed files with 36 additions and 1 deletions

View File

@@ -31,6 +31,10 @@ class Macros {
CallExp(Symbol("Prelude.pow"), [exps[1], exps[0]]); CallExp(Symbol("Prelude.pow"), [exps[1], exps[0]]);
}; };
macros["min"] = foldMacro("Prelude.minInclusive");
macros["max"] = foldMacro("Prelude.maxInclusive");
return macros; return macros;
} }

View File

@@ -24,4 +24,24 @@ class Prelude {
public static function pow(exponent, base) { public static function pow(exponent, base) {
return Math.pow(base, exponent); return Math.pow(base, exponent);
} }
public static function minInclusive(a, b) {
return Math.min(a, b);
}
public static function minExclusive(a, b) {
return if (a == b) null else Math.min(a, b);
}
public static function maxInclusive(a, b) {
return Math.max(a, b);
}
public static function maxExclusive(a, b) {
return if (a == b) null else Math.max(a, b);
}
public static function areEqual(a, b) {
return if (a == b) a else null;
}
} }

View File

@@ -74,4 +74,12 @@ class BasicTestCase extends Test {
function testUnop() { function testUnop() {
Assert.equals(7, BasicTestCase.myInc); Assert.equals(7, BasicTestCase.myInc);
} }
function testMin() {
Assert.equals(1, BasicTestCase.myMin);
}
function testMax() {
Assert.equals(9, BasicTestCase.myMax);
}
} }

View File

@@ -39,3 +39,6 @@
(defvar _myNum 6) (defvar _myNum 6)
(defvar myInc ++_myNum) (defvar myInc ++_myNum)
(defvar myMin (min 9 3 7 1))
(defvar myMax (max 9 3 7 1))