RosettaCodeData/Task/Averages-Simple-moving-average/Elena/averages-simple-moving-average.elena
2019-09-12 10:33:56 -07:00

54 lines
1.1 KiB
Text

import system'routines;
import system'collections;
import extensions;
class SMA
{
object thePeriod;
object theList;
constructor new(period)
{
thePeriod := period;
theList :=new List();
}
append(n)
{
theList.append(n);
var count := theList.Length;
count =>
0 { ^0.0r }
: {
if (count > thePeriod)
{
theList.removeAt:0;
count := thePeriod
};
var sum := theList.summarize(new Real());
^ sum / count
}
}
}
public program()
{
var SMA3 := SMA.new:3;
var SMA5 := SMA.new:5;
for (int i := 1, i <= 5, i += 1) {
console.printPaddingRight(30, "sma3 + ", i, " = ", SMA3.append:i);
console.printLine("sma5 + ", i, " = ", SMA5.append:i)
};
for (int i := 5, i >= 1, i -= 1) {
console.printPaddingRight(30, "sma3 + ", i, " = ", SMA3.append:i);
console.printLine("sma5 + ", i, " = ", SMA5.append:i)
};
console.readChar()
}