(collect) macro

This commit is contained in:
2021-04-25 14:30:37 -06:00
parent 5a05aec560
commit 0e98e72245
3 changed files with 14 additions and 0 deletions

View File

@@ -535,6 +535,12 @@ class Macros {
]));
};
macros["collect"] = (wholeExp:ReaderExp, exps:Array<ReaderExp>, k:KissState) -> {
wholeExp.checkNumArgs(1, 1, "(collect [iterator or iterable])");
var b = wholeExp.expBuilder();
b.call(b.symbol("for"), [b.symbol("elem"), exps[0], b.symbol("elem")]);
}
return macros;
}

View File

@@ -51,6 +51,10 @@ class BasicTestCase extends Test {
Assert.equals(3, BasicTestCase.myArrayLast);
}
function testCollect() {
_testCollect();
}
function testVariadicAdd() {
Assert.equals(6, BasicTestCase.mySum);
}

View File

@@ -27,6 +27,10 @@
// Array access is via nth
(defvar myArrayLast (nth myArray -1))
// (collect) turns iterators to arrays
(defun _testCollect []
(Assert.equals "[0,1,2]" (Std.string (collect (range 3)))))
// Variadic math uses haxe's Lambda.fold under the hood
(defvar mySum (+ 1 2 3))