Add handling for (for []). Nice catch, @olasd.
This commit is contained in:
parent
abd0669911
commit
0f74b1ddf3
@ -46,17 +46,19 @@
|
|||||||
(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 (= (len args) 2)
|
(if (empty? args)
|
||||||
; basecase, let's just slip right in.
|
`(do ~@body)
|
||||||
`(for* [~@args] ~@body)
|
(if (= (len args) 2)
|
||||||
; otherwise, let's do some legit handling.
|
; basecase, let's just slip right in.
|
||||||
(let [[it (iter args)]
|
`(for* [~@args] ~@body)
|
||||||
[az (list (zip it it))]
|
; otherwise, let's do some legit handling.
|
||||||
[alist (list-comp (get x 0) [x az])]
|
(let [[it (iter args)]
|
||||||
[ilist (list-comp (get x 1) [x az])]]
|
[az (list (zip it it))]
|
||||||
`(do
|
[alist (list-comp (get x 0) [x az])]
|
||||||
(import itertools)
|
[ilist (list-comp (get x 1) [x az])]]
|
||||||
(for* [(, ~@alist) (itertools.product ~@ilist)] ~@body)))))
|
`(do
|
||||||
|
(import itertools)
|
||||||
|
(for* [(, ~@alist) (itertools.product ~@ilist)] ~@body))))))
|
||||||
|
|
||||||
|
|
||||||
(defmacro with [args &rest body]
|
(defmacro with [args &rest body]
|
||||||
|
@ -45,6 +45,11 @@
|
|||||||
"NATIVE: test nesting for loops harder"
|
"NATIVE: test nesting for loops harder"
|
||||||
;; This test and feature is dedicated to @nedbat.
|
;; 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
|
;; OK. This first test will ensure that the else is hooked up to the
|
||||||
;; for when we break out of it.
|
;; for when we break out of it.
|
||||||
(for [x (range 2)
|
(for [x (range 2)
|
||||||
|
Loading…
Reference in New Issue
Block a user