Forest::Tree::Pure(3pm) | User Contributed Perl Documentation | Forest::Tree::Pure(3pm) |
Forest::Tree::Pure - An n-ary tree
use Forest::Tree; my $t = Forest::Tree::Pure->new( node => 1, children => [ Forest::Tree::Pure->new( node => 1.1, children => [ Forest::Tree::Pure->new(node => 1.1.1), Forest::Tree::Pure->new(node => 1.1.2), Forest::Tree::Pure->new(node => 1.1.3), ] ), Forest::Tree::Pure->new(node => 1.2), Forest::Tree::Pure->new( node => 1.3, children => [ Forest::Tree::Pure->new(node => 1.3.1), Forest::Tree::Pure->new(node => 1.3.2), ] ), ] ); $t->traverse(sub { my $t = shift; print((' ' x $t->depth) . ($t->node || '\undef') . "\n"); });
This module is a base class for Forest::Tree providing functionality for immutable trees.
It can be used independently for trees that require sharing of children between parents.
There is no single authoritative parent (no upward links at all), and changing of data is not supported.
This class is appropriate when many tree roots share the same children (e.g. in a versioned tree).
This class is strictly a DAG, wheras Forest::Tree produces a graph with back references
It takes a callback in the form:
sub { my ( $tree, $cont, @args ) = @_; ... }
and $cont is a code ref that when invoked will apply that same function to the children of $tree.
This allows you to do things like computing the sum of all the node values in a tree, for instance:
use List::Util qw(sum); my $sum = $tree->fmap_cont(sub { my ( $tree, $cont ) = @_; return sum( $tree->node, $cont->() ); });
And also allows one to stop traversal at a given point.
The children must inherit "Forest::Tree::Pure"
Note that this method does not mutate the tree, instead it clones and returns a tree with the augmented list of children.
Returns a derived tree with overridden children.
Returns a derived tree with overridden children.
$tree->get_child_at(0)->get_child_at(1)->get_child_at(0); $tree->locate(0, 1, 0);
This method is also implemented in Forest::Tree by mutating the tree in place and returning the original tree, so the same transformations should work on both pure trees and mutable ones.
This code:
my $new = $root->transform([ 1, 3 ], insert_child_at => 3, $new_child);
will locate the child at the path "[ 1, 3 ]", call "insert_child_at" on it, creating a new version of "[ 1, 3 ]", and then return a cloned version of "[ 1 ]" and the root node recursively, such that $new appears to be a mutated $root.
Deeply clones the entire tree.
Subclasses should use MooseX::Clone traits to specify the correct cloning behavior for additional attributes if cloning is used.
Does not use "clone".
All complex software has bugs lurking in it, and this module is no exception. If you find a bug please either email me, or add the bug to cpan-RT.
Yuval Kogman
Copyright 2008-2014 Infinity Interactive, Inc.
<http://www.iinteractive.com>
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
2021-01-05 | perl v5.32.0 |