Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

You can do shorter in languages with functional constructs. For example Python:

  sum(map(lambda x: x // 3 - 2, numbers))


Why is that a double slash?


It rounds down the quotient


How intuitive. But thanks anyway.


In 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_astonishmen...


Kotlin:

numbers.sumBy { it / 3 - 2 }




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: