Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: Abusing Map

by AnomalousMonk (Archbishop)
on May 17, 2018 at 13:20 UTC ( [id://1214740]=note: print w/replies, xml ) Need Help??


in reply to Re: Abusing Map
in thread Abusing Map

$b[ $i ] = $a[ $i ] + $a[ --$i ] while $i;

Except that crashes with a "Modification of non-creatable array value attempted ..." fatality (after a couple of warnings) for an empty input array. I've only visually examined them, but all the preceeding for-loop and map (and even grep!) solutions seem as if they would handle empty arrays gracefully. But
    $b[ $i ] = $a[ $i ] + $a[ --$i ] while $i > 0;
avoids the problem.


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^3: Abusing Map
by BrowserUk (Patriarch) on May 17, 2018 at 13:47 UTC
    fatality (after a couple of warnings) for an empty input array

    So don't do that :)

    Test before entering the loop:

    ( my $i = $#a ) > 0 or die; $b[ $i ] = $a[ $i ] + $a[ --$i ] while $i;

    Never do in a loop, what can be done outside of it.

    Wouldn't your version fail for an array with one element?


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
    In the absence of evidence, opinion is indistinguishable from prejudice. Suck that fhit
      Never do in a loop, what can be done outside of it.

      I agree, and that's why I like, in particular, the for-loop and, less so, map (and let's just pass over the grep) versions that iterate over ranges of 1 .. $#array or 0 .. $#array-1 and so will never enter the loop. (Again, I haven't actually tested all the preceding code in this thread, only stared at it, but it looks ok.)

      Wouldn't your version fail for an array with one element?

      That depends on the meaning of "fail."

      c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my @ra = (1); my $i = $#ra; ;; my @rb; $rb[ $i ] = $ra[ $i ] + $ra[ --$i ] while $i > 0; dd \@rb " []
      But maybe an operation that iterates pairwise over an array with less than a pair of elements should throw an exception or at least issue a warning. Who can say? Only the Great Specificator above.


      Give a man a fish:  <%-{-{-{-<

        I agree, and that's why I like, in particular, the for-loop and, less so, map (and let's just pass over the grep) versions that iterate over ranges of 1 .. $#array or 0 .. $#array-1 and so will never enter the loop

        The problem with the map/grep and postfix for is that they generate a list as big as the array.

        Small lists: meh! But for big lists -- where big frequently is in the 100s of millions on my machine and billions on client/AWS X1 instances -- those lists cost big time.

        Postfix while and until avoid that whilst still giving clean, concise code. Combined with each, its good for big hashes also.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
        In the absence of evidence, opinion is indistinguishable from prejudice. Suck that fhit

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2024-03-29 11:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found