Allow calling kwapply with mixed names and dicts

This commit is contained in:
Nicolas Dandrimont 2013-11-02 20:11:53 +01:00
parent 83bb1513db
commit 59e51166fb
2 changed files with 17 additions and 10 deletions

View File

@ -122,10 +122,15 @@
"Use a dictionary as keyword arguments"
(let [[-fun (car call)]
[-args (cdr call)]
[-okwargs kwargs]]
[-okwargs `[(list (.items ~kwargs))]]]
(while (= -fun "kwapply") ;; join any further kw
(setv -okwargs (+ (car (cdr -args)) -okwargs))
(if (not (= (len -args) 2))
(macro-error
call
(.format "Trying to call nested kwapply with {0} args instead of 2"
(len -args))))
(.insert -okwargs 0 `(list (.items ~(car (cdr -args)))))
(setv -fun (car (car -args)))
(setv -args (cdr (car -args))))
`(apply ~-fun [~@-args] ~-okwargs)))
`(apply ~-fun [~@-args] (dict (sum ~-okwargs [])))))

View File

@ -153,14 +153,16 @@
(setv mydict {"one" "three"})
(assert (= (kwapply (kwtest) mydict) mydict))
(assert (= (kwapply (kwtest) ((fn [] {"one" "two"}))) {"one" "two"}))
(assert (= (kwapply
(kwapply
(kwapply
(kwapply (kwtest) {"x" 4})
{"x" 8})
{"x" (- 3 2) "y" 2})
(assert (= (kwapply
(kwapply
(kwapply
(kwapply
(kwapply (kwtest) {"x" 4})
mydict)
{"x" 8})
{"x" (- 3 2) "y" 2})
{"y" 5 "z" 3})
{"x" 1 "y" 5 "z" 3})))
{"x" 1 "y" 5 "z" 3 "one" "three"})))
(defn test-apply []
"NATIVE: test working with args and functions"