在Perl文档中,有一个关于.postfix/.postcircumfix的部分,它说
在大多数情况下,可以在后缀或后缀之前放置一个点:
my @a;
@a[1, 2, 3];
@a.[1, 2, 3]; # Same
Run Code Online (Sandbox Code Playgroud)
从技术上讲,不是真正的运营商; 它的语法特殊于编译器.
我试过自己:
> my @a = 1,2,3,4,5
> @a[1] # 2
> @a.[1] # 2
> my %a = Perl => 6, Python => 3, PHP => 7
> %a<Perl> #6
> %a.<Perl> #6
> my @weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
> @weekdays.antipairs.hash{'Sunday'} # 6, I expected it to be syntax wrong, but it did work!
> @weekdays.antipairs.hash.{'Sunday'} # 6, seems …Run Code Online (Sandbox Code Playgroud) 我定义了一个multi sub有两个签名:
multi sub mie(Str $s, Int $i) { $s x $i }
multi sub mie(Int $s, Int $i) { ... }
say &mie.signature; # ;; Mu | is raw)
Run Code Online (Sandbox Code Playgroud)
我想得到这个签名multi sub,但上面的结果不是我的预期.
正如文档所说,contains是一个多方法,有4个签名:
multi method contains(Str:D: Cool:D $needle)
multi method contains(Str:D: Str:D $needle)
multi method contains(Str:D: Cool:D $needle, Int(Cool:D) $pos)
multi method contains(Str:D: Str:D $needle, Int:D $pos)
Run Code Online (Sandbox Code Playgroud)
但是当我试图获得包含的签名时:
say "a string".^methods.pairs.values[8].value.signature;
Run Code Online (Sandbox Code Playgroud)
它只输出一个签名:
(Str: | is raw)
Run Code Online (Sandbox Code Playgroud)
在REPL中,当我调用contains没有参数的方法时,它输出以下错误:
> "a string".contains() …Run Code Online (Sandbox Code Playgroud) 我想每秒打印当前时间,也想睡5秒钟5秒钟:
react {
whenever Supply.interval(1) {
say DateTime.now.posix;
}
whenever Supply.interval(5) {
sleep 10;
say 'Sleep Done';
}
whenever signal(SIGINT) {
say "Done.";
done;
}
}
Run Code Online (Sandbox Code Playgroud)
输出不是我想要的:
1542371045
Sleep Done
1542371055
Sleep Done
1542371065
Sleep Done
1542371075
Done.
...
Run Code Online (Sandbox Code Playgroud)
我想要的是这个:
1542371045
1542371046
1542371047
1542371048
1542371049
Sleep Done
1542371059
1542371060
1542371061
1542371062
1542371063
Sleep Done
Done.
Run Code Online (Sandbox Code Playgroud)
不太了解Promise,Supply...关于Perl 6,这可能吗?
在Python中,如果我dict用另一个名为u(Perl用作键)的字典更新字典,它将更新值:
>>> dict = {'Python':'2', 'Perl' : 5}
>>> u = {'Perl' : 6}
>>> dict.update(u)
>>> dict
{'Python': '2', 'Perl': 6}
Run Code Online (Sandbox Code Playgroud)
但是在Perl 6中:
> my %hash = 'Python' => 2, Perl => 5;
> my %u = Perl => 6
> %hash.append(%u)
{Perl => [5 6], Python => 2}
Run Code Online (Sandbox Code Playgroud)
那么,Perl 6在字典上是否具有Python的更新方法?
我正在尝试将用Python编写的模块转换为Perl 6,我发现在Perl 6中没有AES方法:
from Cryptodome.Cipher import AES
import base64
def aes(text, key):
pad = 16 - len(text) % 16
text = text + bytearray([pad] * pad)
encryptor = AES.new(key, 2, b"0102030405060708")
ciphertext = encryptor.encrypt(text)
return base64.b64encode(ciphertext)
Run Code Online (Sandbox Code Playgroud)
在Perl 6中是否有任何模块编写实现AES方法?
我有一个Python的SnowFlake 脚本,我将其转换为Raku模块,并调用了 10,000,000 次,但速度非常慢(文件test.raku):
use IdWorker;
my $worker = IdWorker.new(worker_id => 10, sequence => 0);
my @ids = gather for (1...10000000) { take $worker.get_id() };
my $duration = now - INIT now;
say sprintf("%-8s %-8s %-20s", @ids.elems, Set(@ids).elems, $duration);
Run Code Online (Sandbox Code Playgroud)
正如@codesections 的回答所说,这now需要很多时间。
Python 大约需要 12 秒,而 Raku 需要几分钟。我怎样才能解决这个问题?
这个空的for循环大约需要 0.12 秒:
for (1...10000000) {
;
}
Run Code Online (Sandbox Code Playgroud)
和通话get_id()上$worker花费几分钟:
for (1...10000000) {
$worker.get_id();
}
Run Code Online (Sandbox Code Playgroud) 在Python中,Python有Union类型,当一个方法可以接受多种类型时,这很方便:
from typing import Union
def test(x: Union[str,int,float,]):
print(x)
if __name__ == '__main__':
test(1)
test('str')
test(3.1415926)
Run Code Online (Sandbox Code Playgroud)
Raku 可能没有 Python 那样的 Union 类型,但子句where可以达到类似的效果:
sub test(\x where * ~~ Int | Str | Rat) {
say(x)
}
sub MAIN() {
test(1);
test('str');
test(3.1415926);
}
Run Code Online (Sandbox Code Playgroud)
我想知道Raku是否有可能像Python一样提供Union类型?
# vvvvvvvvvvvvvvvvvvvv - the Union type doesn't exist in Raku now.
sub test(Union[Int, Str, Rat] \x) {
say(x)
}
Run Code Online (Sandbox Code Playgroud) 在Python中,__init__用于初始化类:
class Auth(object):
def __init__(self, oauth_consumer, oauth_token=None, callback=None):
self.oauth_consumer = oauth_consumer
self.oauth_token = oauth_token or {}
self.callback = callback or 'http://localhost:8080/callback'
def HMAC_SHA1():
pass
Run Code Online (Sandbox Code Playgroud)
Perl 6 中init的等效方法是什么?是方法new吗?
我有一个来自spark的JSON:
val df = spark.read.parquet("hdfs://xxx-namespace/20190311")
val jsonStr = df.schema.json
Run Code Online (Sandbox Code Playgroud)
jsonStr 就像这样:
{
"type":"struct",
"fields":[
{
"name":"alm_dm_list",
"type":{
"type":"array",
"elementType":"integer",
"containsNull":true
},
"nullable":true,
"metadata":{
}
},
{
"name":"data_batt_sc_volt_lowest",
"type":"double",
"nullable":true,
"metadata":{
}
},
{
"name":"veh_dcdcst",
"type":"integer",
"nullable":true,
"metadata":{
}
},
{
"name":"esd_temp_data",
"type":{
"type":"array",
"elementType":{
"type":"struct",
"fields":[
{
"name":"esd_temp_probe_cnt",
"type":"integer",
"nullable":true,
"metadata":{
}
},
{
"name":"esd_temp_probe_list",
"type":{
"type":"array",
"elementType":"integer",
"containsNull":true
},
"nullable":true,
"metadata":{
}
},
{
"name":"esd_temp_subsys_seq",
"type":"integer",
"nullable":true,
"metadata":{
}
}
]
},
"containsNull":true
},
"nullable":true,
"metadata":{
}
},
{ …Run Code Online (Sandbox Code Playgroud) 我想提取行键(这里是28_2820201112122420516_000000)、列名(这里是bcp_startSoc)和值(这里是64.0)$str,其中$str是 HBase 中的一行:
# `match` is OK
my $str = '28_2820201112122420516_000000 column=d:bcp_startSoc, timestamp=1605155065124, value=64.0';
my $match = $str.match(/^ ([\d+]+ % '_') \s 'column=d:' (\w+) ',' \s timestamp '=' \d+ ',' \s 'value=' (<-[=]>+) $/);
my @match-result = $match».Str.Slip;
say @match-result; # Output: [28_2820201112122420516_000000 bcp_startSoc 64.0]
# `smartmatch` is OK
# $str ~~ /^ ([\d+]+ % '_') \s 'column=d:' (\w+) ',' \s timestamp '=' \d+ ',' \s 'value=' (<-[=]>+) $/
# …Run Code Online (Sandbox Code Playgroud) raku ×10
perl6 ×4
python ×2
comb ×1
compilation ×1
grammar ×1
json ×1
match ×1
performance ×1
smartmatch ×1
union ×1