Averages/Simple moving average: Difference between revisions
Content deleted Content added
m →{{header|Sidef}}: updated code |
|||
Line 1,054: | Line 1,054: | ||
=={{header|Elena}}== |
=={{header|Elena}}== |
||
ELENA 3. |
ELENA 3.1 : |
||
<lang elena> |
<lang elena>import system'routines. |
||
import system'collections. |
|||
import extensions. |
|||
class SMA |
class SMA |
||
Line 1,072: | Line 1,072: | ||
append : aNumber |
append : aNumber |
||
[ |
[ |
||
theList |
theList append:aNumber. |
||
var aCount := theList length. |
var aCount := theList length. |
||
aCount => |
|||
0 [ 0.0r ]; |
0 [ ^0.0r ]; |
||
! [ |
! [ |
||
if (aCount > thePeriod) |
if (aCount > thePeriod) |
||
[ |
[ |
||
theList remove |
theList remove index:0. |
||
⚫ | |||
]. |
]. |
||
var aSum := theList summarize:(Real new |
var aSum := theList summarize:(Real new int:0). |
||
⚫ | |||
^ aSum / aCount |
^ aSum / aCount |
||
] |
] |
||
] |
] |
||
} |
} |
||
Line 1,097: | Line 1,097: | ||
var SMA5 := SMA new:5. |
var SMA5 := SMA new:5. |
||
1 to:5 |
1 to:5 doEach: (:i) |
||
[ |
[ |
||
console |
console print:"sma3 + " :i :" = ": (SMA3 append:i) paddingRight:30 with:$32. |
||
console |
console printLine:"sma5 + " :i :" = ": (SMA5 append:i) |
||
]. |
]. |
||
5 to:1 |
5 to:1 doEach: (:i) |
||
[ |
[ |
||
console |
console print:"sma3 + " :i :" = ": (SMA3 append:i) paddingRight:30 with:$32. |
||
console |
console printLine:"sma5 + " :i :" = ": (SMA5 append:i) |
||
]. |
]. |
||