这就是我所期待的.fib(13)返回233.
sub fib(Int $a --> Int) {
return 0 if $a == 0;
return 1 if $a == 1;
return fib($a -1) + fib($a -2);
}
my $square = -> $x { $x * 2 }; # this works with no return value
my @list = <1 2 3 4 5 6 7 8 9>.map( $square );
# returns [2 4 6 8 10 12 14 16 18]
Run Code Online (Sandbox Code Playgroud)
我尝试使用匿名子实现fib()
my $fib = -> Int $x --> Int {
return 0 …Run Code Online (Sandbox Code Playgroud) 这是来自欧拉项目的问题 36。对以 2 为底和以 10 为底的所有回文数低于 100 万的数字求和。
我最初尝试以更实用的方式解决它。
这在不到 6 秒的时间内运行。
[1..1_000_000]
.grep( * !%% 2 )
.grep( -> $x { $x == $x.flip } )
.grep( -> $y { $y.base(2) == $y.base(2).flip } )
.sum.say
Run Code Online (Sandbox Code Playgroud)
令人惊讶的是,这花了 12 秒,即使我只生成奇数,因此跳过偶数测试。
(1,3 ... 1_000_000)
.grep( -> $x { $x == $x.flip } )
.grep( -> $y { $y.base(2) == $y.base(2).flip } )
.sum.say
Run Code Online (Sandbox Code Playgroud)
这将在大约 3 秒内运行。
my @pals;
for (1,3 ... 1_000_000) -> $x {
next unless $x == $x.flip;
next …Run Code Online (Sandbox Code Playgroud) 我正在尝试在repl中创建一些类,当我创建第二个类具有与第一个类相同的属性时,我告诉它已经定义了.
这是repl没有正确处理命名空间的问题吗?它在文件中按预期工作.
Perl6版本:这是在MoarVM版本2018.10上构建的Rakudo版本2018.10,实现了Perl 6.c.
> class Thing {
* has $.stuff;
* }
(Thing)
> class OtherThing {
* has $.stuff;
* }
Package 'OtherThing' already has an attribute named '$!stuff'
in any scope_declarator at /home/path/.rakudobrew/moar-2018.10 /install/share/nqp/lib/Perl6/Grammar.moarvm line 1
in any term:sym<scope_declarator> at /home/path/.rakudobrew/moar-2018.10/install/share/nqp/lib/Perl6/Grammar.moarvm line 1
in any term at /home/path/.rakudobrew/moar-2018.10/install/share/nqp/lib/Perl6/Grammar.moarvm line 1
Run Code Online (Sandbox Code Playgroud) 我有以下代码:
$r->find('user')->via('post')->over(authenticated => 1);
Run Code Online (Sandbox Code Playgroud)
鉴于该路由,我可以通过使用Mojolicious :: Plugin :: Authentication设置的经过身份验证的检查到达用户路由.
我想在那条路线上添加另一个"结束".
$r->find('user')->via('post')->over(authenticated => 1)->over(access => 1);
Run Code Online (Sandbox Code Playgroud)
这似乎覆盖了经过身份验证的'over'.
我尝试用以下名称分解路线:
my $auth = $r->route('/')->over(authenticated => 1)
->name('Authenticated Route');
$access = $auth->route('/user')->over(access => 1)->name('USER_ACCESS');
Run Code Online (Sandbox Code Playgroud)
但这根本不起作用.两者都没有被访问过.
我的路由是/ user,/ item,使用MojoX :: JSON :: RPC :: Service设置.所以,我没有/ user /:id之类的东西来设置子路由.(不确定是否重要)所有路由都像/ user,带参数发送.
我有一个像这样的条件:
$r->add_condition(
access => sub {
# do some stuff
},
);
Run Code Online (Sandbox Code Playgroud)
这是$ r-> route('/ user') - > over(access => 1)中的'access';
简而言之,使用时路线工作正常:
$r->find('user')->via('post')->over(authenticated => 1);
Run Code Online (Sandbox Code Playgroud)
但是我无法添加第二条路线.
那么,在设置具有多个条件的这些路线时我缺少什么?是否可以向单个路由/ route_name添加多个条件?
我期待收到uri的喜欢
/user/*/account/*
Run Code Online (Sandbox Code Playgroud)
我有一个用户函数定义为
sub user :Path('/user') :PathPart('') :ActionClass('REST' ) {}
Run Code Online (Sandbox Code Playgroud)
然后
sub user_GET :PathPart('user') Chained('/') CaptureArgs(1) {
#do stuff
}
Run Code Online (Sandbox Code Playgroud)
对于帐户我也是类似地定义它们.
sub account :Path('/account') :PathPart('') :ActionClass('REST') {}
sub account_GET :PathPart('account') Chained('user_GET') Args(1) {
#do stuff
}
Run Code Online (Sandbox Code Playgroud)
所以,问题是当我将account_GET中的Chained设置为'user_GET'时,服务器调试显示路径已设置:
[debug] Loaded Chained actions:
.-----------------------------+--------------------------------------.
| Path Spec | Private |
+-----------------------------+--------------------------------------+
| /user/*/account/* | /mcp/user_GET (1) |
| | => /mcp/account_GET |
'-----------------------------+--------------------------------------'
Run Code Online (Sandbox Code Playgroud)
当我将account_GET中的Chained设置为'user'时,服务器调试显示:
[debug] Unattached Chained actions:
[debug] Unattached Chained actions:
.-------------------------------------+--------------------------------------.
| Private | Missing parent |
+-------------------------------------+--------------------------------------+
| /mcp/account_GET …Run Code Online (Sandbox Code Playgroud) 注意:我正在使用这是Rakudo Star版本2019.03.1在REPL上进行所有操作,这是在实现Perl 6.d的MoarVM版本2019.03上构建的Rakudo Star版本2019.03.1。
在euler项目#22中,有一个names.txt文件,类似于“ JERE”,“ HAI”,“ ELDEN”,“ DORSEY”,“ DARELL”,“ BRODERICK”,“ ALONSO”,...
当我读到它时,将其拆分和排序,即可得到我所期望的名称列表。
for '../names.txt'.IO.slurp.split(',').sort -> $name {
say $name;
}
Run Code Online (Sandbox Code Playgroud)
打印出来
...
"ZONIA"
"ZORA"
"ZORAIDA"
"ZULA"
"ZULEMA"
"ZULMA"
Run Code Online (Sandbox Code Playgroud)
现在,如果我添加 comb()
for '../names.txt'.IO.slurp.split(',').sort -> $name {
say $name.comb;
}
Run Code Online (Sandbox Code Playgroud)
我越来越
...
(" Z O N I A ")
(" Z O R A ")
(" Z O R A I D A ")
(" Z U L A ")
(" Z U L E M A ")
(" Z U …Run Code Online (Sandbox Code Playgroud)