Update uses of the old comprehension forms
This commit is contained in:
parent
df4e49ec94
commit
cf0dafef9b
@ -25,9 +25,10 @@
|
||||
(setv -seen (set))
|
||||
(defn hy-repr [obj]
|
||||
(setv [f placeholder] (next
|
||||
(genexpr (get -registry t)
|
||||
[t (. (type obj) __mro__)]
|
||||
(in t -registry))
|
||||
(gfor
|
||||
t (. (type obj) __mro__)
|
||||
:if (in t -registry)
|
||||
(get -registry t))
|
||||
[-base-repr None]))
|
||||
|
||||
(global -quoting)
|
||||
@ -55,18 +56,18 @@
|
||||
; collections.namedtuple.)
|
||||
(.format "({} {})"
|
||||
(. (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.
|
||||
(+ "(," (if x " " "") (-cat x) ")"))))
|
||||
(hy-repr-register dict :placeholder "{...}" (fn [x]
|
||||
(setv text (.join " " (genexpr
|
||||
(+ (hy-repr k) " " (hy-repr v))
|
||||
[[k v] (.items x)])))
|
||||
(setv text (.join " " (gfor
|
||||
[k v] (.items x)
|
||||
(+ (hy-repr k) " " (hy-repr v)))))
|
||||
(+ "{" text "}")))
|
||||
(hy-repr-register HyDict :placeholder "{...}" (fn [x]
|
||||
(setv text (.join " " (genexpr
|
||||
(+ (hy-repr k) " " (hy-repr v))
|
||||
[[k v] (partition x)])))
|
||||
(setv text (.join " " (gfor
|
||||
[k v] (partition x)
|
||||
(+ (hy-repr k) " " (hy-repr v)))))
|
||||
(if (% (len x) 2)
|
||||
(+= text (+ " " (hy-repr (get x -1)))))
|
||||
(+ "{" text "}")))
|
||||
@ -162,5 +163,8 @@
|
||||
; Call (.repr x) using the first class of x that doesn't inherit from
|
||||
; HyObject.
|
||||
(.__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))
|
||||
|
@ -11,8 +11,8 @@
|
||||
--getitem-- (fn [self n]
|
||||
"get nth item of sequence"
|
||||
(if (hasattr n "start")
|
||||
(genexpr (get self x) [x (range n.start n.stop
|
||||
(or n.step 1))])
|
||||
(gfor x (range n.start n.stop (or n.step 1))
|
||||
(get self x))
|
||||
(do (when (neg? n)
|
||||
; Call (len) to force the whole
|
||||
; sequence to be evaluated.
|
||||
|
@ -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."
|
||||
(setv fs (+ (, f) fs))
|
||||
(fn [&rest args &kwargs kwargs]
|
||||
(list-comp (f #* args #** kwargs) [f fs])))
|
||||
(lfor f fs (f #* args #** kwargs))))
|
||||
|
||||
(defn last [coll]
|
||||
"Return last item from `coll`."
|
||||
@ -352,8 +352,8 @@ with overlap."
|
||||
(setv
|
||||
step (or step n)
|
||||
coll-clones (tee coll n)
|
||||
slices (genexpr (islice (get coll-clones start) start None step)
|
||||
[start (range n)]))
|
||||
slices (gfor start (range n)
|
||||
(islice (get coll-clones start) start None step)))
|
||||
(if (is fillvalue -sentinel)
|
||||
(zip #* slices)
|
||||
(zip-longest #* slices :fillvalue fillvalue)))
|
||||
|
@ -38,9 +38,9 @@ be associated in pairs."
|
||||
`(setv ~@(+ (if other-kvs
|
||||
[c coll]
|
||||
[])
|
||||
#* (genexpr [`(get ~c ~k) v]
|
||||
[[k v] (partition (+ (, k1 v1)
|
||||
other-kvs))]))))
|
||||
#* (gfor [k v] (partition (+ (, k1 v1)
|
||||
other-kvs))
|
||||
[`(get ~c ~k) v]))))
|
||||
|
||||
|
||||
(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.
|
||||
|
||||
Such 'o!' params are available within `body` as the equivalent 'g!' symbol."
|
||||
(setv os (list-comp s [s args] (.startswith s "o!"))
|
||||
gs (list-comp (HySymbol (+ "g!" (cut s 2))) [s os]))
|
||||
(setv os (lfor s args :if (.startswith s "o!") s)
|
||||
gs (lfor s os (HySymbol (+ "g!" (cut s 2)))))
|
||||
`(defmacro/g! ~name ~args
|
||||
`(do (setv ~@(interleave ~gs ~os))
|
||||
~@~body)))
|
||||
|
@ -98,8 +98,7 @@
|
||||
(defn comp-op [op a1 a-rest]
|
||||
"Helper for shadow comparison operators"
|
||||
(if a-rest
|
||||
(reduce (fn [x y] (and x y))
|
||||
(list-comp (op x y) [(, x y) (zip (+ (, a1) a-rest) a-rest)]))
|
||||
(and #* (gfor (, x y) (zip (+ (, a1) a-rest) a-rest) (op x y)))
|
||||
True))
|
||||
(defn < [a1 &rest a-rest]
|
||||
"Shadowed `<` operator perform lt comparison on `a1` by each `a-rest`."
|
||||
|
@ -109,18 +109,18 @@
|
||||
`%*` and `%**` name the `&rest` and `&kwargs` parameters, respectively.
|
||||
|
||||
Nesting of `#%` forms is not recommended."
|
||||
(setv %symbols (set-comp a
|
||||
[a (flatten [expr])]
|
||||
(and (symbol? a)
|
||||
(.startswith a '%))))
|
||||
(setv %symbols (sfor a (flatten [expr])
|
||||
:if (and (symbol? a)
|
||||
(.startswith a '%))
|
||||
a))
|
||||
`(fn [;; generate all %i symbols up to the maximum found in expr
|
||||
~@(genexpr (HySymbol (+ "%" (str i)))
|
||||
[i (range 1 (-> (list-comp (int (cut a 1))
|
||||
[a %symbols]
|
||||
(.isdigit (cut a 1)))
|
||||
(or (, 0))
|
||||
max
|
||||
inc))])
|
||||
~@(gfor i (range 1 (-> (lfor a %symbols
|
||||
:if (.isdigit (cut a 1))
|
||||
(int (cut a 1)))
|
||||
(or (, 0))
|
||||
max
|
||||
inc))
|
||||
(HySymbol (+ "%" (str i))))
|
||||
;; generate the &rest parameter only if '%* is present in expr
|
||||
~@(if (in '%* %symbols)
|
||||
'(&rest %*))
|
||||
|
@ -84,5 +84,5 @@ def test_eval():
|
||||
assert eval_str('(.strip " fooooo ")') == 'fooooo'
|
||||
assert eval_str(
|
||||
'(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]
|
||||
|
@ -191,9 +191,9 @@
|
||||
(assert (= s "az"))
|
||||
|
||||
(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)))))
|
||||
(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 []
|
||||
|
@ -144,11 +144,11 @@
|
||||
|
||||
(setv x {1 2 3 [4 5] 6 7})
|
||||
(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
|
||||
; 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 []
|
||||
(import re)
|
||||
|
@ -110,7 +110,7 @@
|
||||
((wraps func)
|
||||
(fn [&rest args &kwargs kwargs]
|
||||
(func #* (map inc args)
|
||||
#** (dict-comp k (inc v) [[k v] (.items kwargs)])))))
|
||||
#** (dfor [k v] (.items kwargs) [k (inc v)])))))
|
||||
|
||||
#@(increment-arguments
|
||||
(defn foo [&rest args &kwargs kwargs]
|
||||
|
@ -29,10 +29,10 @@ Call me Ishmael. Some years ago—never mind how long precisely—having little
|
||||
(setv myset #{4 5 6})
|
||||
(setv mydict {7 8 9 900 10 15})
|
||||
|
||||
(setv mylistcomp (list-comp x [x (range 10)] (% x 2)))
|
||||
(setv mysetcomp (set-comp x [x (range 5)] (not (% x 2))))
|
||||
(setv mydictcomp (dict-comp k (.upper k) [k "abcde"] (!= k "c")))
|
||||
(setv mygenexpr (genexpr x [x (cycle [1 2 3])] (!= x 2)))
|
||||
(setv mylistcomp (lfor x (range 10) :if (% x 2) x))
|
||||
(setv mysetcomp (sfor x (range 5) :if (not (% x 2)) x))
|
||||
(setv mydictcomp (dfor k "abcde" :if (!= k "c") [k (.upper k)]))
|
||||
(setv mygenexpr (gfor x (cycle [1 2 3]) :if (!= x 2) x))
|
||||
|
||||
(setv attr-ref str.upper)
|
||||
(setv subscript (get "hello" 2))
|
||||
|
Loading…
Reference in New Issue
Block a user