lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

Hear, hear!

-- Average function using arithmetic mean
-- (using proposed math.sum function)
function math.avg(...)
    return math.sum(...) / select("#",...)

On a side note, I find it vaguely disappointing how many (too quick) responses to the original post were utterly wrong given the sample results. I know (hope?) we all could do better given some real effort. This seems to be a bit of a trend lately - of shooting from the hip with solution/sample code. Perhaps we should be less quick to respond and more careful with such responses?

On 6/21/2010 5:15 PM, Luiz Henrique de Figueiredo wrote:
function math.mean(...)
   local argc=select("#",...)
   local total=0
   for i=1,argc do
   return total/argc

Now, a case for math.sum could be made in order to avoid select, just as
there is math.min and math.max.