In Any§

See primary documentation in context for method tree

multi method tree(Any:U:)
multi method tree(Any:D:)
multi method tree(Any:D: Whatever )
multi method tree(Any:D: Int(Cool) $count)
multi method tree(Any:D: @ [&first, *@rest])
multi method tree(Any:D: &first, *@rest)

Returns the class if it's undefined or if it's not Iterable, returns the result of applying the tree method to its invocant otherwise.

say Any.tree; # OUTPUT: «Any␤»

.tree has different prototypes for Iterable elements.

my @floors = ( 'A', ('B','C', ('E','F','G')));
say @floors.tree(1).flat.elems; # OUTPUT: «6␤»
say @floors.tree(2).flat.elems; # OUTPUT: «2␤»
say @floors.tree( *.join("-"),*.join(""),*.join("|"));# OUTPUT: «A-B—C—E|F|G␤»

With a number, it iteratively applies tree to every element in the lower level; the first instance will apply .tree(0) to every element in the array, and likewise for the next example.

The second prototype applies the WhateverCode passed as arguments to every level in turn; the first argument will go to level 1 and so on. tree can, thus, be a great way to process complex all levels of complex, multi-level, data structures.