Üniversitede kimya eğitimi alıyorum ve kimyasal formül veya diğer işlemleri dengeleme gibi Perl6 veya Perl'deki ders kitabı örneklerini yazmaya çalışıyorum!Perl 6 özel işleçlerini kullanma
Ardından, sorunun perl6 özel işleci ile ilgili olduğunu gördüm. Özelliği kullandığımda kodumu ve kendimi tekrarladığımı hissediyorum. Okumak ve yazmak zor. Bunu nasıl basitleştirebilirim?
#!/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);
Bu, çözmeniz gereken bir sorundan ziyade bir Kod Gözden Geçirme sorusu gibi görünüyor. Siz * olabilir * (ve onların Yardım Merkezi'ni kontrol edin ilk) kardeş sitemizde daha iyi bir uyum bulmak https://codereview.stackexchange.com – IMSoP
'submethod BUILD's gereksiz çünkü kamu olarak nitelikleri ilan etti. –
evet, bu alt metinde, herhangi bir nesne kullanmadan; –