(unless)
This commit is contained in:
@@ -1,17 +1,16 @@
|
|||||||
(defun main []
|
(defun main []
|
||||||
// Day 1
|
// Day 1
|
||||||
// TODO implement unless
|
|
||||||
(let [p (pairWithSum 2020 [1721 979 366 299 675 1456])]
|
(let [p (pairWithSum 2020 [1721 979 366 299 675 1456])]
|
||||||
(when !(and (has p 1721) (has p 299))
|
(unless (and (has p 1721) (has p 299))
|
||||||
(throw "pairWithSum is broken")))
|
(throw "pairWithSum is broken")))
|
||||||
(let [[a b] (pairWithSum 2020 (readInts "src/year2020/inputs/day1-1.txt"))]
|
(let [[a b] (pairWithSum 2020 (readInts "src/year2020/inputs/day1-1.txt"))]
|
||||||
(when !(= 545379 (* a b))
|
(unless (= 545379 (* a b))
|
||||||
(throw "pairWithSum is broken")))
|
(throw "pairWithSum is broken")))
|
||||||
(let [t (trioWithSum 2020 [1721 979 366 299 675 1456])]
|
(let [t (trioWithSum 2020 [1721 979 366 299 675 1456])]
|
||||||
(when !(and (has t 675) (has t 366) (has t 979))
|
(unless (and (has t 675) (has t 366) (has t 979))
|
||||||
(throw "trioWithSum is broken")))
|
(throw "trioWithSum is broken")))
|
||||||
(let [[a b c] (trioWithSum 2020 (readInts "src/year2020/inputs/day1-1.txt"))]
|
(let [[a b c] (trioWithSum 2020 (readInts "src/year2020/inputs/day1-1.txt"))]
|
||||||
(when !(= 257778836 (* a b c))
|
(unless (= 257778836 (* a b c))
|
||||||
(throw "trioWithSum is broken"))))
|
(throw "trioWithSum is broken"))))
|
||||||
|
|
||||||
(defun readLines [file]
|
(defun readLines [file]
|
||||||
|
|||||||
@@ -48,13 +48,20 @@ class Macros {
|
|||||||
|
|
||||||
macros["_eq"] = foldMacro("Prelude.areEqual");
|
macros["_eq"] = foldMacro("Prelude.areEqual");
|
||||||
|
|
||||||
macros["when"] = (wholeExp:ReaderExp, args:Array<ReaderExp>, k) -> {
|
function bodyIf(formName:String, negated:Bool, wholeExp:ReaderExp, args:Array<ReaderExp>, k) {
|
||||||
wholeExp.checkNumArgs(2, null, "(when [condition] [body...])");
|
wholeExp.checkNumArgs(2, null, '($formName [condition] [body...])');
|
||||||
CallExp(Symbol("if").withPosOf(wholeExp), [
|
var condition = if (negated) {
|
||||||
args[0],
|
CallExp(Symbol("not").withPosOf(args[0]), [args[0]]).withPosOf(args[0]);
|
||||||
|
} else {
|
||||||
|
args[0];
|
||||||
|
}
|
||||||
|
return CallExp(Symbol("if").withPosOf(wholeExp), [
|
||||||
|
condition,
|
||||||
CallExp(Symbol("begin").withPosOf(wholeExp), args.slice(1)).withPosOf(wholeExp)
|
CallExp(Symbol("begin").withPosOf(wholeExp), args.slice(1)).withPosOf(wholeExp)
|
||||||
]).withPos(args[0].pos);
|
]).withPosOf(wholeExp);
|
||||||
};
|
}
|
||||||
|
macros["when"] = bodyIf.bind("when", false);
|
||||||
|
macros["unless"] = bodyIf.bind("unless", true);
|
||||||
|
|
||||||
macros["cond"] = cond;
|
macros["cond"] = cond;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user