Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Challenge: Perl 5: lazy sameFringe()?

by hdb (Prior)
on Jun 30, 2013 at 13:16 UTC ( #1041588=note: print w/ replies, xml ) Need Help??


in reply to Challenge: Perl 5: lazy sameFringe()?

Laurent_R above is looking for a recursive solution. Recursion is the easiest way to traverse a tree. My idea to do this is sketched like this for simultaneous recursive traversal:

use strict; use warnings; sub recursiveFringe { for( @{ +shift } ) { if( ref $_ ) { recursiveFringe( $_ ) } else { print "$_\n" }; } } sub sameFringe { # create a subthread for each tree executing the above recursiveFrin +ge # in such a way that the output from print above can be read in # while(<...>) in the main thread. # once a difference occurs return FALSE and terminate threads } my @trees = ( [ [ 1, [ [2, [4, 7] ], 5 ] ], [3, [6, [8, 9] ] ] ], [ [ 1, [ [2, [4, 7] ], 5 ] ], [3, [6, [8, 9] ] ] ], [ [ 1, [ [ [2, 4], 7], 5 ] ], [3, [6, [8, 9] ] ] ], [ [ 1, [ [2, [4, 7] ], 5 ] ], [3, [6, [8, [9, 0 ] ] ] ] ] +, [ [ 1, [ [0, [4, 7] ], 5 ] ], [3, [6, [8, 9] ] ] ], ); print sameFringe( $trees[0], $_ )?"Same\n":"Different\n" for @trees;

As I have no experience with threads, I do not really know how to start this or whether it is even feasible. What do you think?

LanX: As far as I can see, this would work for trees with more branches than two...


Comment on Re: Challenge: Perl 5: lazy sameFringe()?
Download Code
Re^2: Challenge: Perl 5: lazy sameFringe()?
by BrowserUk (Pope) on Jun 30, 2013 at 13:54 UTC
    # create a subthread for each tree executing the above recursiveFringe # in such a way that the output from print above can be read in # while(<...>) in the main thread. # once a difference occurs return FALSE and terminate threads

    That looks more like a fork-based approach than a thread based.

    See mine for a recursion and threads approach.

    I'd love to see a Coro solution here.


    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".
    In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (11)
As of 2014-12-18 15:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (56 votes), past polls