Raku 提供了许多不可变的类型,因此在创建后无法修改。直到我最近开始研究这个领域,我的理解是这些类型不是 持久数据结构——也就是说,与 Clojure 或 Haskell 中的核心类型不同,我相信 Raku 的不可变类型没有利用结构共享来允许廉价的副本。我认为该语句my List $new = (|$old-list, 42);
从字面上复制了 中的值$old-list
,而没有持久数据结构的数据共享功能。
但是,由于以下代码,我对我的理解的描述是过去时:
my Array $a = do {
$_ = [rand xx 10_000_000];
say "Initialized an Array in $((now - ENTER now).round: .001) seconds"; $_}
my List $l = do {
$_ = |(rand xx 10_000_000);
say "Initialized the List in $((now - ENTER now).round: .001) seconds"; $_}
do { $a.push: rand;
say "Pushed the element to …
Run Code Online (Sandbox Code Playgroud) 我v2020.12
在我的计算机上安装了 Rakudo,并且也是v2020.12
从源代码构建的。在分析启动时间时,我注意到以下结果:(这些基准测试使用hyperfine,但我也得到了类似的结果time
)。
$ which raku
/opt/rakudo-pkg/bin/raku
$ /opt/rakudo-pkg/bin/raku --version
Welcome to Rakudo(tm) v2020.12.
Implementing the Raku(tm) programming language v6.d.
Built on MoarVM version 2020.12.
$ /opt/rakudo-pkg/bin/raku ~/.raku/bin/red --help > /dev/null # make sure modules are compiled
$ hyperfine -i '/opt/rakudo-pkg/bin/raku /home/dsock/.raku/bin/red'
Benchmark #1: /opt/rakudo-pkg/bin/raku /home/dsock/.raku/bin/red
Time (mean ± ?): 890.0 ms ± 15.5 ms [User: 1.411 s, System: 0.068 s]
Range (min … max): 861.3 ms … 913.4 ms 10 runs
Warning: …
Run Code Online (Sandbox Code Playgroud) 我无法理解supply {\xe2\x80\xa6}
区块/它们创建的按需供应的用途。
Live supplies (that is, the types that come from a Supplier
and get new values whenever that Supplier
emits a value) make sense to me \xe2\x80\x93 they\'re a version of asynchronous streams that I can use to broadcast a message from one or more senders to one or more receivers. It\'s easy to see use cases for responding to a live stream of messages: I might want to take an action every time I get …
我对 Cro 如何处理客户端请求感到有点困惑,特别是为什么某些请求似乎会导致 Cro 的内存使用量激增。
\n一个最小的例子出现在字面上的“Hello world!”中。克罗服务器。
\nuse Cro::HTTP::Router;\nuse Cro::HTTP::Server;\n\nmy $application = route {\n get -> {\n content \'text/html\', \'Hello Cro!\';\n }\n}\n\nmy Cro::Service $service = Cro::HTTP::Server.new:\n :host<localhost>, :port<10000>, :$application;\n\n$service.start;\n\nreact whenever signal(SIGINT) {\n $service.stop;\n exit;\n}\n
Run Code Online (Sandbox Code Playgroud)\n该服务器所做的就是用“Hello Cro!\”\xe2\x80\x93 响应 GET 请求,这当然不应该是繁重的。但是,如果我导航到页面localhost:10000
然后快速刷新页面,我会注意到 Cro\'内存使用量开始攀升(然后保持较高水平)。
这种情况似乎只在刷新很快时才会发生,这表明该问题可能与未正确关闭连接或并发问题有关(可能与之前的问题略有相关)。
\n为了简单起见,这个“Hello world”服务器是否省略了一些性能技术或最佳实践?或者我是否遗漏了有关 Cro 设计工作方式的其他信息?
\n我真的很欣赏 Raku 的&?BLOCK
变量——它让你在一个未命名的块内递归,这可能非常强大。例如,这是一个简单的内联匿名阶乘函数:
{ when $_ ? 1 { 1 };
$_ × &?BLOCK($_ - 1) }(5) # OUTPUT: «120»
Run Code Online (Sandbox Code Playgroud)
但是,在更复杂的情况下使用时,我对它有一些疑问。考虑这个代码:
{ say "Part 1:";
my $a = 1;
print ' var one: '; dd $a;
print ' block one: '; dd &?BLOCK ;
{
my $a = 2;
print ' var two: '; dd $a;
print ' outer var: '; dd $OUTER::a;
print ' block two: '; dd &?BLOCK;
print "outer block: "; dd &?OUTER::BLOCK …
Run Code Online (Sandbox Code Playgroud) 我想知道在 Raku 中创建 CLI 应用程序时推荐哪些步骤(例如 shebang 行和包装脚本)。我对将与 Zef 一起安装的脚本和将单独分发的脚本的信息都感兴趣。
文档提供了示例frobnicate
程序:
# inside "frobnicate.raku"
sub MAIN(Str $file where *.IO.f, Int :$length = 24, Bool :$verbose) { #`(body omitted)}
Run Code Online (Sandbox Code Playgroud)
我可以用它来运行raku frobnicate.raku
——一个很好的脚本解决方案,但对真正的程序来说不是很好。
如果我想让它更像一个标准程序,我可以创建一个frobnicate
这样的文件:
#!/usr/bin/env raku
sub MAIN(Str $file where *.IO.f, Int :$length = 24, Bool :$verbose) { #`(body omitted)}
Run Code Online (Sandbox Code Playgroud)
我可以使该文件可执行chmod +x
并将其移动到我的目录中$PATH
;然后我可以用命令 frobnicate frobnicate
。到目前为止,所有这些都非常合理,就像任何其他脚本语言一样。
然而,这些都没有利用 Raku 的编译。因此,执行更多处理的 CLI 应用程序MAIN
可能会变得有点慢(即使是生成--help
输出)。因此,下一步是允许预编译应用程序,但我不太确定如何执行此操作。
当我查看使用 Zef 安装 Raku 程序后执行的脚本时,它们几乎都具有以下形式:
#!/usr/bin/env perl6
sub MAIN(:$name, …
Run Code Online (Sandbox Code Playgroud) 当我使用 REPL 时,我有时需要查找函数的功能,例如splice
. 我通常会访问文档网站。但我并不总是有互联网,如果我可以直接在 REPL 中编写help("splice")
或某些内容(例如splice?
)并查看结果,那就太好了。
然后我认为p6doc
Rakudo Star 附带的可以使用,因为p6doc Array.splice
在命令行上提供了文档。然后我在 REPL 中这样做:
> run <p6doc Array.splice>\n\nProc.new(in => IO::Pipe, out => IO::Pipe, err => IO::Pipe, exitcode => 1,\n signal => 0, pid => Nil, command => ("p6doc", "Array.splice"))\n
Run Code Online (Sandbox Code Playgroud)\n:out
但它有 exitcode 1。当我使用和捕获输出时:err
,两者都是空字符串,但我不知道为什么。
有没有办法让 REPL 中的这种帮助功能与“run p6doc”或其他东西一起工作?
\n我使用 Windows10
\nWelcome to \xe2\x84\xa2 v2021.07.\nImplementing the \xe2\x84\xa2 programming language v6.d.\nBuilt on MoarVM version 2021.07.\n
Run Code Online (Sandbox Code Playgroud)\n introspection code-documentation rakudo read-eval-print-loop raku
我想获得 Raku 中惰性但有限的 Seq 的最后一个元素,例如:
my $s = lazy gather for ^10 { take $_ };
Run Code Online (Sandbox Code Playgroud)
以下内容不起作用:
say $s[* - 1];
say $s.tail;
Run Code Online (Sandbox Code Playgroud)
这些可以工作,但看起来不太惯用:
say (for $s<> { $_ }).tail;
say (for $s<> { $_ })[* - 1];
Run Code Online (Sandbox Code Playgroud)
在保持原始 Seq 惰性的同时,最惯用的方法是什么?
如何使点 ( .
) 元字符匹配 Raku 正则表达式中的换行符?在 Perl 中,我会使用点匹配换行符( /s
)?
The Rakudo implementation of Raku tracks multiple issues about the (very useful!) &?ROUTINE variable not providing the correct value (e.g., #1768 and 2362), so I realize that it's not behaving quite correctly. But I'm trying to understand what it's intended behavior is – which seems like an essential first step in fixing that behavior.
Running this code with Rakudo v2021.06 produces the output noted in the comments. Which parts of this output are correct, and which represent bugs?
sub …
Run Code Online (Sandbox Code Playgroud) raku ×10
rakudo ×8
compile-time ×2
cro ×2
concurrency ×1
immutability ×1
iterable ×1
newline ×1
regex ×1
zef ×1