3 ms·
If you take the awful step of naming things, comprehensions are easy enough to chain. That is (in untested python...) parts=s.split('+') keep=(s for s
by maxerickson 13y ago
If you take the awful step of naming things, comprehensions are easy enough to chain. That is (in untested python...)
parts=s.split('+')
keep=(s for s in parts if s)
ints=(int(s) for s in keep)
sum(ints)
Hiding the middle steps there in a function is easy enough.
That split returns a list, but the other steps are lazy.
- tome 13y agoBut naming your intermediate variables means it's not possible to splice parts out.
- maxerickson 13y agoYou did call it a grain of sand, but I suppose I'm responding to where you call it complicated. Moving a couple of the names into a separate function means passing in one of them and editing the last line (at least in python with explicit returns). That meets the meaning of more complicated, but (in my opinion) in a sort of hair splitting way.
- tome 13y agoHorses for courses then I guess. If the "easier to refactor" argument doesn't convince you maybe the "easier to read" argument will. Personally I strongly prefer zero-length variable names, but everyone has their own taste.