4 ms·
You can do shorter in languages with functional constructs. For example Python: sum(map(lambda x: x // 3 - 2, numbers))
by noname120 7y ago
You can do shorter in languages with functional constructs. For example Python:
sum(map(lambda x: x // 3 - 2, numbers))
- skinnyasianboi 7y agoKotlin: numbers.sumBy { it / 3 - 2 }
- the-dude 7y agoWhy is that a double slash?
- michaelmarkell 7y agoIt rounds down the quotient
- the-dude 7y agoHow intuitive. But thanks anyway.
- jacobolus 7y agoIn Python 2, floor division is the default for integer arguments and exact (to within machine precision) division the default for floating point arguments, but this caused a lot of unnecessary confusion: new users were surprised that 3.0/2.0 == 1.5, but 3/2 == 1. Thus for Python 3 the new // operator was added for explicit floor division (which is often quite useful in its own right), leaving the / operator to always return an exact (to machine precision) floating point result, even with integer arguments. cf. https://en.wikipedia.org/wiki/Principle_of_least_astonishment https://en.wikipedia.org/wiki/Principle_of_least_astonishmen...