How could I cut off an array or an array-reference in Perl 6?
我如何在Perl 6中切断数组或数组引用?
In Perl 5, I can do this:
在Perl 5中,我可以这样做:
my $d = [0 .. 9];
$#$d = 4;
In Perl 6, I get an error if I try this:
在Perl 6中,如果我尝试这个,我会收到错误:
my $d = [0 .. 9];
$d.end = 4; # Cannot modify an immutable Int
This works, but it looks less beautiful than the Perl 5 way and may be expensive:
这可行,但它看起来不如Perl 5方式漂亮,可能很昂贵:
$d.=splice(0, 5);
3 个解决方案
#1
13
There is a simple way:
有一个简单的方法:
my $d = [0..9];
$d[5..*] :delete;
That is problematic if the array is an infinite one.
如果阵列是无限的,则存在问题。
$d.splice(5)
also has the same problem.
$ d.splice(5)也有同样的问题。
Your best bet is likely to be $d = [ $d[^5] ]
in the average case where you may not know anything about the array, and need a mutable Array.
在平均情况下你最好的选择可能是$ d = [$ d [^ 5]],你可能对阵列一无所知,需要一个可变数组。
If you don't need it to be mutable $d = $d[^5]
which returns a List may be better.
如果你不需要它是可变的$ d = $ d [^ 5],它返回一个List可能会更好。
#2
6
splice
is probably the best choice here, but you can also shorten to five elements using the ^N
range constructor shortcut (I call this the "up until" "operator" but I am sure there is a more correct name since it is a constructor of a Range
):
splice可能是这里最好的选择,但你也可以使用^ N范围构造函数快捷方式缩短到五个元素(我称之为“直到”“运算符”但我确信有一个更正确的名称,因为它是一个构造函数范围):
> my $d = [ 0 .. 9 ];
> $d.elems
> 10
> $d = [ $d[^5] ]
[0 1 2 3 4]
> $d.elems
5
> $d
[0 1 2 3 4]
"The caret is ... a prefix operator for constructing numeric ranges starting from zero".
(From theRange
documentation)“插入符号是......用于构造从零开始的数值范围的前缀运算符”。 (来自Range文档)
One can argue that perl6 is "perl-ish" in the sense it usually has an explicit version of some operation (using a kind of "predictable" syntax - a method, a routine, and :adverb
, etc.) that is understandable if you are not familiar with the language, and then a shortcut-ish variant.
有人可以说perl6是“perl-ish”,因为它通常具有某种操作的显式版本(使用一种“可预测”的语法 - 方法,例程和:副词等),这是可以理解的你不熟悉这种语言,然后是一种快捷方式。
I'm not sure which approach (splice
vs. the shortcut vs. using :delete
as Brad Gilbert mentions) would have an advantage in speed or memory use. If you run:
我不确定哪种方法(拼接与快捷方式与使用方式:删除方式为Brad Gilbert提及)在速度或内存使用方面具有优势。如果您运行:
perl6 --profile -e 'my $d = [ 0 .. 9 ]; $d=[ $d[^5] ]'
perl6 --profile -e 'my $d = [ 0 .. 9 ]; $d.=splice(0, 5);'
you can see a slight difference. The difference might be more significant if you compared with a real program and workload.
你可以看到一点点差异。如果与实际程序和工作负载进行比较,差异可能会更大。
#3
2
Another option is using the xx
operator:
另一种选择是使用xx运算符:
my $d = [0..9];
$d.pop xx 4; #-> (9 8 7 6)
say $d; #-> [0 1 2 3 4 5]
$d = [0..9];
$d.shift xx 5 #-> (0 1 2 3 4)
say $d; #-> [5 6 7 8 9)
#1
13
There is a simple way:
有一个简单的方法:
my $d = [0..9];
$d[5..*] :delete;
That is problematic if the array is an infinite one.
如果阵列是无限的,则存在问题。
$d.splice(5)
also has the same problem.
$ d.splice(5)也有同样的问题。
Your best bet is likely to be $d = [ $d[^5] ]
in the average case where you may not know anything about the array, and need a mutable Array.
在平均情况下你最好的选择可能是$ d = [$ d [^ 5]],你可能对阵列一无所知,需要一个可变数组。
If you don't need it to be mutable $d = $d[^5]
which returns a List may be better.
如果你不需要它是可变的$ d = $ d [^ 5],它返回一个List可能会更好。
#2
6
splice
is probably the best choice here, but you can also shorten to five elements using the ^N
range constructor shortcut (I call this the "up until" "operator" but I am sure there is a more correct name since it is a constructor of a Range
):
splice可能是这里最好的选择,但你也可以使用^ N范围构造函数快捷方式缩短到五个元素(我称之为“直到”“运算符”但我确信有一个更正确的名称,因为它是一个构造函数范围):
> my $d = [ 0 .. 9 ];
> $d.elems
> 10
> $d = [ $d[^5] ]
[0 1 2 3 4]
> $d.elems
5
> $d
[0 1 2 3 4]
"The caret is ... a prefix operator for constructing numeric ranges starting from zero".
(From theRange
documentation)“插入符号是......用于构造从零开始的数值范围的前缀运算符”。 (来自Range文档)
One can argue that perl6 is "perl-ish" in the sense it usually has an explicit version of some operation (using a kind of "predictable" syntax - a method, a routine, and :adverb
, etc.) that is understandable if you are not familiar with the language, and then a shortcut-ish variant.
有人可以说perl6是“perl-ish”,因为它通常具有某种操作的显式版本(使用一种“可预测”的语法 - 方法,例程和:副词等),这是可以理解的你不熟悉这种语言,然后是一种快捷方式。
I'm not sure which approach (splice
vs. the shortcut vs. using :delete
as Brad Gilbert mentions) would have an advantage in speed or memory use. If you run:
我不确定哪种方法(拼接与快捷方式与使用方式:删除方式为Brad Gilbert提及)在速度或内存使用方面具有优势。如果您运行:
perl6 --profile -e 'my $d = [ 0 .. 9 ]; $d=[ $d[^5] ]'
perl6 --profile -e 'my $d = [ 0 .. 9 ]; $d.=splice(0, 5);'
you can see a slight difference. The difference might be more significant if you compared with a real program and workload.
你可以看到一点点差异。如果与实际程序和工作负载进行比较,差异可能会更大。
#3
2
Another option is using the xx
operator:
另一种选择是使用xx运算符:
my $d = [0..9];
$d.pop xx 4; #-> (9 8 7 6)
say $d; #-> [0 1 2 3 4 5]
$d = [0..9];
$d.shift xx 5 #-> (0 1 2 3 4)
say $d; #-> [5 6 7 8 9)