Averages/Simple moving average: Difference between revisions

Content added Content deleted
(→‎{{header|Vlang}}: Rename "Vlang" in "V (Vlang)")
(non-recursive lua sum function)
Line 2,537: Line 2,537:
<syntaxhighlight lang="lua">function sma(period)
<syntaxhighlight lang="lua">function sma(period)
local t = {}
local t = {}
function sum(a, ...)
function sum(t)
sum = 0
if a then return a+sum(...) else return 0 end
for _, v in ipairs(t) do
sum = sum + v
end
return sum
end
end
function average(n)
function average(n)
if #t == period then table.remove(t, 1) end
if #t == period then table.remove(t, 1) end
t[#t + 1] = n
t[#t + 1] = n
return sum(unpack(t)) / #t
return sum(t) / #t
end
end
return average
return average