From 0f74b1ddf3d5268fcefde26aa4b8c12f9e90620b Mon Sep 17 00:00:00 2001 From: Paul Tagliamonte Date: Fri, 10 Jan 2014 22:29:03 -0500 Subject: [PATCH] Add handling for (for []). Nice catch, @olasd. --- hy/core/macros.hy | 24 +++++++++++++----------- tests/native_tests/language.hy | 5 +++++ 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/hy/core/macros.hy b/hy/core/macros.hy index 7c10e70..f6b30af 100644 --- a/hy/core/macros.hy +++ b/hy/core/macros.hy @@ -46,17 +46,19 @@ (if (empty? body) (macro-error None "`for' requires a body to evaluate")) - (if (= (len args) 2) - ; basecase, let's just slip right in. - `(for* [~@args] ~@body) - ; otherwise, let's do some legit handling. - (let [[it (iter args)] - [az (list (zip it it))] - [alist (list-comp (get x 0) [x az])] - [ilist (list-comp (get x 1) [x az])]] - `(do - (import itertools) - (for* [(, ~@alist) (itertools.product ~@ilist)] ~@body))))) + (if (empty? args) + `(do ~@body) + (if (= (len args) 2) + ; basecase, let's just slip right in. + `(for* [~@args] ~@body) + ; otherwise, let's do some legit handling. + (let [[it (iter args)] + [az (list (zip it it))] + [alist (list-comp (get x 0) [x az])] + [ilist (list-comp (get x 1) [x az])]] + `(do + (import itertools) + (for* [(, ~@alist) (itertools.product ~@ilist)] ~@body)))))) (defmacro with [args &rest body] diff --git a/tests/native_tests/language.hy b/tests/native_tests/language.hy index 268c6f2..f954909 100644 --- a/tests/native_tests/language.hy +++ b/tests/native_tests/language.hy @@ -45,6 +45,11 @@ "NATIVE: test nesting for loops harder" ;; This test and feature is dedicated to @nedbat. + ;; let's ensure empty iterating is an implicit do + (setv t 0) + (for [] (setv t 1)) + (assert (= t 1)) + ;; OK. This first test will ensure that the else is hooked up to the ;; for when we break out of it. (for [x (range 2)