Averages/Simple moving average: Difference between revisions

(→‎{{header|Lasso}}: Adding Lasso Example)
Line 793:
 
=={{header|Elena}}==
<lang elena>#define std'dictionary'*system.
#define stdsystem'basic'*routines.
#define stdsystem'collections'*.
#define std'patterns'*extensions.
 
#class ESMARoleSMAExtender
{
#field thePeriod.
#initializerconstructor new : aPeriod
[
thePeriod := aPeriod.
]
#method +add : aNumber
[
self += NewInt32Value::aNumber.
 
#ifvar (selfaCount count:= >self thePeriod)?Count.
[^ aCount =>
self0 first_item'reduce? [ 0.0r ]
]. ! [
(aCount > thePeriod)?
[
self removeFirst.
^ aSum / aCount ].
#var aCountaSum := selfSumming countnew:(Real new:0) foreach:self.
#if (aCount == 0)? ^ aSum / self Count.
[ ^ 0. ].
#var aSum := Summing::Real::0 start:Scan::self.
^ aSum / aCount.
]
}
 
#symbol SMAsma := &&:aPeriod =[ __wrapExtension(ESMARole:SMAExtender new:aPeriod, List new) ].
 
// --- Program ---
 
#symbol Programprogram =
[
#var SMA3 := SMA:sma:3.
#var SMA5 := SMA:sma:5.
 
loopcontrol &&from:1 &to:5 run&do: &&:i =
[
'program'output <<consoleEx writeLine:"sma3 + " << :i << :" = ": << (SMA3 + i Number).
'program'output <<consoleEx writeLine:",sma5 + " << :i << :" = ": << (SMA5 + i << "%n"Number).
].
 
loopcontrol &&from:5 &tobackTo:1 &stepdo:-1 run&&: i =
[
'program'output <<consoleEx writeLine:"sma3 + " << :i << :" = ": << (SMA3 + i Number).
'program'output <<consoleEx writeLine:",sma5 + " << :i << :" = ": << (SMA5 + i << "%n"Number).
].
].</lang>
</lang>
 
=={{header|Erlang}}==
Anonymous user