variadic concat. Close #18

This commit is contained in:
2021-07-22 13:55:47 -06:00
parent 8e741bf37f
commit b676e992e7
6 changed files with 22 additions and 4 deletions

View File

@@ -56,6 +56,7 @@ class Kiss {
"print" => Symbol("Prelude.print"),
"sort" => Symbol("Prelude.sort"),
"groups" => Symbol("Prelude.groups"),
"concat" => Symbol("Prelude.concat"),
"pairs" => Symbol("Prelude.pairs"), // TODO test pairs
"reversed" => Symbol("Prelude.reversed"), // TODO test reversed
"memoize" => Symbol("Prelude.memoize"), // TODO test memoize

View File

@@ -170,6 +170,16 @@ class Prelude {
return fullGroups;
}
static function _concat(arrays:Array<Dynamic>):Array<Dynamic> {
var arr:Array<Dynamic> = arrays[0];
for (nextArr in arrays.slice(1)) {
arr = arr.concat(nextArr);
}
return arr;
}
public static var concat:Function = Reflect.makeVarArgs(_concat);
public static function zip(arrays:Array<Array<Dynamic>>, extraHandling = Drop):kiss.List<kiss.List<Dynamic>> {
var lengthsAreEqual = true;
var lengths = [for (arr in arrays) arr.length];

View File

@@ -55,6 +55,10 @@ class BasicTestCase extends Test {
_testCollect();
}
function testConcat() {
_testConcat();
}
function testVariadicAdd() {
Assert.equals(6, BasicTestCase.mySum);
}

View File

@@ -109,6 +109,9 @@
(defun myTypeCheck []
(the Int 5))
(defun _testConcat []
(Assert.equals (.toString [1 2 3 4]) (.toString (concat [1] [2 3] [4]))))
(defun _testGroups []
(Assert.equals (.toString [[1 2] [3 4]]) (.toString (groups [1 2 3 4] 2)))
(Assert.equals (.toString [[1 2 3] [4]]) (.toString (groups [1 2 3 4] 3 Keep)))

View File

@@ -22,9 +22,9 @@
(unless (and
(hcl.startsWith "#")
(= hcl.length 7)
(apply = (.concat [true]
(for c (.split (hcl.substr 1) "")
(<= 0 (.indexOf (.split "0123456789abcdef" "") c))))))
(apply = (concat [true]
(for c (.split (hcl.substr 1) "")
(<= 0 (.indexOf (.split "0123456789abcdef" "") c))))))
(return false)))
(let [ecl (dictGet pp "ecl")]
(unless (<= 0 (.indexOf (.split "amb blu brn gry grn hzl oth" " ") ecl)) (return false)))

View File

@@ -1,7 +1,7 @@
(defun path [:kiss.List<String> hillTile x0 y0 dx dy]
(if (>= y0 .length hillTile)
[]
(.concat [(.charAt (nth hillTile y0) (Math.floor (% x0 .length (nth hillTile y0))))] (path hillTile (+ x0 dx) (+ y0 dy) dx dy))))
(concat [(.charAt (nth hillTile y0) (Math.floor (% x0 .length (nth hillTile y0))))] (path hillTile (+ x0 dx) (+ y0 dy) dx dy))))
(defun pathString [:kiss.List<String> hillTile x0 y0 dx dy] (.join (path hillTile x0 y0 dx dy) ""))