perl6 - Using Perl 6 custom operators -


i studying chemistry in university , want try writing textbook examples in perl6 or perl, balancing chemical formula or other processes!

then encountered problem on perl6 custom operator. feel have been repeating code , myself when use feature. hard read , write. how can simplify this?

#!/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 metric prefix allow pico tera" }         }     } }    class mass metricprefix {     #basic mass g different form si statda     has $.g;      submethod build ( :$!g  ) {     }  }  class length metricprefix {     has $.length ;      submethod build ( :$!length  ) {     } }    multi postfix:<(kg)>( $input ) {     return mass.new( g => mass.baseon("kilo",$input) ) or fail "you must input number"; }  multi postfix:<(g)>( $input ) {     return mass.new( g => $input ) or fail "you must input number"; }  multi infix:<+>( mass $inputone , mass $inputtwo ) assoc<right> {     return mass.new( g => $inputone.g + $inputtwo.g) or fail "error in there "; }  multi infix:<->( mass $inputone , mass $inputtwo ) assoc<right> {     return mass.new( g => $inputone.g - $inputtwo.g) or fail "error in there "; }  multi infix:<*>( mass $inputone , mass $inputtwo ) assoc<right> tighter( &infix:<+> ) tighter( &infix:<-> ) tighter( &infix:</>) {     return mass.new( g => $inputone.g * $inputtwo.g) or fail "error in there "; }  multi infix:</>( mass $inputone , mass $inputtwo ) assoc<right> tighter( &infix:<+> ) 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 number"; }  multi postfix:<(m)>( $input ) {     return length.new( length => $input ) or fail "you must input number"; }  multi infix:<+>( length $inputone , length $inputtwo ) assoc<right> {     return length.new( length => $inputone.length + $inputtwo.length) or fail "error in there "; }  multi infix:<->( length $inputone , length $inputtwo ) assoc<right> {     return length.new( length => $inputone.length - $inputtwo.length) or fail "error in there "; }  multi infix:<*>( length $inputone , length $inputtwo ) assoc<right> tighter( &infix:<+> ) tighter( &infix:<-> ) tighter( &infix:</>) {     return length.new( length => $inputone.length * $inputtwo.length) or fail "error in there "; }  multi infix:</>( length $inputone , length $inputtwo ) assoc<right> tighter( &infix:<+> ) tighter( &infix:<-> )  {     return length.new( length => $inputone.length / $inputtwo.length) or fail "error in there "; }   #just test 10(kg) + 1(g); 10(m) + 1(m); 

i replaced fail messages declaring types on input. let perl 6 worry whether numbers input , give appropriate error messages if not. built in assumption lengths , masses positive.

#!/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 metric prefix within range of pico tera" }         }     } }  class mass metricprefix {     #basic mass g different form si statda     has $.g * > 0; }  class length metricprefix {     has $.length * > 0; }  # mass multi postfix:<(kg)>( $input * > 0) {     return mass.new( g => mass.baseon("kilo",$input) ); }  multi postfix:<(g)>( $input * > 0) {     return mass.new( g => $input ); }  multi infix:<+>( mass $inputone , mass $inputtwo ) assoc<right> {     return mass.new( g => $inputone.g + $inputtwo.g); }  multi infix:<->( mass $inputone , mass $inputtwo ) assoc<right> {     return mass.new( g => $inputone.g - $inputtwo.g); }  multi infix:<*>( mass $inputone , mass $inputtwo ) assoc<right> tighter( &infix:<+> ) tighter( &infix:<-> ) tighter( &infix:</>) {     return mass.new( g => $inputone.g * $inputtwo.g); }  multi infix:</>( mass $inputone , mass $inputtwo ) assoc<right> tighter( &infix:<+> ) tighter( &infix:<-> )  {     return mass.new( g => $inputone.g / $inputtwo.g); }  #length multi postfix:<(km)>( $input * > 0) {     return length.new( length => length.baseon("kilo",$input) ); }  multi postfix:<(m)>( $input * > 0) {     return length.new( length => $input ); }  multi infix:<+>( length $inputone , length $inputtwo ) assoc<right> {     return length.new( length => $inputone.length + $inputtwo.length); }  multi infix:<->( length $inputone , length $inputtwo ) assoc<right> {     return length.new( length => $inputone.length - $inputtwo.length); }  multi infix:<*>( length $inputone , length $inputtwo ) assoc<right> tighter( &infix:<+> ) tighter( &infix:<-> ) tighter( &infix:</>) {     return length.new( length => $inputone.length * $inputtwo.length); }  multi infix:</>( length $inputone , length $inputtwo ) assoc<right> tighter( &infix:<+> ) tighter( &infix:<-> )  {     return length.new( length => $inputone.length / $inputtwo.length); }   #just test 10(kg) + 1(g); 10(m) + 1(m); 

Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -