Raku 的优点之一是,在适当的时候(例如,除两个整数时),它会自动使用有理数而不是浮点数。\n不幸的是,一旦分母变得太大,无论如何都会使用浮点数。
\n有些FatRat
类型不这样做,但据我所知,使用它们的唯一方法是显式地这样做。
例如,此脚本计算 \xcf\x80 的数字:
\n#!/usr/bin/env raku\n\nunit sub MAIN(Int $decimals = 1_000);\n\nsub atan_repr(Int $n, Int :$decimals)\n{\n my $x = $n;\n my $n2 = $n\xc2\xb2;\n my $sign = 1;\n my $limit = 10**($decimals+2);\n my $result = FatRat.new(1, $x);\n for 3,5...* -> $i {\n $x \xc3\x97= $n2;\n $sign \xc3\x97= -1;\n $result += FatRat.new($sign, $i \xc3\x97 $x);\n last if $x \xe2\x89\xa5 $limit;\n }\n\n return $result;\n}\n\nmy $\xcf\x80 = 4 \xc3\x97 (4 \xc3\x97 atan_repr(5, :$decimals) - atan_repr(239, :$decimals));\nmy $\xcf\x80-str …
Run Code Online (Sandbox Code Playgroud) 假设我想要a,b和c中的2个字母的所有排列.
我可以:
my @perm = <a b c>.combinations(2)».permutations;
say @perm;
# [((a b) (b a)) ((a c) (c a)) ((b c) (c b))]
Run Code Online (Sandbox Code Playgroud)
这很接近,但不完全是我需要的.我如何"扁平化"这样以便得到:
# [(a b) (b a) (a c) (c a) (b c) (c b)]
Run Code Online (Sandbox Code Playgroud)
?
在Perl 6中找到列表中的最大值很容易:
> my @list = 1,4,9,7,3;
> say @list.max;
9
Run Code Online (Sandbox Code Playgroud)
但是如果我想找到最大条目的索引,似乎没有一种优雅的方法来做到这一点.
> say (^@list).sort({ -@list[$_] })[0];
2
> say @list.pairs.sort(*.value).tail.key;
2
> say @list.first(@list.max, :k);
2
Run Code Online (Sandbox Code Playgroud)
这些都是有效的,但它们并不优雅,更不用说高效了.
有一个更好的方法吗?
如果max
有:k
,:v
和:kv
选项,如例如,那将是很好的first
.当然,也有可能不会是一个独特的指数(例如,在的情况下(1,4,9,7,9).max
,但话又说回来,有可能不是一个独特的价值之一:
> dd (1, 2.0, 2.0e0, 2).max;
2.0
> say <the quick brown fox>.max(*.chars);
quick
Run Code Online (Sandbox Code Playgroud)
max
已经检索到第一个最大值,因此用(或)返回第一个索引是完全合理的.:k
:kv
我试图在Linux上的Perl脚本中确定它是否在终端中运行.
也就是说,我需要以下代码:
./myscript.pl | less
甚至还会返回true./myscript.pl </dev/null >/dev/null 2>/dev/null
特别是因为第二个子弹,我不能使用-t STDOUT
和变化,IO :: Interactive也没用.
信息似乎确实可用.如果我运行ps
,它显示的条目类似pts/2
的TTY
专栏,甚至当我运行./myscript.pl </dev/null >/dev/null 2>/dev/null
,并?
作为cron作业或CGI脚本运行的时候.
有没有一种优雅的方法来在Perl脚本中确定这个?我宁愿不必解析输出ps
.
这段代码:
sub MAIN(Int $N = 128)
{
my @pascal = ((1,), { (0, |@^a) Z+ (|@^a, 0) } ... *);
my @top = @pascal[^$N];
say "Percentage of odd numbers in the first $N rows: ",
(100 × @top».grep(* % 2).sum / @top.sum).fmt('%.12f%%');
}
Run Code Online (Sandbox Code Playgroud)
给我一个错误:
The iterator of this Seq is already in use/consumed by another Seq
(you might solve this by adding .cache on usages of the Seq, or
by assigning the Seq into an array)
in sub MAIN at …
Run Code Online (Sandbox Code Playgroud) 默认情况下,哈希将所有键转换为字符串.当您的密钥是可能接近的数字时,这会导致问题:
> my %h; %h{1/3} = 1; %h{0.333333} = 2; dd %h;
Hash %h = {"0.333333" => 2}
Run Code Online (Sandbox Code Playgroud)
当然,这可以修复如下:
> my %h{Real}; %h{1/3} = 1; %h{0.333333} = 2; dd %h;
Hash[Any,Real] %h = (my Any %{Real} = 0.333333 => 2, <1/3> => 1)
Run Code Online (Sandbox Code Playgroud)
但现在我需要哈希数字哈希,例如{ 1/3 => { 2/3 => 1, 0.666667 => 2 } }
.
> my %h{Real}; %h{1/3}{2/3} = 1; %h{1/3}{0.666667} = 2; dd %h;
Hash[Any,Real] %h = (my Any %{Real} = <1/3> => ${"0.666667" => 2}) …
Run Code Online (Sandbox Code Playgroud) enum Direction <north east south west>;
for north, east, south, west -> $dir {
say $dir;
...
}
Run Code Online (Sandbox Code Playgroud)
我不想在这里重复方向值列表。我如何以编程方式获取此列表?
我在文档中没有找到任何内容。最接近的是.enums
,但它返回(字符串)键到(整数)值的映射,而不是枚举值。
我试图在一个数字中找到一组重复的数字,例如12334555611
变成(1 2 33 4 555 6 11)
.
这有效:
$n.comb(/ 0+ | 1+ | 2+ | 3+ | 4+ | 5+ | 6+ | 7+ | 8+ | 9+ /)
Run Code Online (Sandbox Code Playgroud)
但不是很优雅。
有一个更好的方法吗?
返回没有第 n 个元素的列表的优雅且有效的方法是什么?我现在正在使用类似的东西:
my @b = @a;
@b.splice($n,1);
return @b;
Run Code Online (Sandbox Code Playgroud)
但这不是很优雅,而且可能也不是高效的。
(类似的东西return @b.spliced($n,1)
会更好,但这不起作用。)
return flat @a[0..^$n,$n^..*]
Run Code Online (Sandbox Code Playgroud)
也好不了多少。
以下代码有效:
my @fibo = 1, 1, -> $a, $b { $a + $b } ... *;
say @fibo[^10];
Run Code Online (Sandbox Code Playgroud)
输出:
(1 1 2 3 5 8 13 21 34 55)
Run Code Online (Sandbox Code Playgroud)
但是如果你将其分成多行,如下所示:
my @fibo = 1,
1,
-> $a, $b { $a + $b }
...
*;
say @fibo[^10];
Run Code Online (Sandbox Code Playgroud)
它给出了一个错误:
*
in block <unit> at ./dotdotdot line 4
Run Code Online (Sandbox Code Playgroud)
在这种情况下, 似乎...
被解释为 yada 运算符。但不应该,不是吗?两个版本的代码应该是等效的。(这不是 Python,空格应该是无关紧要的。)
作为解决方法,以下内容确实可以正常工作:
my @fibo = 1,
1,
-> $a, $b { $a + $b } ...
*;
say …
Run Code Online (Sandbox Code Playgroud)