dict-get, dict-set, set-nth
This commit is contained in:
@@ -30,9 +30,8 @@
|
|||||||
(deflocal :Map<Int,Int> numbersMap (new Map))
|
(deflocal :Map<Int,Int> numbersMap (new Map))
|
||||||
(deflocal &mut pair null)
|
(deflocal &mut pair null)
|
||||||
(doFor number numbers
|
(doFor number numbers
|
||||||
// TODO implement dict-set, dict-get, set-nth, and use them
|
(dict-set numbersMap number (- sum number))
|
||||||
(set (nth numbersMap number) (- sum number))
|
(let [requiredForPair (dict-get numbersMap number)]
|
||||||
(let [requiredForPair (nth numbersMap number)]
|
|
||||||
(when (numbersMap.exists requiredForPair)
|
(when (numbersMap.exists requiredForPair)
|
||||||
(set pair (or pair [number requiredForPair])))))
|
(set pair (or pair [number requiredForPair])))))
|
||||||
|
|
||||||
|
|||||||
@@ -100,6 +100,21 @@ class Macros {
|
|||||||
]).withPosOf(wholeExp);
|
]).withPosOf(wholeExp);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function arraySet(wholeExp:ReaderExp, exps:Array<ReaderExp>, k:KissState) {
|
||||||
|
return CallExp(Symbol("set").withPosOf(wholeExp), [
|
||||||
|
CallExp(Symbol("nth").withPosOf(wholeExp), [exps[0], exps[1]]).withPosOf(wholeExp),
|
||||||
|
exps[2]
|
||||||
|
]).withPosOf(wholeExp);
|
||||||
|
}
|
||||||
|
macros["set-nth"] = (wholeExp:ReaderExp, exps:Array<ReaderExp>, k:KissState) -> {
|
||||||
|
wholeExp.checkNumArgs(3, 3, "(set-nth [list] [index] [value])");
|
||||||
|
arraySet(wholeExp, exps, k);
|
||||||
|
};
|
||||||
|
macros["dict-set"] = (wholeExp:ReaderExp, exps:Array<ReaderExp>, k:KissState) -> {
|
||||||
|
wholeExp.checkNumArgs(3, 3, "(dict-set [dict] [key] [value])");
|
||||||
|
arraySet(wholeExp, exps, k);
|
||||||
|
};
|
||||||
|
|
||||||
// Under the hood, (defmacrofun ...) defines a runtime function that accepts Quote arguments and a special form that quotes the arguments to macrofun calls
|
// Under the hood, (defmacrofun ...) defines a runtime function that accepts Quote arguments and a special form that quotes the arguments to macrofun calls
|
||||||
macros["defmacrofun"] = (wholeExp:ReaderExp, exps:Array<ReaderExp>, k:KissState) -> {
|
macros["defmacrofun"] = (wholeExp:ReaderExp, exps:Array<ReaderExp>, k:KissState) -> {
|
||||||
wholeExp.checkNumArgs(3, null, "(defmacrofun [name] [args] [body...])");
|
wholeExp.checkNumArgs(3, null, "(defmacrofun [name] [args] [body...])");
|
||||||
|
|||||||
@@ -25,9 +25,16 @@ class SpecialForms {
|
|||||||
EBlock([for (bodyExp in args) k.convert(bodyExp)]).withMacroPosOf(wholeExp);
|
EBlock([for (bodyExp in args) k.convert(bodyExp)]).withMacroPosOf(wholeExp);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function arrayAccess(wholeExp:ReaderExp, args:Array<ReaderExp>, k:KissState) {
|
||||||
|
return EArray(k.convert(args[0]), k.convert(args[1])).withMacroPosOf(wholeExp);
|
||||||
|
};
|
||||||
map["nth"] = (wholeExp:ReaderExp, args:Array<ReaderExp>, k:KissState) -> {
|
map["nth"] = (wholeExp:ReaderExp, args:Array<ReaderExp>, k:KissState) -> {
|
||||||
wholeExp.checkNumArgs(2, 2, "(nth [list] [idx])");
|
wholeExp.checkNumArgs(2, 2, "(nth [list] [idx])");
|
||||||
EArray(k.convert(args[0]), k.convert(args[1])).withMacroPosOf(wholeExp);
|
arrayAccess(wholeExp, args, k);
|
||||||
|
};
|
||||||
|
map["dict-get"] = (wholeExp:ReaderExp, args:Array<ReaderExp>, k:KissState) -> {
|
||||||
|
wholeExp.checkNumArgs(2, 2, "(dict-get [dict] [key])");
|
||||||
|
arrayAccess(wholeExp, args, k);
|
||||||
};
|
};
|
||||||
|
|
||||||
function makeQuickNth(idx:Int, name:String) {
|
function makeQuickNth(idx:Int, name:String) {
|
||||||
|
|||||||
Reference in New Issue
Block a user