From f2f38a1cf82bc0374d28acdd4334ba9c1569baa9 Mon Sep 17 00:00:00 2001 From: Matthew Wampler-Doty Date: Tue, 22 Apr 2014 16:47:42 -0700 Subject: [PATCH] Refactor for loop to use cond --- hy/core/macros.hy | 45 ++++++++++++++++++++------------------------- 1 file changed, 20 insertions(+), 25 deletions(-) diff --git a/hy/core/macros.hy b/hy/core/macros.hy index 6ae1cd7..537cfa4 100644 --- a/hy/core/macros.hy +++ b/hy/core/macros.hy @@ -31,31 +31,6 @@ [hy._compat [PY33 PY34]]) -(defmacro for [args &rest body] - "shorthand for nested for loops: - (for [x foo - y bar] - baz) -> - (for* [x foo] - (for* [y bar] - baz))" - - (if (odd? (len args)) - (macro-error args "`for' requires an even number of args.")) - - (if (empty? body) - (macro-error None "`for' requires a body to evaluate")) - - (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 [[alist (slice args 0 nil 2)]] - `(for* [(, ~@alist) (genexpr (, ~@alist) [~@args])] ~@body))))) - - (defmacro with [args &rest body] "shorthand for nested for* loops: (with [[x foo] [y bar]] baz) -> @@ -113,6 +88,26 @@ root) +(defmacro for [args &rest body] + "shorthand for nested for loops: + (for [x foo + y bar] + baz) -> + (for* [x foo] + (for* [y bar] + baz))" + (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] ~@body)] + [true + (let [[alist (slice args 0 nil 2)]] + `(for* [(, ~@alist) (genexpr (, ~@alist) [~@args])] ~@body))])) + + (defmacro -> [head &rest rest] ;; TODO: fix the docstring by someone who understands this (setv ret head)