I do not agree, it would be superfluous to have something primitive and specific like table.sum in the standard library.
It would be more useful to implement table.reduce by line:
table.reduce = function (list, fn) local acc for k, v in ipairs(list) do if 1 == k then acc = v else acc = fn(acc, v) end end return acc end
And use it with a simple lambda:
table.reduce( {1, 2, 3}, function (a, b) return a + b end )
There is no type checking in the reduce implementation example, but you should get this idea.
source share