(gen)
generator expressions
clojure
(gen body?)Where:
- body: expression yielded by the generator (the
xin(x for x in items)) *
Matching:
- simple generators, e.g.
(x for x in items) - filtered generators, e.g.
(f(x) for x in items if x)
Behavior
- Does not match list comprehensions like
[x for x in items].
Examples
Find generators that yield identifiers directly.
clojure
(gen (id x))Selects in lines { 1 } but not in { 2 }:
python
(x for x in items)
(y for y in items)Locate sum calls that use a generator expression.
clojure
(call (id "sum") (gen))Selects in lines { 1 } but not in { 2 }:
python
total = sum(x for x in items)
total = sum([x for x in items])Find generators that call len on each item.
clojure
(gen (call len))Selects in lines { 1 } but not in { 2 }:
python
sum(len(x) for x in items)
sum(x for x in items)