Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^2: Passing Variables

by jdporter (Paladin)
on Feb 25, 2021 at 22:40 UTC ( [id://11128806]=note: print w/replies, xml ) Need Help??


in reply to Re: Passing Variables
in thread Passing Variables

sub find_average needs to:

  1. initialize my $sum = 0;
  2. handle the arg list being empty. You probably don't want to throw a divide-by-zero exception.

I suppose I'd write sub above_average like this:

sub above_average { my $average_num = shift; grep { $_ > $average_num } @_ }

Replies are listed 'Best First'.
Re^3: Passing Variables
by Marshall (Canon) on Feb 25, 2021 at 23:38 UTC
    Your comments are appropriate, thanks!
    My goal was to refactor the OP's code in a straight-forward way, with the intent of being instructive - there are of course imperfections .
    When coding this myself, I would be thinking about functions in List::Util and passing references to array instead of an array itself. But that is not what I thought my job here was. My intent was to present easy to understand code. But even so, I figure that the OP will have some questions about it.

      Example code that skips error handling, unless that is made clear in the accompanying text, is an example of how to do something badly. I'm sure you didn't intend it like that, but error handling and considering edge cases are the key to writing robust reliable code.

      Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
        Well, jdporter says: "handle the arg list being empty. You probably don't want to throw a divide-by-zero exception."

        I am actually fine with that error code!

        I have written Perl code that intercepts various warnings in situations where I can actually fix the problem. I remember one sort routine where a numeric comparison didn't work and the code defaulted back to an alpha comparison. There is no "fix" for a "divide by zero".

        I added comments about this in further code. Grandfather is right about that. Document limitations.
        In the code, allowing Perl to throw a "divide by zero" exception is fine.
        The code is short. Detecting and printing an error that "will happen" is unnecessary, because there is no "fix" for that error.

Re^3: Passing Variables
by GrandFather (Saint) on Mar 01, 2021 at 00:40 UTC
    "initialize my $sum = 0;"

    An interesting variation, that is probably premature optimization in simple cases, is to:

    sub average { my $sum = shift @_; ...;

    For algorithms such as moving averages or integrators initializing the accumulator with the first sample value can be essential to avoiding large glitches at the start of the result data.

    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11128806]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-23 22:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found