小编uzl*_*xxx的帖子

在Perl 6中,如何将模块的pod保留在文件的底部,同时仍然使用声明块来记录方法/子?

假设我有以下模块:

module Simple-Mod;

#| Calculate the nth fibonacci number.
multi fib( 0 ) { 1 }
multi fib( 1 ) { 1 }
multi fib( Int $n where * > 1 ) {
    fib($n - 2 ) + fib($n - 1);
}

#| Say hello to  a person.
sub hello( $person ) { say "Hello, $person!" }

=begin pod
=head1 SYNOPSIS

A really simple module.

=head1 Example
=begin code
use Simple-Mod;

say fib(3);    #=> 2
hello("Gina"); #=> Hello, Gina!

=end …
Run Code Online (Sandbox Code Playgroud)

perl6 raku

10
推荐指数
1
解决办法
131
查看次数

JavaScript 和 Python 中使用的 super 关键字在 Raku 中的等价物是什么?

每当您在 JavaScript 或 Python 中扩展类时,派生类都必须使用super关键字才能设置属性和/或调用基类中的方法和构造函数。例如:

class Rectangle {
    constructor(length, width) {
        this.name = "Rectangle";
        this.length = length;
        this.width = width;
    }

    shoutArea() {
        console.log(
            `I AM A ${this.name.toUpperCase()} AND MY AREA IS ${this.length * this.width}`
        );
    }
    
    rectHello() {
        return "Rectanglish: hello";
    }
}

class Square extends Rectangle {
    constructor(length) {
        super(length, length);
        this.name = "Square"
    }
    
    squaHello() {
        const h = super.rectHello();
        return "Squarish:" + h.split(':')[1];
    }
}

const rect = new Rectangle(6, 4);
rect.shoutArea(); //=> I …
Run Code Online (Sandbox Code Playgroud)

javascript python oop inheritance raku

10
推荐指数
1
解决办法
356
查看次数

在 for 循环中用 .subst 替换字符串

我想for使用命名捕获在块中进行字符串替换。我期望得到数字 1,2,3 作为输出。但它Nil用于第一次运行,然后是 1 和 2 用于第二次和第三次运行。如何.subst在循环结构中正确使用?当使用map构造而不是for循环时,我看到了相同的行为。如果我用固定的字符串值替换它,它确实按预期工作。

for <a1 b2 c3> -> $var {
    say $var;
    say $var.subst(/.$<nr>=(\d)/, $<nr>); #.subst(/.$<nr>=(\d)/, 'X'); #OK      
}

#`[
This is Rakudo version 2019.11 built on MoarVM version 2019.11   
Output:

a1
Use of Nil in string context
  in block  at test3.pl6 line 3

b2
1
c3
2
]
Run Code Online (Sandbox Code Playgroud)

string-substitution raku

9
推荐指数
1
解决办法
207
查看次数

可以在 Raku 中导出子集吗?

我想定义一些子集,我还die为一些有用的错误消息添加了一些约束和一些语句。我不想在使用这些子集的模块的顶部定义它们,而是想将它们放在另一个模块中,同时也不要使用它们的完全限定名称 (FQN)。例如,我有

unit module Long::Module::Subsets;

subset PosInt
where ($_ ~~ Int || "The value must be an integer")
   && ($_ > 0    || "The value must be greater than 0")
is export
;

# other subsets ...
Run Code Online (Sandbox Code Playgroud)

但得到

===SORRY!=== Error while compiling /tmp/637321813/main.pl6
Two terms in a row ...
Run Code Online (Sandbox Code Playgroud)

那不起作用我想我可以做以下事情,但我想知道我是否可以避免这样做:

use Long::Module::Subsets;

unit Long::Module;

my constant PosInt = Long::Module::Subsets::PosInt;
my constant Byte   = Long::Module::Subsets::Byte;
# ... more subsets here

# ... some code here

my PosInt $age;
Run Code Online (Sandbox Code Playgroud)

module subset raku

9
推荐指数
1
解决办法
125
查看次数

如何使用 Raku 将十六进制字符串转换为 IEEE754 浮点数?

在Python中,unpack可以将十六进制字符串转换为IEEE754浮点数:

import struct

print(struct.unpack('<f', bytes.fromhex("00000042"))[0]) # 32.0
Run Code Online (Sandbox Code Playgroud)

<表示 LITTLE ENDIAN 字节顺序,f表示 Float 格式。

如何使用 Raku 将十六进制字符串转换为 IEEE754 浮点数?

unpack pack raku

9
推荐指数
1
解决办法
154
查看次数

内容检查部分而非全部类属性

我有一个带有属性的类。我想检查是否定义了一些但不是全部。所以:

class A { 
    has $.a is rw;
    has $.b is rw;
    has $.c is rw;
    has $.d is rw;

    method delete { ... }
}

my A $x .= new(:a<hi>, :d<good>);

## later
$x.b = 'there';

## code in which $x.c may or may not be defined.

## now I want to check if the attributes a, b, and c are defined, without
## needing to know about d
my Bool $taint = False;
for <a b c> { …
Run Code Online (Sandbox Code Playgroud)

class introspection raku

8
推荐指数
1
解决办法
85
查看次数

Raku 中的“我的”和“我们的”有什么区别?

此外,什么标准应该告诉您使用一个而不是另一个?


我发现了一些 SO 答案/评论 ([1][2][3]),它们主要回答了标题的问题,但是我认为将它放在一个地方会更好。这个问题的灵感来自Perl 中的一个类似问题。

scope raku

8
推荐指数
0
解决办法
172
查看次数

为什么 for 循环中带有 Match 的尖头块输出 Nil?

在我看来,有效的Match 对象不是空的或未定义的:

say Match.new(:orig("20230213112803"), :from(4), :pos(6)).elems;   # 0
say Match.new(:orig("20230213112803"), :from(4), :pos(6)).chars;   # 2
say Match.new(:orig("20230213112803"), :from(4), :pos(6)).defined; # True
Run Code Online (Sandbox Code Playgroud)

但以下带有循环输出Match中的对象的尖头块:forNil

for Match.new(:orig("20230213112803"), :from(4), :pos(6)) -> $m {
    say ~$m
}
# OUTPUT: Nil
Run Code Online (Sandbox Code Playgroud)

但该given声明输出了我所期望的:

given Match.new(:orig("20230213112803"), :from(4), :pos(6)) -> $m {
    say ~$m
}
# OUTPUT: 02
Run Code Online (Sandbox Code Playgroud)

是不是有一些空的或未定义的东西阻止了for循环的执行?

for "a" -> $m { say $m } # a
for []  -> $m { say $m } # Nil
for …
Run Code Online (Sandbox Code Playgroud)

regex raku

8
推荐指数
1
解决办法
163
查看次数

如何反省Raku运营者?

我可以反省 Raku 中的例程,例如say 例程:

[5] >  &say.WHAT                                                                                                                                                                   
(Sub)
[6] >  &say.^mro                                                                                                                                                                   
((Sub) (Routine) (Block) (Code) (Any) (Mu))
Run Code Online (Sandbox Code Playgroud)

但我如何自省诸如+doc page)之类的操作符?

[7] >  &+.WHAT                                                                                                                                                                     
Use of uninitialized value of type Callable in numeric context
  in block <unit> at <unknown file> line 1
  in any <main> at /home/toni/.rakubrew/versions/moar-blead/install/share/perl6/runtime/perl6.moarvm line 1
  in any <entry> at /home/toni/.rakubrew/versions/moar-blead/install/share/perl6/runtime/perl6.moarvm line 1

Use of uninitialized value of type Any in numeric context
  in block <unit> at <unknown file> line 1
  in any <main> …
Run Code Online (Sandbox Code Playgroud)

function operators raku

8
推荐指数
2
解决办法
147
查看次数

如何创建一个与 Raku 中的内置类同名的类,而又不会掩盖它?

这很愚蠢,我知道显而易见的事情就是简单地以不同的方式命名该类,但我仍然很好奇。在类中ColumnType::Date,我想typecast返回一个 Raku 的Date对象,而不是一个ColumnType::Date

module ColumnType {
    class Date {
        method typecast(Blob:D $value) {
            my $date = $value.decode('ascii').trim;
            my ($year, $month, $day) = $date.split('/');
            return try Date.new: :$year, :$month, :$day;
        }
    }
}

my $blob = "1980/01/01".encode('ascii');
say ColumnType::Date.new.typecast($blob);
Run Code Online (Sandbox Code Playgroud)

换句话说,是否可以将 Raku's 引用Date为完全限定的名称?

raku

8
推荐指数
1
解决办法
93
查看次数