Re:Examples of Incorrect Abstractions in Other Languages
by noelwelsh on Wednesday May 20, 2020 @04:36AM
(#23244651)
It's very common in data processing to sum up stuff (e.g. unique visitors, total visitors, total revenue, etc.) To handle this you typically want to write a generic library that makes summing up stuff easy. I can't think of an example where you'd want to add up strings (more likely lists of strings) but it is definitely the case for other data structures (sets, vectors, lists, numbers, etc.)
[ Parent
| on HN ]
Re:Examples of Incorrect Abstractions in Other Languages
by kragen on Wednesday May 20, 2020 @05:19AM
(#23244852)
Python sum() can handle summing up other data structures, although, as with foldl, you may have to give it an initial accumulator argument:
>>> import numpy as np
>>> xs = np.arange(9).reshape((3,3))
>>> xs
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
>>> sum(xs) # builtin sum, not numpy sum
array([ 9, 12, 15])
>>> sum([[3, 11], [5]], [])
[3, 11, 5]
>>> sum([(3, 11), (5,)], ())
(3, 11, 5)
However, that doesn't work for strings; there's a special-case check for that.