4 ms·
The reduce(..., True) function kind of bothers me in this code. Python has the all(...) function to achieve that functionality.
by _asummers 11y ago
The reduce(..., True) function kind of bothers me in this code. Python has the all(...) function to achieve that functionality.
- evmar 11y agoIt bugged me too. Here's the equivalent logic without even using all (which is definitely the right approach): for rule in self.rules: if not rule.canPost: return False return True That's the same number of lines of code, doesn't require two imports, and is way easier to follow. I love functional programming but Python (intentionally, see reduce in py3) is not a language for doing it.
- jacobolus 11y agoreturn all(rule.canPost for rule in self.rules)
- semisight 11y agoNoticed the same thing, wondered if I was missing some niche side effect here. TBH, I've noticed there's almost never a reason for a general reduce() in Python (to my usual dismay, as an ex-Clojure programmer). It does result in clearer, less clever code for the most part.
- openasocket 11y agowhat bugs me is that his method means every single rule is always evaluated: no short-circuiting! So the code to sort the rules by weights, which is supposed to put stricter rules first to cut down on computing time, is completely useless.