Averages/Simple moving average: Difference between revisions

→‎{{header|Perl}}: some more clarification and code clean-up
(→‎{{header|Perl}}: clean up code (this is not a golf contest!), fix small bug, and add usage demonstration)
(→‎{{header|Perl}}: some more clarification and code clean-up)
Line 2,235:
 
=={{header|Perl}}==
 
Using an initializer function which returns an anonymous closure which closes over an instance ''(separate for each call to the initializer!)'' of the lexical variables <code>$period</code>, <code>@list</code>, and <code>$sum</code>:
 
<lang perl>sub sma_generator {
my $period = shift;
my (@alist, $sum);
 
return sub {
unshiftmy @a,$number = shift;
$sumpush +=@list, $a[0]number;
@a > $period and $sum -+= pop @a$number;
return $sum /-= shift @alist if @list > $period;
return $sum / @list;
}
}
Anonymous user