树方法 - 任何,可迭代

Tree Method - Any, Iterable

Perl6 中树方法的作用是什么?

来自Perl6 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.

然而,"iterable"文档中并没有对树方法的描述。

我试过了:

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

并得到:

$ perl6 test.pl6
(1 2 3 4)

与尝试相反:

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

并获得:

$ perl6 test.pl6
1..4

但是,我不太确定有什么区别或含义。

在 Perl6 文档网站的类型列表下,似乎没有单独的 "tree" 类型。

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

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

请注意,只有第二个示例的第 3 级应用了 .sum,第 2 级如何反转,第 1 级如何保持不变。

如果您需要示例,您可以随时查看 https://github.com/perl6/roast

上的 perl6 roast 测试套件

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