Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Raku 使用Perl6自定义运算符_Raku_Custom Operator - Fatal编程技术网

Raku 使用Perl6自定义运算符

Raku 使用Perl6自定义运算符,raku,custom-operator,Raku,Custom Operator,我在大学学习化学,想尝试用Perl6或Perl编写教科书示例,比如平衡化学公式或其他过程 然后我遇到的问题是关于perl6的自定义运算符。我觉得在使用该功能时,我一直在重复我的代码和我自己。 读写起来很难。我如何简化这一点 #!/usr/bin/env perl6 use v6; #basic SI(International System of Units) type role MetricPrefix { method baseOn ( Str $base , Numeric

我在大学学习化学,想尝试用Perl6或Perl编写教科书示例,比如平衡化学公式或其他过程

然后我遇到的问题是关于perl6的自定义运算符。我觉得在使用该功能时,我一直在重复我的代码和我自己。 读写起来很难。我如何简化这一点

#!/usr/bin/env perl6
use v6;
#basic SI(International System of Units) type 


role MetricPrefix {
    method baseOn ( Str $base , Numeric $input ) {
        given $base {
            when 'pico' { return $input * 10**-12 }
            when 'namo' { return $input * 10**-9 }
            when 'micro' { return $input * 10**-6}
            when 'milli' { return $input * 10**-3 }
            when 'centi' { return $input * 10**-2 }
            when 'hecto' { return $input * 10**2 }
            when 'kilo' { return $input * 10**3 }
            when 'mega' { return $input * 10**6 }
            when 'giga' { return $input * 10**9 }
            when 'tera' { return $input * 10**12 }
            default { fail "you must input a metric prefix which allow pico to tera" }
        }
    }
}



class Mass does MetricPrefix {
    #basic Mass is g is different form si statda
    has $.g;

    submethod BUILD ( :$!g  ) {
    }

}

class Length does MetricPrefix {
    has $.Length ;

    submethod BUILD ( :$!Length  ) {
    }
}



multi postfix:<(kg)>( $input ) {
    return Mass.new( g => Mass.baseOn("kilo",$input) ) or fail "you Must input a number";
}

multi postfix:<(g)>( $input ) {
    return Mass.new( g => $input ) or fail "you Must input a number";
}

multi infix:<+>( Mass $inputOne , Mass $inputTwo ) is assoc<right> {
    return Mass.new( g => $inputOne.g + $inputTwo.g) or fail "error in there ";
}

multi infix:<->( Mass $inputOne , Mass $inputTwo ) is assoc<right> {
    return Mass.new( g => $inputOne.g - $inputTwo.g) or fail "error in there ";
}

multi infix:<*>( Mass $inputOne , Mass $inputTwo ) is assoc<right> is tighter( &infix:<+> ) is tighter( &infix:<-> ) is tighter( &infix:</>) {
    return Mass.new( g => $inputOne.g * $inputTwo.g) or fail "error in there ";
}

multi infix:</>( Mass $inputOne , Mass $inputTwo ) is assoc<right> is tighter( &infix:<+> ) is tighter( &infix:<-> )  {
    return Mass.new( g => $inputOne.g / $inputTwo.g) or fail "error in there ";
}





#the meterLeng
multi postfix:<(km)>( $input ) {
    return Length.new( Length => Length.baseOn("kilo",$input) ) or fail "you Must input a number";
}

multi postfix:<(m)>( $input ) {
    return Length.new( Length => $input ) or fail "you Must input a number";
}

multi infix:<+>( Length $inputOne , Length $inputTwo ) is assoc<right> {
    return Length.new( Length => $inputOne.Length + $inputTwo.Length) or fail "error in there ";
}

multi infix:<->( Length $inputOne , Length $inputTwo ) is assoc<right> {
    return Length.new( Length => $inputOne.Length - $inputTwo.Length) or fail "error in there ";
}

multi infix:<*>( Length $inputOne , Length $inputTwo ) is assoc<right> is tighter( &infix:<+> ) is tighter( &infix:<-> ) is tighter( &infix:</>) {
    return Length.new( Length => $inputOne.Length * $inputTwo.Length) or fail "error in there ";
}

multi infix:</>( Length $inputOne , Length $inputTwo ) is assoc<right> is tighter( &infix:<+> ) is tighter( &infix:<-> )  {
    return Length.new( Length => $inputOne.Length / $inputTwo.Length) or fail "error in there ";
}


#just a test
say 10(kg) + 1(g);
say 10(m) + 1(m);
#/usr/bin/env perl6
使用v6;
#基本SI(国际单位制)类型
角色度量前缀{
方法baseOn(Str$base,Numeric$input){
给定$base{
当'pico'{返回$input*10**-12}
当'namo'{返回$input*10**-9}
当'micro'{返回$input*10**-6}
当'milli'{返回$input*10**-3}
当'centi'{返回$input*10**-2}
当'hecto'{返回$input*10**2}
当'kilo'{返回$input*10**3}
当'mega'{返回$input*10**6}
当'giga'{返回$input*10**9}
当'tera'{返回$input*10**12}
默认{fail“您必须输入一个允许pico到tera的度量前缀”}
}
}
}
类质量不包含度量前缀{
#基本质量是g,与si statda不同
有$.g;
子方法生成(:$!g){
}
}
类长度不包含MetricPrefix{
长度为$.Length;
子方法生成(:$!长度){
}
}
多后缀:($input){
返回Mass.new(g=>Mass.baseOn(“kilo”,$input))或失败“您必须输入一个数字”;
}
多后缀:($input){
返回Mass.new(g=>$input)或失败“您必须输入一个数字”;
}
多中缀:(Mass$inputOne,Mass$inputwo)是关联的{
返回Mass.new(g=>$inputOne.g+$inputwo.g)或失败“错误在那里”;
}
多中缀:(Mass$inputOne,Mass$inputwo)是关联的{
返回Mass.new(g=>$inputOne.g-$inputwo.g)或失败“错误在那里”;
}
多中缀:(Mass$inputOne,Mass$inputwo)关联更紧密(&infix:)关联更紧密(&infix:)关联更紧密(&infix:){
返回Mass.new(g=>$inputOne.g*$inputwo.g)或失败“错误在那里”;
}
多中缀:(Mass$inputOne,Mass$inputwo)关联更紧密(&infix:)关联更紧密(&infix:){
返回Mass.new(g=>$inputOne.g/$inputwo.g)或失败“错误在那里”;
}
#梅特林酒店
多后缀:($input){
返回Length.new(Length=>Length.baseOn(“kilo”,$input))或失败“您必须输入一个数字”;
}
多后缀:($input){
返回Length.new(Length=>$input)或失败“您必须输入一个数字”;
}
多中缀:(长度$inputOne,长度$inputwo)是关联的{
返回Length.new(Length=>$inputOne.Length+$inputwo.Length)或失败“此处出错”;
}
多中缀:(长度$inputOne,长度$inputwo)是关联的{
返回Length.new(Length=>$inputOne.Length-$inputwo.Length)或失败“此处出错”;
}
多中缀:(长度$inputOne,长度$inputwo)关联更紧密(&infix:)关联更紧密(&infix:)关联更紧密(&infix:){
返回Length.new(Length=>$inputOne.Length*$inputwo.Length)或失败“此处出错”;
}
多中缀:(长度$inputOne,长度$inputwo)关联更紧密(&infix:)关联更紧密(&infix:){
返回Length.new(Length=>$inputOne.Length/$inputwo.Length)或失败“此处出错”;
}
#只是个测试
表示10(kg)+1(g);
说10米+1米;

我将失败消息替换为输入上的声明类型。这会让Perl6担心是否输入了数字,如果没有输入,则会给出相应的错误消息。我还假设所有长度和质量都是正的

#!/usr/bin/env perl6
use v6;
#basic SI(International System of Units) type

role MetricPrefix {
    method baseOn ( Str $base , Numeric $input ) {
        given $base {
            when 'pico'  { return $input * 10**-12 }
            when 'namo'  { return $input * 10**-9  }
            when 'micro' { return $input * 10**-6  }
            when 'milli' { return $input * 10**-3  }
            when 'centi' { return $input * 10**-2  }
            when 'hecto' { return $input * 10**2   }
            when 'kilo'  { return $input * 10**3   }
            when 'mega'  { return $input * 10**6   }
            when 'giga'  { return $input * 10**9   }
            when 'tera'  { return $input * 10**12  }
            default { fail "you must input a metric prefix within the range of pico to tera" }
        }
    }
}

class Mass does MetricPrefix {
    #basic Mass is g is different form si statda
    has $.g where * > 0;
}

class Length does MetricPrefix {
    has $.Length where * > 0;
}

# Mass
multi postfix:<(kg)>( $input where * > 0) {
    return Mass.new( g => Mass.baseOn("kilo",$input) );
}

multi postfix:<(g)>( $input where * > 0) {
    return Mass.new( g => $input );
}

multi infix:<+>( Mass $inputOne , Mass $inputTwo ) is assoc<right> {
    return Mass.new( g => $inputOne.g + $inputTwo.g);
}

multi infix:<->( Mass $inputOne , Mass $inputTwo ) is assoc<right> {
    return Mass.new( g => $inputOne.g - $inputTwo.g);
}

multi infix:<*>( Mass $inputOne , Mass $inputTwo ) is assoc<right> is tighter( &infix:<+> ) is tighter( &infix:<-> ) is tighter( &infix:</>) {
    return Mass.new( g => $inputOne.g * $inputTwo.g);
}

multi infix:</>( Mass $inputOne , Mass $inputTwo ) is assoc<right> is tighter( &infix:<+> ) is tighter( &infix:<-> )  {
    return Mass.new( g => $inputOne.g / $inputTwo.g);
}

#Length
multi postfix:<(km)>( $input where * > 0) {
    return Length.new( Length => Length.baseOn("kilo",$input) );
}

multi postfix:<(m)>( $input where * > 0) {
    return Length.new( Length => $input );
}

multi infix:<+>( Length $inputOne , Length $inputTwo ) is assoc<right> {
    return Length.new( Length => $inputOne.Length + $inputTwo.Length);
}

multi infix:<->( Length $inputOne , Length $inputTwo ) is assoc<right> {
    return Length.new( Length => $inputOne.Length - $inputTwo.Length);
}

multi infix:<*>( Length $inputOne , Length $inputTwo ) is assoc<right> is tighter( &infix:<+> ) is tighter( &infix:<-> ) is tighter( &infix:</>) {
    return Length.new( Length => $inputOne.Length * $inputTwo.Length);
}

multi infix:</>( Length $inputOne , Length $inputTwo ) is assoc<right> is tighter( &infix:<+> ) is tighter( &infix:<-> )  {
    return Length.new( Length => $inputOne.Length / $inputTwo.Length);
}


#just a test
say 10(kg) + 1(g);
say 10(m) + 1(m);
#/usr/bin/env perl6
使用v6;
#基本SI(国际单位制)类型
角色度量前缀{
方法baseOn(Str$base,Numeric$input){
给定$base{
当'pico'{返回$input*10**-12}
当'namo'{返回$input*10**-9}
当'micro'{返回$input*10**-6}
当'milli'{返回$input*10**-3}
当'centi'{返回$input*10**-2}
当'hecto'{返回$input*10**2}
当'kilo'{返回$input*10**3}
当'mega'{返回$input*10**6}
当'giga'{返回$input*10**9}
当'tera'{返回$input*10**12}
默认值{fail“您必须输入一个在pico到tera范围内的度量前缀”}
}
}
}
类质量不包含度量前缀{
#基本质量是g,与si statda不同
有$.g,其中*>0;
}
类长度不包含MetricPrefix{
具有$.Length,其中*>0;
}
#弥撒
多后缀:($input,其中*>0){
返回Mass.new(g=>Mass.baseOn(“kilo”,$input));
}
多后缀:($input,其中*>0){
返回新质量(g=>$input);
}
多中缀:(Mass$inputOne,Mass$inputwo)是关联的{
返回质量.new(g=>$inputOne.g+$inputwo.g);
}
多中缀:(Mass$inputOne,Mass$inputwo)是关联的{
返回质量.new(g=>$inputOne.g-$inputwo.g);
}
多中缀:(Mass$inputOne,Mass$inputwo)关联更紧密(&infix:)关联更紧密(&infix:)关联更紧密(&infix:){
返回质量。新(g=>$inputOne.g*$inputwo.g);
}
多中缀:(Mass$inputOne,Mass$inputwo)关联更紧密(&infix:)关联更紧密(&infix:){
返回质量.new(g=>$inputOne.g/$inputwo.g);
}
#长度
多后缀:($input,其中*>0){
返回Length.new(Length=>Length.baseOn(“kilo”,$input));
}
多后缀:($input,其中*>0){
返回长度.new(长度=>$input);
}
多中缀:(长度$inputOne,长度$inputwo)是关联的{
返回长度.new(长度=>$inputOne.Length+$inputwo.Length);
}
多中缀:(长度$inputOne,长度$inputwo)是关联的{
返回长度.new(长度=>$inputOne.Length-$inputwo.Length);
}
多中缀:(长度$inputOne,长度$inputwo)关联更紧密(&infix:)关联更紧密(&infix:)关联更紧密(&infix:){
返回长度.new(长度=>$inputOne.Lengt