给定单词abcd
,我将如何构建以下嵌套列表?
[ (abcd) (a bcd) (ab cd) (abc d) (a b cd) (a bc d) (ab c d) (a b c d) ]
Run Code Online (Sandbox Code Playgroud)
即以各种可能的方式拆分单词,同时保持字母顺序。
#raku-beginner 上的 Nemokosch 向我指出了.combinations
该模块snip
,但我无法将它们组合在一起。
enum Colors<red green blue>
say red; # OUTPUT: red
my $foo = "red";
my Colors $color = $foo.(...)
Run Code Online (Sandbox Code Playgroud)
我应该在存根中放入什么代码才能将 Str“红色”转换为颜色红色?
#!/usr/bin/env raku
use v6.d;
sub MAIN (
:$foo = 42, #= A test
) {
run $*EXECUTABLE, '--doc', $*PROGRAM;
}
=begin pod
=head1 Bar
blah, blah, blah
=head2 Baz
yadda, yadda, yadda
=end pod
Run Code Online (Sandbox Code Playgroud)
输出:
class Mu $
A test
Bar
blah, blah, blah
Baz
yadda, yadda, yadda
Run Code Online (Sandbox Code Playgroud)
那在那里做什么class Mu $\nA test
?
doc=HTML
和也会发生这种情况doc=Markdown
。 doc=Man
给出这个错误:
Unknown POD element of type 'Pod::Block::Declarator': Pod::Block::Declarator.new(WHEREFORE => Mu $, config => {}, contents => [])
in method pod-node at …
Run Code Online (Sandbox Code Playgroud) 我想在应用程序加载/初始化时显示进度条。
该代码不起作用,但应该让您了解我想要完成的任务。
my Bool $done-compiling = False;
BEGIN {
start repeat {
print '*';
sleep 0.33;
} until $done-compiling;
};
INIT {
$done-compiling = True;
};
Run Code Online (Sandbox Code Playgroud)
是否触发了我可以在 BEGIN 块中响应的事件?
my %f;
for $*HOME.dir() -> $file {
my $filename = $file.basename;
%f{$filename}.push: $file, rand;
}
my $p = %f.pick; # just need any old random element
say $p.^name;
say "{$p.values.^name} has {$p.values.elems} elements";
say "{$p.values[0].^name} has {$p.values[0].elems} elements";
say '';
say $*RAKU;
say $*DISTRO;
say $*KERNEL;
say $*VM;
Run Code Online (Sandbox Code Playgroud)
输出:
Pair
Seq has 1 elements
Array has 2 elements
Raku (6.d)
macos (13.2.1)
darwin
moar (2023.02)
Run Code Online (Sandbox Code Playgroud)
为什么是.values
of $p
a Seq
of Array
,而不是 simple Array
?
此代码按预期工作:
\nsub infix:<mean>(*@a) {\n @a.sum / @a.elems\n}\nsub Mean (*@a) {\n @a.sum / @a.elems\n}\n\nsay EVAL 'Mean 2, 6, 4'; # Output: 4\nsay EVAL '2 mean 6 mean 4'; # Output: 4\n
Run Code Online (Sandbox Code Playgroud)\n当第 7 行在其自己的范围内时,它会按预期工作:
\n{say EVAL 'Mean 2, 6, 4';} # Output: 4\n
Run Code Online (Sandbox Code Playgroud)\n但是当第 8 行在它自己的作用域内时:
\n{say EVAL '2 mean 6 mean 4';} \n\n===SORRY!=== Error while compiling .../EVAL_1\nTwo terms in a row\nat .../EVAL_1:1\n------> 2\xe2\x8f\x8f mean 6 mean 4\n expecting any of:\n infix\n infix stopper\n statement end\n …
Run Code Online (Sandbox Code Playgroud) 我想实施:
Press any key to continue...
Run Code Online (Sandbox Code Playgroud)
它可以响应任何键,而无需按Return或Enter键。
Term::ReadKey
不实现此行为。它需要一个 CRLF。
我正在使用iTerm2。
[19] > $*KERNEL
darwin
[20] > $*DISTRO
macos (13.0.1)
[21] > $*RAKU
Raku (6.d)
[22] > $*VM
moar (2022.12)
Run Code Online (Sandbox Code Playgroud) 我正在尝试为类方法创建一个可调用变量。
class Person {
method walk(Str $direction) {
say "Walking $direction";
}
}
Run Code Online (Sandbox Code Playgroud)
我可以为“walk”方法创建一个可按预期工作的可调用变量。
my $person = Person.new;
my $callable = $person.^find_method('walk');
$person.$callable('up'); # OUTPUT: "Walking up"
Run Code Online (Sandbox Code Playgroud)
现在我想创建一个可调用函数,它将使用参数“up”调用方法“walk”。
my $callable = $person.^find_method('walk').assuming('up');
$person.$callable();
# Type check failed in binding to parameter '$direction'; expected Str but got Person (Person.new)
# in sub __PRIMED_ANON at EVAL_7 line 1
# in block <unit> at <unknown file> line 1
Run Code Online (Sandbox Code Playgroud)
$person.$callable
(不带括号)给出相同的错误
我尝试“直接”调用它,但出现了不同的错误。
$person.^find_method('walk')('up')
# Too few positionals passed; expected 2 arguments but got 1 …
Run Code Online (Sandbox Code Playgroud) my @foo;
@foo = (1, (2, 3), (4, (5, 6), 7), (8), 9).List;
say @foo.flat;
# OUTPUT: (1 (2 3) (4 (5 6) 7) 8 9)
# this is NOT the output I expected.
@foo = (1, (2, 3), (4, (5, 6), 7), (8), 9);
say @foo.List.flat;
# OUTPUT: (1 2 3 4 5 6 7 8 9)
# this is the output I expected.
say $*DISTRO; # macos (12.6)
say $*VM; # moar (2022.07)
say $*RAKU; # Raku (6.d) …
Run Code Online (Sandbox Code Playgroud) foo.raku
:
#! /usr/bin/env raku
use v6.d;
sub MAIN (Str $some-text = $*IN.slurp, Bool :$verbose) {
say "Your text:" if $verbose;
say $some-text;
}
Run Code Online (Sandbox Code Playgroud)
当我运行它时
~% echo "Hello World" | ./foo.raku --verbose
Run Code Online (Sandbox Code Playgroud)
我得到:
Your text:
Run Code Online (Sandbox Code Playgroud)
如何写入签名MAIN
以便它捕获通过管道输入的字符串?