Anonymous user
Averages/Simple moving average: Difference between revisions
→{{header|Perl 6}}: update & simplify
m (added a ;Task: (bold) header.) |
(→{{header|Perl 6}}: update & simplify) |
||
Line 2,936:
=={{header|Perl 6}}==
<lang perl6>sub sma(Int \P where * > 0) returns Sub {▼
{{works with|Rakudo|2016.08}}
sub ($x) {
state @a = 0 xx $P;
@a.push($x).shift;
}
}</lang>
Usage:
<lang perl6>my &sma = sma-generator 3;
for 1, 2, 3, 2, 7 {
printf "append $_ --> sma = %.2f (with period 3)\n", sma $_;
}</lang>
{{out}}
<pre>
append 1 --> sma = 0.33 (with period 3)
append 2 --> sma = 1.00 (with period 3)
append 3 --> sma = 2.00 (with period 3)
append 2 --> sma = 2.33 (with period 3)
append 7 --> sma = 4.00 (with period 3)
</pre>
=={{header|Phix}}==
|