From 2167c211203fc1d43cb3a9d8c0d10598f7c55388 Mon Sep 17 00:00:00 2001 From: Paul Tagliamonte Date: Fri, 10 Jan 2014 22:00:30 -0500 Subject: [PATCH] Adjust (for) to just use itertools.product This is to avoid nesting in for loops, helping make clear what (break) and (else) do. This commit is hereby dedicated to @nedbat --- hy/core/macros.hy | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/hy/core/macros.hy b/hy/core/macros.hy index cdc5d17..7c10e70 100644 --- a/hy/core/macros.hy +++ b/hy/core/macros.hy @@ -45,10 +45,18 @@ (if (empty? body) (macro-error None "`for' requires a body to evaluate")) - (if args - `(for* [~(.pop args 0) ~(.pop args 0)] - (for ~args ~@body)) - `(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]