diff --git a/projects/advent-of-code/src/year2020/Solutions.kiss b/projects/advent-of-code/src/year2020/Solutions.kiss index c4e4f205..a7e74e70 100644 --- a/projects/advent-of-code/src/year2020/Solutions.kiss +++ b/projects/advent-of-code/src/year2020/Solutions.kiss @@ -28,15 +28,12 @@ (defun :kiss.List pairWithSum [sum :kiss.List numbers] // Put the numbers in a map for random access. This gives an O(n) solution (deflocal :Map numbersMap (new Map)) - (deflocal &mut pair null) (doFor number numbers (dict-set numbersMap number (- sum number)) (let [requiredForPair (dict-get numbersMap number)] (when (numbersMap.exists requiredForPair) - (set pair (or pair [number requiredForPair]))))) - - // TODO implement early return, break - pair) + (return [number requiredForPair])))) + null) (defun :kiss.List trioWithSum [sum :kiss.List numbers] (deflocal &mut trio null) @@ -44,5 +41,5 @@ (let [requiredForTrio (- sum number) pairThatSatisfies (pairWithSum requiredForTrio numbers)] (when pairThatSatisfies - (set trio [number (nth pairThatSatisfies 0) (nth pairThatSatisfies 1)])))) - trio) \ No newline at end of file + (return [number (nth pairThatSatisfies 0) (nth pairThatSatisfies 1)])))) + null) \ No newline at end of file diff --git a/src/kiss/SpecialForms.hx b/src/kiss/SpecialForms.hx index 155f4a01..d7212d35 100644 --- a/src/kiss/SpecialForms.hx +++ b/src/kiss/SpecialForms.hx @@ -177,6 +177,11 @@ class SpecialForms { EArrayDecl([forExpr("for", wholeExp, args, k)]).withMacroPosOf(wholeExp); }; + map["return"] = (wholeExp:ReaderExp, args:Array, k:KissState) -> { + wholeExp.checkNumArgs(1, 1, '(return [value])'); + EReturn(k.convert(args[0])).withMacroPosOf(wholeExp); + }; + // TODO (case... ) for switch // Type check syntax: