Averages/Simple moving average: Difference between revisions

+ AutoHotkey contributed by Laszlo from ahk forums
(→‎{{header|Common Lisp}}: use first/rest accessors on lists, explain)
(+ AutoHotkey contributed by Laszlo from ahk forums)
Line 3:
 
Create a [[wp:Stateful|stateful]] function/class/instance that takes a number as argument and returns a simple moving average of its arguments so far.
=={{header|AutoHotkey}}==
ahk forum: [http://www.autohotkey.com/forum/post-276695.html#276695 discussion]
<lang AutoHotkey>MsgBox % MovingAverage() ; reset: blank
MsgBox % MovingAverage(1) ; 1
MsgBox % MovingAverage(3) ; 2
MsgBox % MovingAverage(-1) ; 1
 
MovingAverage(x="") {
Static sum:=0, n:=0
If (x="") ; blank parameter: reset
sum := 0, n := 0
Else
sum += x, n++ ; update state
Return sum/n
}</lang>
 
=={{header|Common Lisp}}==
Anonymous user