How do I chain to an inline block in Perl 6?
我想修改一個數(shù)組(我在這個例子中使用了 splice
, 但是它也可能是修改數(shù)組的任何操作)并返回修改后的數(shù)組 - 和 slice
不一樣, slice 返回的是從數(shù)組中摳出的項。我可以很容易地通過在數(shù)組中存儲一個 block 來做到, 就像下面這樣:
my $1 = -> $a { splice($a,1,3,[1,2,3]); $a };
say (^6).map( { $_ < 4 ?? 0 !! $_ } ).Array;
# [0 0 0 0 4 5]
say (^6).map( { $_ < 4 ?? 0 !! $_ } ).Array.$1;
# [0 1 2 3 4 5]
我怎么把由 $1
代表的 block 內(nèi)聯(lián)到單個表達(dá)式中呢杭措? 下面的解決方法不正確:
say (^6).map( { $_ < 4 ?? 0 !! $_ } ).Array.(-> $a { splice($a,1,3,[1,2,3]); $a })
Invocant requires a type object of type Array, but an object instance was passed. Did you forget a 'multi'?
解決方法是添加一個 &
符號:
say (^6).map( { $_ < 4 ?? 0 !! $_ } ).Array.&(-> $a { splice($a,1,3,[1,2,3]); $a })
# 輸出 [0 1 2 3 4 5]
Getting a positional slice using a Range variable as a subscript
my @numbers = <4 8 16 16 23 42>;
.say for @numbers[0..2]; # this works
# 4
# 8
# 15
# but this doesn't
my $range = 0..2;
.say for @numbers[$range];
# 16
最后的那個下標(biāo)看起來好像把 $range
解釋為range中元素的個數(shù)(3)。怎么回事?
解決方法
使用 @numbers[|$range]
把 range 對象展平到列表中。或者在 Range 對象上使用綁定來傳遞它們帆谍。
# On Fri Jul 2016, gfldex wrote:
my @numbers = <4 8 15 16 23 42>; my $range = 0..2; .say for @numbers[$range];
# OUTPUT?16?
# expected:
# OUTPUT?4\n 8\n 15?
# 這是對的, 并且還跟 "Scalar container implies item" 規(guī)則有關(guān).
# Changing it would break things like the second evaluation here:
my @x = 1..10; my @y := 1..3; @x[@y]
# (2 3 4)
@x[item @y]
# 4
# 注意在簽名中 range 可以被綁定給 @y, 而特殊的 Range 可以生成一個像 @x[$(@arr-param)] 的表達(dá)式
# 這在它的語義中是不可預(yù)期的允睹。
# 同樣, 綁定給 $range 也能提供預(yù)期的結(jié)果
my @numbers = <4 8 15 16 23 42>; my $range := 0..2; .say for @numbers[$range];
# OUTPUT?4
8
15
?
# 這也是預(yù)期的結(jié)果, 因為使用綁定就沒有標(biāo)量容器來強制被當(dāng)成一個 item 了。
# So, all here is working as designed.
或者:
.say for @numbers[@($range)]
# 4
# 8
# 15
綁定到標(biāo)量容器的符號輸出一個東西
可以達(dá)到你想要的選擇包含:
前置一個 @ 符號來得到單個東西的復(fù)數(shù)形式:numbers[@$range]; 或者以不同的形式來聲明 ragne 變量, 以使它直接工作涩堤。
對于后者, 考慮下面的形式:
# Bind the symbol `numbers` to the value 1..10:
my \numbers = [0,1,2,3,4,5,6,7,8,9,10];
# Bind the symbol `rangeA` to the value 1..10:
my \rangeA := 1..10;
# Bind the symbol `rangeB` to the value 1..10:
my \rangeB = 1..10;
# Bind the symbol `$rangeC` to the value 1..10:
my $rangeC := 1..10;
# Bind the symbol `$rangeD` to a Scalar container
# and then store the value 1..10 in it:`
my $rangeD = 1..10;
# Bind the symbol `@rangeE` to the value 1..10:
my @rangeE := 1..10;
# Bind the symbol `@rangeF` to an Array container and then
# store 1 thru 10 in the Scalar containers 1 thru 10 inside the Array
my @rangeF = 1..10;
say numbers[rangeA]; # (1 2 3 4 5 6 7 8 9 10)
say numbers[rangeB]; # (1 2 3 4 5 6 7 8 9 10)
say numbers[$rangeC]; # (1 2 3 4 5 6 7 8 9 10)
say numbers[$rangeD]; # 10
say numbers[@rangeE]; # (1 2 3 4 5 6 7 8 9 10)
say numbers[@rangeF]; # (1 2 3 4 5 6 7 8 9 10)
綁定到標(biāo)量容器($rangeD
)上的符號總是產(chǎn)生單個值。在 [...]
下標(biāo)中單個值必須是數(shù)字分瘾。
對于 range, 被當(dāng)作單個數(shù)字時, 產(chǎn)生的是 range 的長度胎围。