我有以下课程:
class Names {
has @!names;
method add-name( $name ) { @!names.push($name) }
multi method AT-POS( ::?CLASS:D: $index ) {
my $new-names-obj = Names.new;
for @!names[$index] -> $name {
$new-names-obj.add-name($name);
}
return $new-names-obj;
}
method gist {
@!names.join("\n")
}
}
Run Code Online (Sandbox Code Playgroud)
我希望能够对一个Names对象进行切片,并且返回的值应该是另一个Names对象,其元素是从原始Names对象中切下的。例如:
my $original = Names.new;
$original.add-name($_) for <jonathan joseph jotaro josuke giorno>;
my $sliced-off = $original[0..2];
say $original.^name; #=> Names
say $original; #=> jonathan, joseph, jotaro, josuke, giorno
say $sliced-off.^name; #=> List
say $sliced-off; …Run Code Online (Sandbox Code Playgroud) 我正在学习 JS 异步编程,我忍不住注意到 JS 和 Raku 都有一些同名的异步编程结构,但是我不确定一个知识可以在多大程度上转移到另一个。我尝试阅读JS to Raku ,但关于异步编程的部分大多是贫乏的。
例如,在 Raku 中可以做这样的事情吗?
fetch('https://jsonplaceholder.typicode.com/todos/1')
.then(response => response.json())
.then(json => console.log(json))
Run Code Online (Sandbox Code Playgroud)
或者如果我想创造自己的承诺,类似这样的事情吗?
function getLanguages() {
return new Promise((resolve, reject) => {
setTimeout(() => {
const success = Math.random() >= 0.5;
if (success) {
const languages = ['js', 'perl', 'python', 'raku'];
resolve(languages);
}
else {
reject(new Error('No languages'));
}
}, 0);
});
}
getLanguages()
.then((languages) => {
console.log(languages);
})
.catch((error) => {
console.log(error);
});
Run Code Online (Sandbox Code Playgroud) 我发现了这个 JS 代码片段:
function append(demo = "", file = "") {
const extra = "ctl=1&embed=1";
if (demo && file) return `/${demo}/?file=${file}&${extra}`;
if (!demo && file) return `/?file=${file}&${extra}`;
if (demo && !file) return `/${demo}&${extra}`;
return `?${extra}`;
}
Run Code Online (Sandbox Code Playgroud)
我可以使用ifs 轻松地将其移植到 Raku,但我认为使用given/when将是展示该功能的好方法。我想出了以下几点:
sub append(Str :$demo, Str :$file) {
my $extra = "ctl=1&embed=1";
given ($demo, $file) {
when $demo.so, $file.so { "/$demo/?file=$file&$extra" }
when $demo.not, $file.so { "/?file=$file&$extra" }
when $demo.so, $file.not { "/$demo?$extra" }
default { "?$extra" …Run Code Online (Sandbox Code Playgroud) 在 JavaScript (ES6) 中,您可以使用模板文字 (``) 创建多行字符串,如以下示例所示:
const html = `
<div>
<p>Raku is <b>ofun</b>.</p>
</div>
`
Run Code Online (Sandbox Code Playgroud)
Raku 相当于什么?
正如文档中所解释的,您可以通过在前面添加 sigil 来引用现有函数&:
&say # reference to the `say` function
&infix:<+> # reference to the infix `+` operator
Run Code Online (Sandbox Code Playgroud)
我该如何为方法执行此操作?
假设我.pm6在目录中有以下两个文件Foo:
Vehicle.pm6 -车辆的接口。=TITLE C<Vehicle> interface
unit role Foo::Vehicle;
#| Get the vehicle to move
method run(--> Nil) { ... }
#| Get fuel into the vehicle
method get-fuel(--> Nil) { ... }
Run Code Online (Sandbox Code Playgroud)
Car.pm6-实现Vehicle接口的类。=TITLE C<Car> class
use Foo::Vehicle;
unit class Foo::Car does Foo::Vehicle;
has $!speed = 2;
#| Get the car to move
method run(--> Str) {
"{::?CLASS.perl} is moving at {$!speed}km/h."
}
#| Get fuel into the car
method get-fuel(--> Str) …Run Code Online (Sandbox Code Playgroud) Imagine that you have a module X whose functionalities are available to the user through a Command Line Interface. Such module doesn't do much in and of itself but it allows for other people to create plugin-like modules they can hook-up to module X. Ideally, the plugins could be used through X's CLI.
Thus my question:
What do you need to do in order to hook-up whatever functionality
a plugin might provide to X's CLI?
This …
假设我有一个具有多个属性的类,但我只需要其中的一些用于对象构造;其余属性的值取决于这些公共属性。但是,我仍然想使用以属性命名的方法访问其余属性的值。在我的脑海里,有两个选择:
第一个选项:
new方法。或者保持new方法BUILD不变,而是使用子方法将传递的参数设置为正确的属性,并使用此参数的值设置剩余的属性。class Foo {
has Int $.a;
has Int $.b;
has Int $.c;
submethod BUILD(:$!a) {
$!b = $!a ** 2;
$!c = $!a ** 3;
}
}
Run Code Online (Sandbox Code Playgroud)
say Foo.new(:2a, :1b); #=> Foo.new(a => 2, b => 4, c => 8)
say Foo.new(:2a, :1b).b; #=> 8
Run Code Online (Sandbox Code Playgroud)
第二种选择:
$.仅声明对象构建所需的那些属性,并在对象构建后使用子方法修改其余属性(用 声明$!)TWEAK。但是,现在我需要为这些属性创建访问器方法。
class Bar {
has Int $.a;
has Int $!b;
has Int $!c;
submethod TWEAK {
$!b …Run Code Online (Sandbox Code Playgroud) 在 C++ 中,您可以创建在模板化对象上使用特定运算符的模板化类,并且实例化这些对象的类必须重载该特定运算符才能使其对象与模板化类一起使用。例如,insertionBST 实现的方法可能依赖于<运算符,因此任何要存储在 BST 中的对象都必须实现该运算符。
如果可能,我如何对 Raku 中的参数化角色执行相同的操作?
为了提供一些上下文,例如将以下参数化角色定义为其自己的模块:
role BST[::T] {
my role BinaryNode[::T] {
has T $.item is rw;
has BinaryNode $.left is rw;
has BinaryNode $.right is rw;
}
has BinaryNode $!root;
method insert( BST:D: T $x --> Nil ) {
self!rec-insert($x, $!root)
}
method !rec-insert( T $x, BinaryNode $node is rw --> Nil ) {
if !$node.defined { $node = BinaryNode[$(T)].new(item => $x) }
elsif $x < $node.item { self!rec-insert($x, $node.left) …Run Code Online (Sandbox Code Playgroud) 例如,在此类中,Foo两个方法b和c返回相同的值:
class Foo {
method a { 42 }
method b { $.a }
method c { self.a }
}
my $foo = Foo.new;
say $foo.a; #=> 42
say $foo.b; #=> 42
say $foo.c; #=> 42
Run Code Online (Sandbox Code Playgroud)
我注意到我可以对其他 sigil 和 twigil 组合做同样的事情,例如@.scoresvs self.score、%.matchesvsself.matches等。
编辑:我将这个问题标记为重复,但是正如 librasteve 指出的那样,我没有意识到另一个问题的答案中提到的微妙区别不是焦点,并且可能很容易被错过,特别是对于 Raku 的新手来说。
raku ×10
perl6 ×2
asynchronous ×1
attributes ×1
class ×1
function ×1
javascript ×1
methods ×1
multiline ×1
object ×1
oop ×1
promise ×1
rakudo ×1
reference ×1
slice ×1
string ×1
subroutine ×1