Allow 'for' and 'cond' to take a multi-expression body (closes #868)

This commit is contained in:
Ryan Gonzalez 2015-07-30 17:31:57 -05:00 committed by Gergely Nagy
parent 54fb0102aa
commit 6c076f76f7
4 changed files with 19 additions and 17 deletions

View File

@ -267,11 +267,10 @@ is only called on every other value in the list.
;; collection is a list of numerical values ;; collection is a list of numerical values
(for [x collection] (for [x collection]
(do (side-effect1 x)
(side-effect1 x) (if (% x 2)
(if (% x 2) (continue))
(continue)) (side-effect2 x))
(side-effect2 x)))
dict-comp dict-comp

View File

@ -73,10 +73,11 @@
"check `cond` branch for validity, return the corresponding `if` expr" "check `cond` branch for validity, return the corresponding `if` expr"
(if (not (= (type branch) HyList)) (if (not (= (type branch) HyList))
(macro-error branch "cond branches need to be a list")) (macro-error branch "cond branches need to be a list"))
(if (!= (len branch) 2) (if (< (len branch) 2)
(macro-error branch "cond branches need two items: a test and a code branch")) (macro-error branch "cond branches need at least two items: a test and one or more code branches"))
(setv (, test thebranch) branch) (setv test (car branch))
`(if ~test ~thebranch)) (setv thebranch (cdr branch))
`(if ~test (do ~@thebranch)))
(setv root (check-branch branch)) (setv root (check-branch branch))
(setv latest-branch root) (setv latest-branch root)
@ -96,14 +97,14 @@
(for* [x foo] (for* [x foo]
(for* [y bar] (for* [y bar]
baz))" baz))"
(cond (cond
[(odd? (len args)) [(odd? (len args))
(macro-error args "`for' requires an even number of args.")] (macro-error args "`for' requires an even number of args.")]
[(empty? body) [(empty? body)
(macro-error None "`for' requires a body to evaluate")] (macro-error None "`for' requires a body to evaluate")]
[(empty? args) `(do ~@body)] [(empty? args) `(do ~@body)]
[(= (len args) 2) `(for* [~@args] ~@body)] [(= (len args) 2) `(for* [~@args] (do ~@body))]
[true [true
(let [[alist (cut args 0 nil 2)]] (let [[alist (cut args 0 nil 2)]]
`(for* [(, ~@alist) (genexpr (, ~@alist) [~@args])] ~@body))])) `(for* [(, ~@alist) (genexpr (, ~@alist) [~@args])] ~@body))]))

View File

@ -32,4 +32,4 @@
(defn test-macroexpand-all [] (defn test-macroexpand-all []
(assert (= (macroexpand-all '(with [a b c] (for [d c] foo))) (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)))))))))

View File

@ -86,10 +86,12 @@
(defn test-for-loop [] (defn test-for-loop []
"NATIVE: test for loops" "NATIVE: test for loops"
(setv count 0) (setv count1 0 count2 0)
(for [x [1 2 3 4 5]] (for [x [1 2 3 4 5]]
(setv count (+ count x))) (setv count1 (+ count1 x))
(assert (= count 15)) (setv count2 (+ count2 x)))
(assert (= count1 15))
(assert (= count2 15))
(setv count 0) (setv count 0)
(for [x [1 2 3 4 5] (for [x [1 2 3 4 5]
y [1 2 3 4 5]] y [1 2 3 4 5]]
@ -224,7 +226,7 @@
"NATIVE: test if cond sorta works." "NATIVE: test if cond sorta works."
(cond (cond
[(= 1 2) (assert (is true false))] [(= 1 2) (assert (is true false))]
[(is null null) (assert (is true true))])) [(is null null) (setv x true) (assert x)]))
(defn test-index [] (defn test-index []