variadic min, max
This commit is contained in:
@@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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))
|
||||||
Reference in New Issue
Block a user