常见问题解答:在 Raku 中,如何根据索引从字符串中删除一些字符?
假设我想删除索引 1 到 3 和 8
xxx("0123456789", (1..3, 8).flat); # 045679
Run Code Online (Sandbox Code Playgroud) Raku 的正则表达式中有两种类型的交替:|
和||
。有什么不同 ?
say 'foobar' ~~ / foo || foobar / # ?foo?
say 'foobar' ~~ / foo | foobar / # ?foobar?
Run Code Online (Sandbox Code Playgroud) FAQ:在 Raku 中如何检查列表是否为空?是否有比以下更惯用的方法:
my @l = ();
say @l.elems == 0;
say @l == ();
say @l.Bool;
Run Code Online (Sandbox Code Playgroud)
say $l ~~ ();
Run Code Online (Sandbox Code Playgroud)
() === ()
即使"" === ""
是正确的,您能否解释为什么 是错误的:我对此不清楚。我正在寻找等待停止(发送异常)到SIGINT
. 文档中给出的示例退出了整个过程,而不仅仅是一名工人。
有人知道如何“杀死”、“取消调度”、“停止”正在运行的线程吗?
这是针对p6-jupyter-kernel问题或这个REPL 问题。
当前的解决方案是重新启动 repl 但不杀死被阻塞的线程
await Promise.anyof(
start {
ENTER $running = True;
LEAVE $running = False;
CATCH {
say $_;
reset;
}
$output :=
self.repl-eval($code,:outer_ctx($!save_ctx),|%adverbs);
},
$ctrl-c
);
Run Code Online (Sandbox Code Playgroud) 如何在 NQP 中打印对象?(用于调试目的)
class Toto { has $.member = 42; }
class Titi { has $.member = 41; has $.toto = Toto.new }
my $ti = Titi.new;
say $ti;
# Titi.new(member => 41, toto => Toto.new(member => 42))
dd $ti;
# Titi $ti = Titi.new(member => 41, toto => Toto.new(member => 42))
Run Code Online (Sandbox Code Playgroud)
class Toto { has $!member; sub create() {$!member := 42}};
class Titi { …
Run Code Online (Sandbox Code Playgroud) FAQ,Int Raku,如何合并,合并两个哈希?
说:
my %a = 1 => 2;
my %b = 3 => 4, 5 => 6
Run Code Online (Sandbox Code Playgroud)
如何获得%c = 1 => 2, 3 => 4, 5 => 6
?
FAQ:在 Raku 中,如何检查String 是否包含子字符串?在哪里和多少次?我想要 3 个功能,例如:
xxx-bool("az and az and az again", "az"); # True
xxx-num("az and az and az again", "az"); # 3
xxx-list("az and az and az again", "az"); # (0 7 14)
Run Code Online (Sandbox Code Playgroud)
PS:Routines index和rindex很酷,但只有一个匹配。
相关链接:
...或者如何从匹配的文本中独立地更改$<sigil>.Str
值。是的,我在问如何欺骗上面的语法(即打电话给)我。token sigil { ... }
所以我想要nogil
令牌,匹配任何东西<?>
以返回字符串化的 NqpMatch:$<sigil>.Str
到 '$'。
目前,我的令牌符号看起来像这样
token sigil {
| <[$@%&]>
| <nogil> { say "Nogil returned: ", lk($/, 'nogil').Str; # Here It should print "$"
}
}
token nogil-proxy {
| '€'
| <?>
{log "No sigil:", get-stack; }
}
Run Code Online (Sandbox Code Playgroud)
并且带有该方法的方法应该返回一个NQPMatch
with 方法Str
被覆盖
method nogil {
my $cursor := self.nogil-proxy;
# …
Run Code Online (Sandbox Code Playgroud) 如何使用将在已经定义的方法之前调用的多方法来扩充类?
我试图使负标:@arr[-1]
在喜欢这个文章,但不改变源。
所以我用以下方法扩充Array:
augment class Array {
proto method AT-POS(Array:D: Int:D $i where <0 ) {
say "AT-POS called";
my $pos = -1;
my $ix = $pos + self.elems;
return self.AT-POS($ix);
}
};
Run Code Online (Sandbox Code Playgroud)
但正如文档中所述
Please note that adding a multi candidate that differs only
in its named parameters will add that candidate behind the already defined one
and as such it won't be picked by the dispatcher.
Run Code Online (Sandbox Code Playgroud)
所以我的 multi 永远不会被调用:
say .signature …
Run Code Online (Sandbox Code Playgroud)