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
This commit is contained in:
Paul Tagliamonte 2014-01-10 22:00:30 -05:00
parent 5017e3c211
commit 2167c21120

View File

@ -45,10 +45,18 @@
(if (empty? body) (if (empty? body)
(macro-error None "`for' requires a body to evaluate")) (macro-error None "`for' requires a body to evaluate"))
(if args
`(for* [~(.pop args 0) ~(.pop args 0)] (if (= (len args) 2)
(for ~args ~@body)) ; basecase, let's just slip right in.
`(do ~@body))) `(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] (defmacro with [args &rest body]