Fix multi-statement 'for' with 'else' and add test cases for 'else'
This commit is contained in:
parent
62e3116529
commit
a0cb250f24
@ -97,16 +97,21 @@
|
||||
(for* [x foo]
|
||||
(for* [y bar]
|
||||
baz))"
|
||||
(setv body (list body))
|
||||
(setv lst (get body -1))
|
||||
(setv belse (if (and (isinstance lst HyExpression) (= (get lst 0) "else"))
|
||||
[(body.pop)]
|
||||
[]))
|
||||
(cond
|
||||
[(odd? (len args))
|
||||
(macro-error args "`for' requires an even number of args.")]
|
||||
[(empty? body)
|
||||
(macro-error None "`for' requires a body to evaluate")]
|
||||
[(empty? args) `(do ~@body)]
|
||||
[(= (len args) 2) `(for* [~@args] (do ~@body))]
|
||||
[(empty? args) `(do ~@body ~@belse)]
|
||||
[(= (len args) 2) `(for* [~@args] (do ~@body) ~@belse)]
|
||||
[true
|
||||
(let [[alist (cut args 0 nil 2)]]
|
||||
`(for* [(, ~@alist) (genexpr (, ~@alist) [~@args])] ~@body))]))
|
||||
`(for* [(, ~@alist) (genexpr (, ~@alist) [~@args])] (do ~@body) ~@belse))]))
|
||||
|
||||
|
||||
(defmacro -> [head &rest rest]
|
||||
|
@ -95,8 +95,10 @@
|
||||
(setv count 0)
|
||||
(for [x [1 2 3 4 5]
|
||||
y [1 2 3 4 5]]
|
||||
(setv count (+ count x y)))
|
||||
(assert (= count 150))
|
||||
(setv count (+ count x y))
|
||||
(else
|
||||
(+= count 1)))
|
||||
(assert (= count 151))
|
||||
(assert (= (list ((fn [] (for [x [[1] [2 3]] y x] (yield y)))))
|
||||
(list-comp y [x [[1] [2 3]] y x])))
|
||||
(assert (= (list ((fn [] (for [x [[1] [2 3]] y x z (range 5)] (yield z)))))
|
||||
|
Loading…
Reference in New Issue
Block a user