Remove for/a, for*, and for/a*

This commit is contained in:
Kodi Arfer 2018-06-12 10:57:57 -07:00
parent 3256932b13
commit e1972c535f
9 changed files with 23 additions and 75 deletions

View File

@ -1442,36 +1442,6 @@ class HyASTCompiler(object):
return result
@special(["for*", (PY35, "for/a*")],
[brackets(FORM, FORM), many(notpexpr("else")), maybe(dolike("else"))])
def compile_for_expression(self, expr, root, args, body, else_expr):
target_name, iterable = args
target = self._storeize(target_name, self.compile(target_name))
ret = Result()
orel = Result()
if else_expr is not None:
for else_body in else_expr:
orel += self.compile(else_body)
orel += orel.expr_as_stmt()
ret += self.compile(iterable)
body = self._compile_branch(body)
body += body.expr_as_stmt()
node = asty.For if root == 'for*' else asty.AsyncFor
ret += node(expr,
target=target,
iter=ret.force_expr,
body=body.stmts or [asty.Pass(expr)],
orelse=orel.stmts)
ret.contains_yield = body.contains_yield
return ret
@special(["while"], [FORM, many(notpexpr("else")), maybe(dolike("else"))])
def compile_while_expression(self, expr, root, cond, body, else_expr):
cond_compiled = self.compile(cond)

View File

@ -18,7 +18,7 @@
(% "received a `%s' instead of a symbol for macro name"
(. (type name)
__name__)))))
(for* [kw '[&kwonly &kwargs]]
(for [kw '[&kwonly &kwargs]]
(if* (in kw lambda-list)
(raise (hy.errors.HyTypeError macro-name
(% "macros cannot use %s"

View File

@ -41,7 +41,7 @@
fs (tuple rfs))
(fn [&rest args &kwargs kwargs]
(setv res (first-f #* args #** kwargs))
(for* [f fs]
(for [f fs]
(setv res (f res)))
res))))
@ -79,7 +79,7 @@ If the second argument `codegen` is true, generate python code instead."
(defn distinct [coll]
"Return a generator from the original collection `coll` with no duplicates."
(setv seen (set) citer (iter coll))
(for* [val citer]
(for [val citer]
(if (not-in val seen)
(do
(yield val)
@ -159,7 +159,7 @@ Return series of accumulated sums (or other binary function results)."
(setv it (iter iterable)
total (next it))
(yield total)
(for* [element it]
(for [element it]
(setv total (func total element))
(yield total)))
@ -193,7 +193,7 @@ Return series of accumulated sums (or other binary function results)."
(defn _flatten [coll result]
(if (coll? coll)
(do (for* [b coll]
(do (for [b coll]
(_flatten b result)))
(.append result coll))
result)
@ -402,9 +402,9 @@ Raises ValueError for (not (pos? n))."
(if (not (pos? n))
(raise (ValueError "n must be positive")))
(setv citer (iter coll) skip (dec n))
(for* [val citer]
(for [val citer]
(yield val)
(for* [_ (range skip)]
(for [_ (range skip)]
(try
(next citer)
(except [StopIteration]

View File

@ -101,42 +101,20 @@ used as the result."
(setv root (check-branch branch))
(setv latest-branch root)
(for* [branch branches]
(for [branch branches]
(setv cur-branch (check-branch branch))
(.append latest-branch cur-branch)
(setv latest-branch cur-branch))
root)))
(defn _for [node args body]
(setv body (list body))
(setv belse (if (and body (isinstance (get body -1) HyExpression) (= (get body -1 0) "else"))
[(body.pop)]
[]))
(if
(odd? (len args)) (macro-error args "`for' requires an even number of args.")
(empty? args) `(do ~@body ~@belse)
(= (len args) 2) `(~node [~@args] (do ~@body) ~@belse)
(do
(setv alist (cut args 0 None 2))
`(~node [(, ~@alist) (genexpr (, ~@alist) [~@args])] (do ~@body) ~@belse))))
(defmacro for/a [args &rest body]
"Build a for/a-loop with `args` as a [element coll] bracket pair and run `body`.
Args may contain multiple pairs, in which case it executes a nested for/a-loop
in order of the given pairs."
(_for 'for/a* args body))
(defmacro -> [head &rest args]
"Thread `head` first through the `rest` of the forms.
The result of the first threaded form is inserted into the first position of
the second form, the second result is inserted into the third form, and so on."
(setv ret head)
(for* [node args]
(for [node args]
(setv ret (if (isinstance node HyExpression)
`(~(first node) ~ret ~@(rest node))
`(~node ~ret))))
@ -162,7 +140,7 @@ the second form, the second result is inserted into the third form, and so on."
The result of the first threaded form is inserted into the last position of
the second form, the second result is inserted into the third form, and so on."
(setv ret head)
(for* [node args]
(for [node args]
(setv ret (if (isinstance node HyExpression)
`(~@node ~ret)
`(~node ~ret))))
@ -203,7 +181,7 @@ the second form, the second result is inserted into the third form, and so on."
(defmacro with-gensyms [args &rest body]
"Execute `body` with `args` as bracket of names to gensym for use in macros."
(setv syms [])
(for* [arg args]
(for [arg args]
(.extend syms [arg `(gensym '~arg)]))
`(do
(setv ~@syms)
@ -218,7 +196,7 @@ the second form, the second result is inserted into the third form, and so on."
(.startswith x "g!")))
(flatten body))))
gensyms [])
(for* [sym syms]
(for [sym syms]
(.extend gensyms [sym `(gensym ~(cut sym 2))]))
`(defmacro ~name [~@args]
(setv ~@gensyms)

View File

@ -161,7 +161,7 @@
(defn get [coll key1 &rest keys]
"Access item in `coll` indexed by `key1`, with optional `keys` nested-access."
(setv coll (get coll key1))
(for* [k keys]
(for [k keys]
(setv coll (get coll k)))
coll)

View File

@ -830,13 +830,13 @@
(defn test-for-else []
"NATIVE: test for else"
(setv x 0)
(for* [a [1 2]]
(for [a [1 2]]
(setv x (+ x a))
(else (setv x (+ x 50))))
(assert (= x 53))
(setv x 0)
(for* [a [1 2]]
(for [a [1 2]]
(setv x (+ x a))
(else))
(assert (= x 3)))

View File

@ -101,7 +101,7 @@
(defn test-midtree-yield-in-for []
"NATIVE: test yielding in a for with a return"
(defn kruft-in-for []
(for* [i (range 5)]
(for [i (range 5)]
(yield i))
(+ 1 2)))
@ -117,7 +117,7 @@
(defn test-multi-yield []
"NATIVE: testing multiple yields"
(defn multi-yield []
(for* [i (range 3)]
(for [i (range 3)]
(yield i))
(yield "a")
(yield "end"))

View File

@ -13,7 +13,7 @@
(.run_until_complete (get-event-loop) (coro)))
(defn test-for/a []
(defn test-for-async []
(defn/a numbers []
(for [i [1 2]]
(yield i)))
@ -21,11 +21,11 @@
(run-coroutine
(fn/a []
(setv x 0)
(for/a [a (numbers)]
(for [:async a (numbers)]
(setv x (+ x a)))
(assert (= x 3)))))
(defn test-for/a-else []
(defn test-for-async-else []
(defn/a numbers []
(for [i [1 2]]
(yield i)))
@ -33,7 +33,7 @@
(run-coroutine
(fn/a []
(setv x 0)
(for/a [a (numbers)]
(for [:async a (numbers)]
(setv x (+ x a))
(else (setv x (+ x 50))))
(assert (= x 53)))))

View File

@ -49,7 +49,7 @@
(defn test-yield-from []
"NATIVE: testing yield from"
(defn yield-from-test []
(for* [i (range 3)]
(for [i (range 3)]
(yield i))
(yield-from [1 2 3]))
(assert (= (list (yield-from-test)) [0 1 2 1 2 3])))
@ -63,7 +63,7 @@
(yield 3)
(assert 0))
(defn yield-from-test []
(for* [i (range 3)]
(for [i (range 3)]
(yield i))
(try
(yield-from (yield-from-subgenerator-test))