Search code examples
treeiterableraku

Tree Method - Any, Iterable


What does the tree method do in Raku?

From the Raku Documentation

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

However, there is no description of the tree method in the "iterable" documentation.

I tried:

my $a = 1..4;
say $a.tree;

And got:

$ raku test.raku
(1 2 3 4)

As opposed to trying:

my $a = 1..4;
say $a;

And getting:

$ raku test.raku
1..4

But, I'm not really sure what the difference is or what it means.

Under the types list, on the Raku docs website, there doesn't seem to be a separate "tree" type.


Solution

  • $ raku -e 'say (1, (^5), ((4,5),), 6).tree(*.self, *.reverse)'
    (1 (4 3 2 1 0) ((4 5)) 6)
    
    $ raku -e 'say (1, (^5), ((4,5),), 6).tree(*.self, *.reverse, *.sum)'
    (1 (4 3 2 1 0) (9) 6)
    

    Note that only the 3rd level of the second example has the .sum applied, how both have the 2nd level reversed, and how both have the 1st level left intact.

    If you need examples you can always look in the perl6 roast test suite at https://github.com/Raku/roast

    https://github.com/Raku/roast/blob/ad9f949e2b479b5800c3e6315f979ded595a09fd/S02-lists/tree.t