make zip, concat, and intersect wrap the result in Array type

This commit is contained in:
2022-05-09 15:12:03 -04:00
parent d8960c5e68
commit 7abb624565
3 changed files with 31 additions and 10 deletions

View File

@@ -111,16 +111,16 @@ class Kiss {
// These ones *probably* won't conflict with variables and might be passed as functions
"chooseRandom" => Symbol("Prelude.chooseRandom"),
// These ones *probably* won't conflict with variables and might commonly be used with (apply) because they are variadic
"concat" => Symbol("Prelude.concat"),
"zip" => Symbol("Prelude.zipThrow"),
"zipKeep" => Symbol("Prelude.zipKeep"),
"zipDrop" => Symbol("Prelude.zipDrop"),
"zipThrow" => Symbol("Prelude.zipThrow"),
"intersect" => Symbol("Prelude.intersect"),
"joinPath" => Symbol("Prelude.joinPath"),
"readDirectory" => Symbol("Prelude.readDirectory"),
"substr" => Symbol("Prelude.substr"),
"isListExp" => Symbol("Prelude.isListExp")
/* zip functions used to live here as aliases but now they are macros that also
apply (the Array<Array<Dynamic>>) to the result */
/* intersect used to live here as an alias but now it is in a macro that also
applies (the Array<Array<Dynamic>>) to the result */
/* concat used to live here as an alias but now it is in a macro that also
applies (the Array<Dynamic>) to the result */
],
fieldList: [],
fieldDict: new Map(),

View File

@@ -82,10 +82,17 @@ class Macros {
destructiveVersion("*", "*=");
destructiveVersion("/", "/=");
// These shouldn't be ident aliases because they are common variable names
var opAliases = [
// These shouldn't be ident aliases because they are common variable names
"min" => "Prelude.min",
"max" => "Prelude.max"
"max" => "Prelude.max",
// These can't be ident aliases because they would supercede the typed call macros that wrap them:
"zip" => "Prelude.zipThrow",
"zipThrow" => "Prelude.zipThrow",
"zipKeep" => "Prelude.zipKeep",
"zipDrop" => "Prelude.zipDrop",
"concat" => "Prelude.concat",
"intersect" => "Prelude.intersect",
];
macros["apply"] = (wholeExp:ReaderExp, exps:Array<ReaderExp>, k) -> {
@@ -1176,6 +1183,20 @@ class Macros {
b.let(bindings, body);
};
function typedCallMacro(name:String, symbol:String, type:String) {
macros[name] = (wholeExp:ReaderExp, exps:Array<ReaderExp>, k:KissState) -> {
wholeExp.checkNumArgs(2, null, '($name <lists...>)');
var b = wholeExp.expBuilder();
b.callSymbol("the", [b.symbol(type), b.callSymbol('Prelude.$symbol', exps)]);
};
}
typedCallMacro("zip", "zipThrow", "Array<Array<Dynamic>>");
typedCallMacro("zipKeep", "zipKeep", "Array<Array<Dynamic>>");
typedCallMacro("zipDrop", "zipDrop", "Array<Array<Dynamic>>");
typedCallMacro("zipThrow", "zipThrow", "Array<Array<Dynamic>>");
typedCallMacro("intersect", "intersect", "Array<Array<Dynamic>>");
typedCallMacro("concat", "concat", "Array<Dynamic>");
return macros;
}

View File

@@ -610,9 +610,9 @@
(function _testIntersect []
(let [intersection2d
(for i (the Array<Array<Dynamic>> (intersect (.split "abc" "") (.split "xyz" ""))) (i.join ""))
(for i (intersect (.split "abc" "") (.split "xyz" "")) (i.join ""))
intersection3d
(for i (the Array<Array<Dynamic>> (intersect (.split "abc" "") (.split "xyz" "") (.split "123" ""))) (i.join ""))]
(for i (intersect (.split "abc" "") (.split "xyz" "") (.split "123" "")) (i.join ""))]
(assert (contains intersection2d "ax"))
(assert (contains intersection2d "ay"))
(assert (contains intersection2d "az"))