More math macros
This commit is contained in:
@@ -9,14 +9,34 @@ class Macros {
|
|||||||
public static function builtins() {
|
public static function builtins() {
|
||||||
var macros:Map<String, MacroFunction> = [];
|
var macros:Map<String, MacroFunction> = [];
|
||||||
|
|
||||||
macros["+"] = (exps) -> {
|
macros["+"] = foldMacro("Prelude.add");
|
||||||
CallExp(Symbol("Lambda.fold"), [ListExp(exps), Symbol("Prelude.add"), Symbol("0")]);
|
|
||||||
|
macros["-"] = foldMacro("Prelude.subtract");
|
||||||
|
|
||||||
|
macros["*"] = foldMacro("Prelude.multiply");
|
||||||
|
|
||||||
|
macros["/"] = foldMacro("Prelude.divide");
|
||||||
|
|
||||||
|
macros["%"] = (exps:Array<ReaderExp>) -> {
|
||||||
|
if (exps.length != 2) {
|
||||||
|
throw 'Got ${exps.length} arguments for % instead of 2.';
|
||||||
|
}
|
||||||
|
CallExp(Symbol("Prelude.mod"), [exps[1], exps[0]]);
|
||||||
};
|
};
|
||||||
|
|
||||||
macros["-"] = (exps:Array<ReaderExp>) -> {
|
macros["^"] = (exps:Array<ReaderExp>) -> {
|
||||||
CallExp(Symbol("Lambda.fold"), [ListExp(exps.slice(1)), Symbol("Prelude.subtract"), exps[0]]);
|
if (exps.length != 2) {
|
||||||
}
|
throw 'Got ${exps.length} arguments for ^ instead of 2.';
|
||||||
|
}
|
||||||
|
CallExp(Symbol("Prelude.pow"), [exps[1], exps[0]]);
|
||||||
|
};
|
||||||
|
|
||||||
return macros;
|
return macros;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static function foldMacro(func:String):MacroFunction {
|
||||||
|
return (exps) -> {
|
||||||
|
CallExp(Symbol("Lambda.fold"), [ListExp(exps.slice(1)), Symbol(func), exps[0]]);
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,4 +8,20 @@ class Prelude {
|
|||||||
public static function subtract(val, from) {
|
public static function subtract(val, from) {
|
||||||
return from - val;
|
return from - val;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function multiply(a, b) {
|
||||||
|
return a * b;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function divide(bottom:Float, top:Float) {
|
||||||
|
return top / bottom;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function mod(bottom, top) {
|
||||||
|
return top % bottom;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function pow(exponent, base) {
|
||||||
|
return Math.pow(base, exponent);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,4 +50,20 @@ class BasicTestCase extends Test {
|
|||||||
function testVariadicSubtract() {
|
function testVariadicSubtract() {
|
||||||
Assert.equals(-2, BasicTestCase.myDifference);
|
Assert.equals(-2, BasicTestCase.myDifference);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function testVariadicMultiply() {
|
||||||
|
Assert.equals(60, BasicTestCase.myProduct);
|
||||||
|
}
|
||||||
|
|
||||||
|
function testVariadicDivide() {
|
||||||
|
Assert.equals(0.5, BasicTestCase.myQuotient);
|
||||||
|
}
|
||||||
|
|
||||||
|
function testMod() {
|
||||||
|
Assert.equals(4, BasicTestCase.myRemainder);
|
||||||
|
}
|
||||||
|
|
||||||
|
function testPow() {
|
||||||
|
Assert.equals(256, BasicTestCase.myPower);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,4 +24,12 @@
|
|||||||
// Variadic math uses haxe's Lambda.fold under the hood
|
// Variadic math uses haxe's Lambda.fold under the hood
|
||||||
(defvar mySum (+ 1 2 3))
|
(defvar mySum (+ 1 2 3))
|
||||||
|
|
||||||
(defvar myDifference (- 5 4 3))
|
(defvar myDifference (- 5 4 3))
|
||||||
|
|
||||||
|
(defvar myProduct (* 2 5 6))
|
||||||
|
|
||||||
|
(defvar myQuotient (/ 6 3 2 2))
|
||||||
|
|
||||||
|
(defvar myRemainder (% 10 6))
|
||||||
|
|
||||||
|
(defvar myPower (^ 2 8))
|
||||||
Reference in New Issue
Block a user