flake8-pie
flake8-pie copied to clipboard
`prefer-sum-counting`
# 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
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)
see: https://github.com/sbdchd/flake8-pie/issues/77#issuecomment-836779943