Cumulative standard deviation: Difference between revisions

m
→‎{{header|Raku}}: notate squaring with super-script
m (sntax highlighting fixup automation)
m (→‎{{header|Raku}}: notate squaring with super-script)
Line 3,623:
=={{header|Raku}}==
(formerly Perl 6)
 
{{works with|Rakudo Star|2010.08}}
Using a closure:
<syntaxhighlight lang="raku" line>sub sd (@a) {
my $mean = @a R/ [+] @a;
sqrt @a R/ [+] map (* - $mean)**2², @a;
}
Line 3,638:
say f $_ for 2, 4, 4, 4, 5, 5, 7, 9;</syntaxhighlight>
 
Using a state variable (remember that <tt><(x-<x>)²> = <x²> - <x>²</tt>):
<syntaxhighlight lang="raku" line>#sub remember that <stddev($x-<x>)²> = <x²> - <x>²{
sub stddev($x) {
sqrt
( .[2] += $x**2²) / ++.[0] -
- ((.[1] += $x ) / .[0])**2²
given state @;
}
 
say .&stddev $_ for <2 4 4 4 5 5 7 9>;</syntaxhighlight>
 
{{out}}
2,392

edits