Add handling for (for []). Nice catch, @olasd.

This commit is contained in:
Paul Tagliamonte 2014-01-10 22:29:03 -05:00
parent abd0669911
commit 0f74b1ddf3
2 changed files with 18 additions and 11 deletions

View File

@ -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]

View File

@ -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)