collectWhileLet

This commit is contained in:
2023-05-02 16:46:08 -06:00
parent ca019159f6
commit a6c0cf88c6
2 changed files with 24 additions and 1 deletions

View File

@@ -839,6 +839,22 @@ class Macros {
]);
};
k.doc("collectWhileLet", 2, null, "(collectWhileLet [<bindings...>] <body...>)");
macros["collectWhileLet"] = (wholeExp:ReaderExp, exps:Array<ReaderExp>, k:KissState) -> {
var b = wholeExp.expBuilder();
var results = b.symbol();
var nextExp = b.symbol();
var body = b.begin(exps.slice(1));
return b.let([results, b.list([])], [
b.callSymbol("whileLet", [
exps[0],
b.let([nextExp, body],
[b.callField("push", results, [nextExp])])
]),
results
]);
};
k.doc("withTempSet", 2, null, "(withTempSet [<bindings...>] <body...>)");
macros["withTempSet"] = (wholeExp:ReaderExp, exps:Array<ReaderExp>, k:KissState) -> {
var b = wholeExp.expBuilder();

View File

@@ -679,7 +679,14 @@ From:[(assert false (+ \"false \" \"should \" \"have \" \"been \" \"true\"))]" m
stream (Stream.fromString "a\nb\nc")]
(whileLet [(Some line) (stream.takeLine)]
(Assert.equals (nth lines idx) line)
(+= idx 1))))
(+= idx 1)))
(let [lines ["a" "b" "c"]
options [(Some "a") (Some "b") (Some "c") None]
collected
(collectWhileLet [(Some line) (options.shift)]
line)]
(doFor i (range collected.length)
(Assert.equals (nth lines i) (nth collected i)))))
(function _testTrace []
(Assert.equals 5 (trace 5 "num"))