Update uses of the old comprehension forms

This commit is contained in:
Kodi Arfer 2018-06-07 12:18:49 -07:00
parent df4e49ec94
commit cf0dafef9b
11 changed files with 48 additions and 45 deletions

View File

@ -25,9 +25,10 @@
(setv -seen (set)) (setv -seen (set))
(defn hy-repr [obj] (defn hy-repr [obj]
(setv [f placeholder] (next (setv [f placeholder] (next
(genexpr (get -registry t) (gfor
[t (. (type obj) __mro__)] t (. (type obj) __mro__)
(in t -registry)) :if (in t -registry)
(get -registry t))
[-base-repr None])) [-base-repr None]))
(global -quoting) (global -quoting)
@ -55,18 +56,18 @@
; collections.namedtuple.) ; collections.namedtuple.)
(.format "({} {})" (.format "({} {})"
(. (type x) __name__) (. (type x) __name__)
(.join " " (genexpr (+ ":" k " " (hy-repr v)) [[k v] (zip x._fields x)]))) (.join " " (gfor [k v] (zip x._fields x) (+ ":" k " " (hy-repr v)))))
; Otherwise, print it as a regular tuple. ; Otherwise, print it as a regular tuple.
(+ "(," (if x " " "") (-cat x) ")")))) (+ "(," (if x " " "") (-cat x) ")"))))
(hy-repr-register dict :placeholder "{...}" (fn [x] (hy-repr-register dict :placeholder "{...}" (fn [x]
(setv text (.join " " (genexpr (setv text (.join " " (gfor
(+ (hy-repr k) " " (hy-repr v)) [k v] (.items x)
[[k v] (.items x)]))) (+ (hy-repr k) " " (hy-repr v)))))
(+ "{" text "}"))) (+ "{" text "}")))
(hy-repr-register HyDict :placeholder "{...}" (fn [x] (hy-repr-register HyDict :placeholder "{...}" (fn [x]
(setv text (.join " " (genexpr (setv text (.join " " (gfor
(+ (hy-repr k) " " (hy-repr v)) [k v] (partition x)
[[k v] (partition x)]))) (+ (hy-repr k) " " (hy-repr v)))))
(if (% (len x) 2) (if (% (len x) 2)
(+= text (+ " " (hy-repr (get x -1))))) (+= text (+ " " (hy-repr (get x -1)))))
(+ "{" text "}"))) (+ "{" text "}")))
@ -162,5 +163,8 @@
; Call (.repr x) using the first class of x that doesn't inherit from ; Call (.repr x) using the first class of x that doesn't inherit from
; HyObject. ; HyObject.
(.__repr__ (.__repr__
(next (genexpr t [t (. (type x) __mro__)] (not (issubclass t HyObject)))) (next (gfor
t (. (type x) __mro__)
:if (not (issubclass t HyObject))
t))
x)) x))

View File

@ -11,8 +11,8 @@
--getitem-- (fn [self n] --getitem-- (fn [self n]
"get nth item of sequence" "get nth item of sequence"
(if (hasattr n "start") (if (hasattr n "start")
(genexpr (get self x) [x (range n.start n.stop (gfor x (range n.start n.stop (or n.step 1))
(or n.step 1))]) (get self x))
(do (when (neg? n) (do (when (neg? n)
; Call (len) to force the whole ; Call (len) to force the whole
; sequence to be evaluated. ; sequence to be evaluated.

View File

@ -287,7 +287,7 @@ Return series of accumulated sums (or other binary function results)."
"Return a function applying each `fs` to args, collecting results in a list." "Return a function applying each `fs` to args, collecting results in a list."
(setv fs (+ (, f) fs)) (setv fs (+ (, f) fs))
(fn [&rest args &kwargs kwargs] (fn [&rest args &kwargs kwargs]
(list-comp (f #* args #** kwargs) [f fs]))) (lfor f fs (f #* args #** kwargs))))
(defn last [coll] (defn last [coll]
"Return last item from `coll`." "Return last item from `coll`."
@ -352,8 +352,8 @@ with overlap."
(setv (setv
step (or step n) step (or step n)
coll-clones (tee coll n) coll-clones (tee coll n)
slices (genexpr (islice (get coll-clones start) start None step) slices (gfor start (range n)
[start (range n)])) (islice (get coll-clones start) start None step)))
(if (is fillvalue -sentinel) (if (is fillvalue -sentinel)
(zip #* slices) (zip #* slices)
(zip-longest #* slices :fillvalue fillvalue))) (zip-longest #* slices :fillvalue fillvalue)))

View File

@ -38,9 +38,9 @@ be associated in pairs."
`(setv ~@(+ (if other-kvs `(setv ~@(+ (if other-kvs
[c coll] [c coll]
[]) [])
#* (genexpr [`(get ~c ~k) v] #* (gfor [k v] (partition (+ (, k1 v1)
[[k v] (partition (+ (, k1 v1) other-kvs))
other-kvs))])))) [`(get ~c ~k) v]))))
(defn _with [node args body] (defn _with [node args body]
@ -206,8 +206,8 @@ the second form, the second result is inserted into the third form, and so on."
"Like `defmacro/g!`, with automatic once-only evaluation for 'o!' params. "Like `defmacro/g!`, with automatic once-only evaluation for 'o!' params.
Such 'o!' params are available within `body` as the equivalent 'g!' symbol." Such 'o!' params are available within `body` as the equivalent 'g!' symbol."
(setv os (list-comp s [s args] (.startswith s "o!")) (setv os (lfor s args :if (.startswith s "o!") s)
gs (list-comp (HySymbol (+ "g!" (cut s 2))) [s os])) gs (lfor s os (HySymbol (+ "g!" (cut s 2)))))
`(defmacro/g! ~name ~args `(defmacro/g! ~name ~args
`(do (setv ~@(interleave ~gs ~os)) `(do (setv ~@(interleave ~gs ~os))
~@~body))) ~@~body)))

View File

@ -98,8 +98,7 @@
(defn comp-op [op a1 a-rest] (defn comp-op [op a1 a-rest]
"Helper for shadow comparison operators" "Helper for shadow comparison operators"
(if a-rest (if a-rest
(reduce (fn [x y] (and x y)) (and #* (gfor (, x y) (zip (+ (, a1) a-rest) a-rest) (op x y)))
(list-comp (op x y) [(, x y) (zip (+ (, a1) a-rest) a-rest)]))
True)) True))
(defn < [a1 &rest a-rest] (defn < [a1 &rest a-rest]
"Shadowed `<` operator perform lt comparison on `a1` by each `a-rest`." "Shadowed `<` operator perform lt comparison on `a1` by each `a-rest`."

View File

@ -109,18 +109,18 @@
`%*` and `%**` name the `&rest` and `&kwargs` parameters, respectively. `%*` and `%**` name the `&rest` and `&kwargs` parameters, respectively.
Nesting of `#%` forms is not recommended." Nesting of `#%` forms is not recommended."
(setv %symbols (set-comp a (setv %symbols (sfor a (flatten [expr])
[a (flatten [expr])] :if (and (symbol? a)
(and (symbol? a) (.startswith a '%))
(.startswith a '%)))) a))
`(fn [;; generate all %i symbols up to the maximum found in expr `(fn [;; generate all %i symbols up to the maximum found in expr
~@(genexpr (HySymbol (+ "%" (str i))) ~@(gfor i (range 1 (-> (lfor a %symbols
[i (range 1 (-> (list-comp (int (cut a 1)) :if (.isdigit (cut a 1))
[a %symbols] (int (cut a 1)))
(.isdigit (cut a 1)))
(or (, 0)) (or (, 0))
max max
inc))]) inc))
(HySymbol (+ "%" (str i))))
;; generate the &rest parameter only if '%* is present in expr ;; generate the &rest parameter only if '%* is present in expr
~@(if (in '%* %symbols) ~@(if (in '%* %symbols)
'(&rest %*)) '(&rest %*))

View File

@ -84,5 +84,5 @@ def test_eval():
assert eval_str('(.strip " fooooo ")') == 'fooooo' assert eval_str('(.strip " fooooo ")') == 'fooooo'
assert eval_str( assert eval_str(
'(if True "this is if true" "this is if false")') == "this is if true" '(if True "this is if true" "this is if false")') == "this is if true"
assert eval_str('(list-comp (pow num 2) [num (range 100)] (= (% num 2) 1))') == [ assert eval_str('(lfor num (range 100) :if (= (% num 2) 1) (pow num 2))') == [
pow(num, 2) for num in range(100) if num % 2 == 1] pow(num, 2) for num in range(100) if num % 2 == 1]

View File

@ -191,9 +191,9 @@
(assert (= s "az")) (assert (= s "az"))
(assert (= (list ((fn [] (for [x [[1] [2 3]] y x] (yield y))))) (assert (= (list ((fn [] (for [x [[1] [2 3]] y x] (yield y)))))
(list-comp y [x [[1] [2 3]] y x]))) (lfor x [[1] [2 3]] y x y)))
(assert (= (list ((fn [] (for [x [[1] [2 3]] y x z (range 5)] (yield z))))) (assert (= (list ((fn [] (for [x [[1] [2 3]] y x z (range 5)] (yield z)))))
(list-comp z [x [[1] [2 3]] y x z (range 5)])))) (lfor x [[1] [2 3]] y x z (range 5) z))))
(defn test-nasty-for-nesting [] (defn test-nasty-for-nesting []

View File

@ -144,11 +144,11 @@
(setv x {1 2 3 [4 5] 6 7}) (setv x {1 2 3 [4 5] 6 7})
(setv (get x 3 1) x) (setv (get x 3 1) x)
(assert (in (hy-repr x) (list-comp (assert (in (hy-repr x) (lfor
; The ordering of a dictionary isn't guaranteed, so we need ; The ordering of a dictionary isn't guaranteed, so we need
; to check for all possible orderings. ; to check for all possible orderings.
(+ "{" (.join " " p) "}") p (permutations ["1 2" "3 [4 {...}]" "6 7"])
[p (permutations ["1 2" "3 [4 {...}]" "6 7"])])))) (+ "{" (.join " " p) "}")))))
(defn test-matchobject [] (defn test-matchobject []
(import re) (import re)

View File

@ -110,7 +110,7 @@
((wraps func) ((wraps func)
(fn [&rest args &kwargs kwargs] (fn [&rest args &kwargs kwargs]
(func #* (map inc args) (func #* (map inc args)
#** (dict-comp k (inc v) [[k v] (.items kwargs)]))))) #** (dfor [k v] (.items kwargs) [k (inc v)])))))
#@(increment-arguments #@(increment-arguments
(defn foo [&rest args &kwargs kwargs] (defn foo [&rest args &kwargs kwargs]

View File

@ -29,10 +29,10 @@ Call me Ishmael. Some years ago—never mind how long precisely—having little
(setv myset #{4 5 6}) (setv myset #{4 5 6})
(setv mydict {7 8 9 900 10 15}) (setv mydict {7 8 9 900 10 15})
(setv mylistcomp (list-comp x [x (range 10)] (% x 2))) (setv mylistcomp (lfor x (range 10) :if (% x 2) x))
(setv mysetcomp (set-comp x [x (range 5)] (not (% x 2)))) (setv mysetcomp (sfor x (range 5) :if (not (% x 2)) x))
(setv mydictcomp (dict-comp k (.upper k) [k "abcde"] (!= k "c"))) (setv mydictcomp (dfor k "abcde" :if (!= k "c") [k (.upper k)]))
(setv mygenexpr (genexpr x [x (cycle [1 2 3])] (!= x 2))) (setv mygenexpr (gfor x (cycle [1 2 3]) :if (!= x 2) x))
(setv attr-ref str.upper) (setv attr-ref str.upper)
(setv subscript (get "hello" 2)) (setv subscript (get "hello" 2))