RosettaCodeData/Task/Averages-Simple-moving-average/Sidef/averages-simple-moving-average-1.sidef
2023-07-01 13:44:08 -04:00

22 lines
456 B
Text
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

func simple_moving_average(period) {
var list = []
var sum = 0
func (number) {
list.append(number)
sum += number
if (list.len > period) {
sum -= list.shift
}
(sum / list.length)
}
}
var ma3 = simple_moving_average(3)
var ma5 = simple_moving_average(5)
for num (1..5, flip(1..5)) {
printf("Next number = %d, SMA_3 = %.3f, SMA_5 = %.1f\n",
num, ma3.call(num), ma5.call(num))
}