From 59e51166fb8ac082ea749710c69653d3ff50a136 Mon Sep 17 00:00:00 2001 From: Nicolas Dandrimont Date: Sat, 2 Nov 2013 20:11:53 +0100 Subject: [PATCH] Allow calling kwapply with mixed names and dicts --- hy/core/macros.hy | 11 ++++++++--- tests/native_tests/language.hy | 16 +++++++++------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/hy/core/macros.hy b/hy/core/macros.hy index 9498b87..82eb342 100644 --- a/hy/core/macros.hy +++ b/hy/core/macros.hy @@ -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 []))))) diff --git a/tests/native_tests/language.hy b/tests/native_tests/language.hy index 6521589..0fedb67 100644 --- a/tests/native_tests/language.hy +++ b/tests/native_tests/language.hy @@ -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"