Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
The reason BrowserUk passed the node-printing code as an anonymous subroutine was to separate the traversal logic from the node-processing logic. With this separation, we can use the same traversal logic for other purposes, and we can use the same node-processing logic with other traversals.

Consider the following nested-array-style tree and logic to traverse it:

my $tree = [ 1 , [ 2 , 3 , [ 4 , [ 5 ] , 6 ] ] , 7 ] ; sub traverse { my ($nodefn, $tree, $depth) = @_; $depth ||= 0; $depth++; for (@$tree) { ref() ? traverse( $nodefn, $_, $depth ) : $nodefn->( $_, $depth ); } }
We can combine this traversal logic with task-specific node-processing logic. For example, to count the nodes in the tree, we might use logic like this:
sub count_nodes { my $count; traverse( sub { $count++ }, @_ ); $count; } print count_nodes($tree), $/; # 7
For other jobs we can easily "plug in" other node-processing code. Here is code to print a single indented node:
sub print_node { my ($leaf, $depth) = @_; print " " x ($depth - 1), "* $leaf", $/; }
We just plug it into traverse to print the tree's outline:
traverse( \&print_node, $tree ); # * 1 # * 2 # * 3 # * 4 # * 5 # * 6 # * 7
Going further, we can plug in new traversal logic, too. Here is logic to do a breadth-first traversal – all the leaves at each depth are processed before going deeper.
sub bfs_traverse { my ($nodefn, $tree, $depth) = @_; $depth ||= 0; $depth++; my @leaves = grep !ref(), @$tree; my @branches = grep ref(), @$tree; $nodefn->( $_, $depth ) for @leaves; bfs_traverse( $nodefn, [map @$_, @branches], $depth ) if @branches; }
Now we can use this traversal logic with our existing print_node logic to outline the tree in breadth-first order:
bfs_traverse( \&print_node, $tree ); # * 1 # * 7 # * 2 # * 3 # * 4 # * 6 # * 5
By this point, I hope you can see the answer to your second question: by keeping traversal and node-processing concerns separated and by combining them through a well-defined interface (here, function passing), we can mix and match logic. If we want a breadth-first outline, we don't need to write a breadth-first outline function; we can just combine a breadth-first traversal with a node-printing processor.

Cheers,
Tom


In reply to Re: Passing anon sub as param by tmoertel
in thread Passing anon sub as param by punkish

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-04-25 13:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found