我想在映射例程中使用数组的索引。例如,这个 Raku 代码:
raku -e 'my @a = "First", "Second", "Third", "First"; say @a.map({ "Index of $_ is: ;" })'
印刷:
(Index of First is: ; Index of Second is: ; Index of Third is: ; Index of First is: ;)
是否可以获取数组的索引,例如:
(Index of First is: 0; Index of Second is: 1; Index of Third is: 2; Index of First is: 3;)
谢谢!
在编写测试时,将在测试文件夹中读取文本文件,但不在该文件夹外部读取文本文件(即,在运行证明6时)。例如,以下代码在测试文件夹中读取ReadConfig.ini,但不在其外部读取:
my %v = ReadIni( 'ReadConfig.ini' );
Run Code Online (Sandbox Code Playgroud)
另一方面,此代码在test文件夹之外起作用:
my %v = ReadIni( $*PROGRAM.dirname.IO.add('ReadConfig.ini') );
Run Code Online (Sandbox Code Playgroud)
正确的语法是什么?
谢谢!
这是/ t中的通用代码,用于测试/ lib中的 .pm6模块是否加载。
use lib $*PROGRAM.sibling('../lib');
use Test;
my @dir = dir($*PROGRAM.sibling('../lib'), test => { $_ ~~ /.*pm6/ } );
plan @dir.elems;
sub module( IO $dir ) {
$dir.basename.Str ~~ /(\w+)\.pm6/;
return $0.Str;
}
for @dir.map(&module) -> $module {
use-ok $module, "This module loads: $module";
}
Run Code Online (Sandbox Code Playgroud)
在继续之前(递归地查看lib子文件夹),我想知道这是正确的方法。
谢谢!
使用cro stub http test test创建存根服务后,我在.cro.yml 中定义了 TEST_PORT 环境变量:
---
id: test
cro: 1
name: "test"
endpoints:
-
id: http
name: HTTP
protocol: http
host-env: TEST_HOST
port-env: TEST_PORT
links: []
entrypoint: service.p6
env:
- name: TEST_PORT
value: "3001"
...
Run Code Online (Sandbox Code Playgroud)
尽管如此,Cro 仍然使用默认的 20000 端口。Alex Schroeder导出环境变量以在不同的端口中启动 Cro。命令“ export TEST_PORT="3001" && cro run仍然使用默认端口。
在 Cro 中定义服务端口的正确形式是什么?
这个 Raku 表达式将颜色代码从 RGB 转换为 HEX:
raku -e 'my @array = (0, 255, 0), { @^a «+» (25.5, -25.5, 0) } ... ( * ~~ (255, 0, 0 ) );
say @array.map: "#" ~ *.fmt: "%02X"'
(#00 FF 00 #19 E5 00 #33 CC 00 #4C B2 00 #66 99 00 #7F 7F 00 #99 66 00 #B2 4C 00 #CC 33 00 #E5 19 00 #FF 00 00)
Run Code Online (Sandbox Code Playgroud)
将 q{} 添加到表达式会删除空格:
raku -e 'my @array = (0, 255, 0), …
Run Code Online (Sandbox Code Playgroud) 我已经尝试在raku OOP中编写了yangyanzhan的raku-riddle-contest解决方案。Raku 类系统非常直观,在我遇到递归函数之前,一切都很有趣。这是类和函数的代码版本:
class Encounters {
has $.tigers;
has @!encounters;
method encounters {
if @!encounters.elems eq $.tigers {
return [@!encounters, ];
}
my @total_encounters = [] ;
for 1..$.tigers -> $tiger {
if ($tiger / 2) eq ($tiger / 2).round {
@!encounters = ( @!encounters, [$tiger]).flat ;
my @encounter_events = Encounters.new( tigers => $.tigers, encounters => @!encounters ).encounters;
@total_encounters.append: @encounter_events;
}
}
return @total_encounters;
}
}
sub encounters($tigers, @encounters) {
if @encounters.elems eq $tigers {
return …
Run Code Online (Sandbox Code Playgroud) 控制台在启动cro('cro run')后显示此消息:
? Starting JoanPujol (JoanPujol)
**Endpoint HTTP will be at http://localhost:20000/**
JoanPujol Listening at http://localhost:3000
JoanPujol Shutting down...
? Restarting JoanPujol (JoanPujol)
JoanPujol Listening at http://localhost:3000
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚定义"端点HTTP"的位置.这是我的' service.p6 ':
use Cro::HTTP::Log::File;
use Cro::HTTP::Server;
use Routes;
constant HOST = 'localhost';
constant PORT = 3000;
my Cro::Service $http = Cro::HTTP::Server.new(
:http<1.1>,
host => HOST,
port => PORT,
application => routes(),
after => [
Cro::HTTP::Log::File.new(logs => $*OUT, errors => %*ERR)
]
);
$http.start;
say "Listening at http://{HOST}:{PORT}";
react {
whenever signal(SIGINT) { …
Run Code Online (Sandbox Code Playgroud) 该问题已移至“代码审查”中的该职位。请在代码审查中回答问题。谢谢!
受此颜色渐变生成器的启发,这是perl6中的渐变生成器。我想知道代码是否可以简化或改进。它将十六进制颜色转换为base10,计算渐变颜色,然后将颜色转换为base16。
my $initial_color = '#FF0000';
my $final_color = '#00FF00';
my $gradient = 10;
my @initial = ($initial_color ~~ /\#(..)(..)(..)/).list.map: { .Str.parse-base(16) };
my @final = ($final_color ~~ /\#(..)(..)(..)/).list.map: { .Str.parse-base(16) };
my @range = @final Z- @initial;
my @increment = @range.map: { $_ / $gradient };
my @color;
@color.push: @initial;
for (1..$gradient) -> $i {
@color.push: @(@color[$i-1]) Z+ @increment;
}
for (0..$gradient) -> $i {
@color[$i] = '#' ~ ( @(@color[$i]).map: { .base(16,0).fmt('%02s') } ).join; …
Run Code Online (Sandbox Code Playgroud) Perl6 Twitter模块使用搜索查询中的推文提供多维变量.这段代码:
%tweets<statuses>[0]<metadata><iso_language_code>.say;
%tweets<statuses>[0]<created_at>.say;
Run Code Online (Sandbox Code Playgroud)
打印:
es
Fri May 04 13:54:47 +0000 2018
Run Code Online (Sandbox Code Playgroud)
以下代码从搜索查询中打印推文的"created_at"值.
for @(%tweets<statuses>) -> $tweet {
$tweet<created_at>.say;
}
Run Code Online (Sandbox Code Playgroud)
是否有更好的语法来访问变量%tweets的值?
谢谢!