小编pmq*_*mqs的帖子

Raku有Python的Union类型吗?

在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 union raku

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

在超级方法链中使用带有收集/接收的 CALL-ME 不起作用

一直在玩方法链和CALL-ME

下面是我用来玩的一个玩具类。它CALL-ME只是调用double给定值的 Seq 方法。

class Math does Callable
{
    method  CALL-ME(Iterable:D $i)
    {
        gather {
            $i.map: -> $v { take self.double($v) } ;
        }
    }

    method double($x) { return $x * 2}
}

my $d = Math.new();
Run Code Online (Sandbox Code Playgroud)

首先检查是否可以将Math对象包含$d在方法链中。

这段代码

(1,2,3).map( * + 1 ).$d.map( * +1).say;
Run Code Online (Sandbox Code Playgroud)

输出正确的结果

(5 7 9)
Run Code Online (Sandbox Code Playgroud)

现在放入hyper方法链中

(1,2,3).hyper.map( * + 1 ).$d.map( * + 1 ).say;
Run Code Online (Sandbox Code Playgroud)

这次的输出是

A worker in a parallel iteration (hyper or …
Run Code Online (Sandbox Code Playgroud)

raku

7
推荐指数
1
解决办法
157
查看次数

如何在 Perl 命令中扩展 shell 变量

我想使用 shell 变量替换字符串。

\n
today =`date '+%Y%m%d'`\n\nperl -p -i -e 's/file"20221212"/file="${today}"/g' \n
Run Code Online (Sandbox Code Playgroud)\n

期望是file="20221215"。\n但失败了,结果是file=""

\n

如何逃脱这种情况\xef\xbc\x9f

\n

shell perl

2
推荐指数
1
解决办法
156
查看次数

Python - 为什么 enumerate() 导致稍后的 zip() 仅从列表中的最后一项提取?

代码:

boylist = ['Jim', 'James', 'Jack', 'John', 'Jason']
for i, boylist in enumerate(boylist):
    print(f'Index {i} is {boylist} in my list')
#boylist = ['Jim', 'James', 'Jack', 'John', 'Jason']
girllist = ['Emma', 'Clara', 'Susan', 'Jill', 'Lisa']
for boylist, girllist in zip(boylist, girllist):
    print(f'{boylist} and {girllist} form a nice couple')\
Run Code Online (Sandbox Code Playgroud)

输出:

Index 0 is Jim in my list
Index 1 is James in my list
Index 2 is Jack in my list
Index 3 is John in my list
Index 4 is Jason …
Run Code Online (Sandbox Code Playgroud)

python string list enumerate python-zip

2
推荐指数
1
解决办法
53
查看次数

标签 统计

python ×2

raku ×2

enumerate ×1

list ×1

perl ×1

python-zip ×1

shell ×1

string ×1

union ×1