Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^2: understand and prevent 'Out of memory!' during sub recursion

by Discipulus (Canon)
on Nov 09, 2017 at 10:50 UTC ( [id://1203016]=note: print w/replies, xml ) Need Help??


in reply to Re: understand and prevent 'Out of memory!' during sub recursion
in thread understand and prevent 'Out of memory!' during sub recursion

Thanks Eily,

the trick is in the and rigth? it breaks the recursion when grep return false.

It's so bad map in void context?

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
  • Comment on Re^2: understand and prevent 'Out of memory!' during sub recursion
  • Download Code

Replies are listed 'Best First'.
Re^3: understand and prevent 'Out of memory!' during sub recursion
by Eily (Monsignor) on Nov 09, 2017 at 11:11 UTC

    Well it doesn't explicitly break the recursion (it just doesn't recurse anymore, but in the end that's quite the same). I'd say the trick is that grep checks all elements of @_, while your test on $_[0][0] only checks the first array (which happens to be the first to be empty, you're actually lucky that this array was the smallest, with [1,2,3,4,5,6],[1,2],[1,2,3],[1,2,3,4,5] your code might have worked.

    And no, map in void context isn't so bad. It's just considered bad practice, often the reason given is that it's wasteful to build a new list (the output of map) just to throw it away, but also most of the time you can replace it by a for loop, which makes the intent clearer (map indicates that you want to transform a list, while for indicates that you want to iterate over it). map {print shift @$_} @_; for example can be translated to print shift @$_ for @_;

      ahh now I understand my underlying misconception! shifting @$_ i, wrongly, supposed the array to disappear from @_ when empty! while in reality it just trun to be [] an empty one, so i get an infinite call to &inter([],[],[])

      L*

      There are no rules, there are no thumbs..
      Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-04-24 08:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found