flake8-pie icon indicating copy to clipboard operation
flake8-pie copied to clipboard

`prefer-sum-counting`

Open sbdchd opened this issue 4 years ago • 2 comments

# err
foo = len([x for x in bar if x.buzz > bar])
foo = len([n for n in buzz if n.bar is not True])
foo = len([x for x, bar in results if bar])

# ok
foo = sum(x.buzz > bar for x in bar)
foo = sum(n.bar is not True for n in buzz)
foo = sum(bool(bar) for _, bar in results)

sbdchd avatar May 07 '21 14:05 sbdchd

@sbdchd

Couldn't we also call len() on a generator instead. Why do we need to use sum()?

# err
foo = len([x for x in bar if x.buzz > bar])
foo = len([n for n in buzz if n.bar is not True])
foo = len([x for x, bar in results if bar])


# ok
foo = len(x for x in bar if x.buzz > bar)
foo = len(n for n in buzz if n.bar is not True)
foo = len(x for x, bar in results if bar)

chdsbd avatar May 10 '21 14:05 chdsbd

see: https://github.com/sbdchd/flake8-pie/issues/77#issuecomment-836779943

sbdchd avatar May 10 '21 14:05 sbdchd