Allow 'for' and 'cond' to take a multi-expression body (closes #868)
This commit is contained in:
parent
54fb0102aa
commit
6c076f76f7
@ -267,11 +267,10 @@ is only called on every other value in the list.
|
||||
;; collection is a list of numerical values
|
||||
|
||||
(for [x collection]
|
||||
(do
|
||||
(side-effect1 x)
|
||||
(if (% x 2)
|
||||
(continue))
|
||||
(side-effect2 x)))
|
||||
(side-effect2 x))
|
||||
|
||||
|
||||
dict-comp
|
||||
|
@ -73,10 +73,11 @@
|
||||
"check `cond` branch for validity, return the corresponding `if` expr"
|
||||
(if (not (= (type branch) HyList))
|
||||
(macro-error branch "cond branches need to be a list"))
|
||||
(if (!= (len branch) 2)
|
||||
(macro-error branch "cond branches need two items: a test and a code branch"))
|
||||
(setv (, test thebranch) branch)
|
||||
`(if ~test ~thebranch))
|
||||
(if (< (len branch) 2)
|
||||
(macro-error branch "cond branches need at least two items: a test and one or more code branches"))
|
||||
(setv test (car branch))
|
||||
(setv thebranch (cdr branch))
|
||||
`(if ~test (do ~@thebranch)))
|
||||
|
||||
(setv root (check-branch branch))
|
||||
(setv latest-branch root)
|
||||
@ -102,7 +103,7 @@
|
||||
[(empty? body)
|
||||
(macro-error None "`for' requires a body to evaluate")]
|
||||
[(empty? args) `(do ~@body)]
|
||||
[(= (len args) 2) `(for* [~@args] ~@body)]
|
||||
[(= (len args) 2) `(for* [~@args] (do ~@body))]
|
||||
[true
|
||||
(let [[alist (cut args 0 nil 2)]]
|
||||
`(for* [(, ~@alist) (genexpr (, ~@alist) [~@args])] ~@body))]))
|
||||
|
@ -32,4 +32,4 @@
|
||||
|
||||
(defn test-macroexpand-all []
|
||||
(assert (= (macroexpand-all '(with [a b c] (for [d c] foo)))
|
||||
'(with* [a] (with* [b] (with* [c] (do (for* [d c] foo))))))))
|
||||
'(with* [a] (with* [b] (with* [c] (do (for* [d c] (do foo)))))))))
|
||||
|
@ -86,10 +86,12 @@
|
||||
|
||||
(defn test-for-loop []
|
||||
"NATIVE: test for loops"
|
||||
(setv count 0)
|
||||
(setv count1 0 count2 0)
|
||||
(for [x [1 2 3 4 5]]
|
||||
(setv count (+ count x)))
|
||||
(assert (= count 15))
|
||||
(setv count1 (+ count1 x))
|
||||
(setv count2 (+ count2 x)))
|
||||
(assert (= count1 15))
|
||||
(assert (= count2 15))
|
||||
(setv count 0)
|
||||
(for [x [1 2 3 4 5]
|
||||
y [1 2 3 4 5]]
|
||||
@ -224,7 +226,7 @@
|
||||
"NATIVE: test if cond sorta works."
|
||||
(cond
|
||||
[(= 1 2) (assert (is true false))]
|
||||
[(is null null) (assert (is true true))]))
|
||||
[(is null null) (setv x true) (assert x)]))
|
||||
|
||||
|
||||
(defn test-index []
|
||||
|
Loading…
Reference in New Issue
Block a user