Here you go. This one's free! ;)
sub standard_deviation { # declared all at once my ($mean, $std_dev) = (0,0); # idioms are handy -- I like the postfix 'for' # and '$x = $x + $y' can be written as '$x += $y' $mean += $_ for @_; # no need for $size, it's used once, and is @_ # again, using the 'A = A OP B' => 'A OP= B' trick $mean /= @_; # appending to arrays is best done with push() # @big_array = (@big_array, $x) can get slow :( # for (@_) { # push @diffs, ($_ - $mean)**2; # } # that can be rewritten as a map() # @diffs = map { ($_ - $mean)**2 } @_; # but we only use @diffs ONCE, so we don't need it $std_dev += ($_ - $mean)**2 for @_; # and $size was removed, so we use @_ again return $std_dev / @_; }
When it comes down to it, the function becomes:
sub standard_deviation { my ($mean, $std_dev) = (0,0); $mean += $_ for @_; $mean /= @_; $std_dev += ($_ - $mean)**2 for @_; return $std_dev / @_; }

_____________________________________________________
Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;


In reply to Re: Code Review! Go Ahead, Rip It Up! by japhy
in thread Code Review! Go Ahead, Rip It Up! by chaoticset

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.