patternMinor
Mass and length calculator using Perl 6 custom operator
Viewed 0 times
operatorperlmasslengthcustomusingandcalculator
Problem
I am studying chemistry in the university, and then I try to write all the things in the textbook with Perl6 or Perl, like balancing the chemical formula or other process!
Then I encountered the problem is on perl6 custom operator.
I feel I have been repeating my code and myself when i use the feature.
It is hard to read and write, and what is the way to deal with such problems?
```
#!/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:( $input ) {
return Mass.new( g => Mass.baseOn("kilo",$input) ) or fail "you Must input a number";
}
multi postfix:( $input ) {
return Mass.new( g => $input ) or fail "you Must input a number";
}
multi infix:( Mass $inputOne , Mass $inputTwo ) is assoc {
return Mass.new( g => $inputOne.g + $inputTwo.g) or fail "error in there ";
}
multi infix:( Mass $inputOne , Mass $inputTwo ) is assoc {
return Mass.new( g => $inputOne.g - $inputTwo.g) or fail "error in there ";
}
multi infix:( Mass $inputOne , Mass $inputTwo ) is assoc is tighter( &infix: ) is tight
Then I encountered the problem is on perl6 custom operator.
I feel I have been repeating my code and myself when i use the feature.
It is hard to read and write, and what is the way to deal with such problems?
```
#!/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:( $input ) {
return Mass.new( g => Mass.baseOn("kilo",$input) ) or fail "you Must input a number";
}
multi postfix:( $input ) {
return Mass.new( g => $input ) or fail "you Must input a number";
}
multi infix:( Mass $inputOne , Mass $inputTwo ) is assoc {
return Mass.new( g => $inputOne.g + $inputTwo.g) or fail "error in there ";
}
multi infix:( Mass $inputOne , Mass $inputTwo ) is assoc {
return Mass.new( g => $inputOne.g - $inputTwo.g) or fail "error in there ";
}
multi infix:( Mass $inputOne , Mass $inputTwo ) is assoc is tighter( &infix: ) is tight
Solution
This is an interesting use case of postfixes. Here's how I would go about it. Unfortunately, because postfixes can't take regexen, you need to specify each one individually, but it's not horrible, especially if you throw things into a module.
Let's first create a postfix operator for each value type:
Basically, we're saying we want to attach
Now let's take a look at the
The
By making it independent of the gram, we can use it later for a length / etc class. For that reason, I'd define it outside of the class so that it can be used by others easily. Believe it or not, it is possible to have an empty string as a key. Kind of useful in our case.
At this point, we can start to use our values:
Okay, not the most useful output, but at least we know everything is working. Let's make a
Now all we really have to do is handle the addition:
This works great, but does automatically convert everything back to being a
As I said, this would work great as a module. You can do that like this:
`unit module SI-Operators;
my constant \si-prefix := Map.new(
'Y', 10 24, 'y', 10 -24,
'Z', 10 21, 'z', 10 -21,
'E', 10 18, 'a', 10 -18,
'P', 10 15, 'f', 10 -15,
'T', 10 12, 'p', 10 -12,
'G', 10 9, 'n', 10 -9,
'M', 10 6, 'μ', 10 -6,
'k', 10 3, 'm', 10 -3,
'h', 10 2, 'c', 10 -2,
'da', 10 1, 'd', 10 -1,
'', 1
);
# MASSES
class Mass is export (:mass) {
has $.grams;
has $.prefix;
method new ($number, $prefix = '') {
self.bless:
:grams($number * si-prefix{$prefix}),
:$prefix
}
method Str {
$!grams / si-prefix{$!prefix}
~ $!prefix
~ 'g'
}
method gist { self.Str }
}
multi infix: (Mass \a, Mass \b) is export (:mass) { Mass.new: a.grams + b.grams }
multi infix: (Mass \a, Mass \b) is export (:mass) { Mass.new: a.grams - b.grams }
multi postfix: (Numeric() $m) is export (:mass) { Mass.new: $m, 'Y' }
multi postfix: (Numeric() $m) is export (:mass) { Mass.new: $m, 'Z' }
multi postfix: (Numeric() $m) is export (:mass) { Mass.new: $m, 'E' }
multi postfix: (Numeric() $m) is export (:mass) { Mass.new: $m, 'P' }
multi postfix: (Numeric() $m) is export (:mass) { Mass.new: $m, 'T' }
multi postfix: (Numeric() $m) is export (:mass) { Mass.new: $m, 'G' }
multi postfix: (Numeric() $m) is export (:mass) { Mass.new: $m, 'M' }
multi postfix: (Numeric() $m) is export (:mass) { Mass.new: $m, 'k' }
multi postfix: (Numeric() $m) is export (:mass) { Mass.new: $m, 'h' }
multi postfix: (Numeric() $m) is export (:mass) { Mass.new: $m, 'da' }
multi postfix: (Numeric() $m) is export (:mass) { Mass.new: $m }
multi postfix: (Numeric() $m) is export (:mass) { Mass.new: $m, 'd' }
multi postfix: (Numeric() $m) is export (:mass) { Mass.new: $m, 'c' }
multi postfix: (Numeric() $m) is export (:mass) { Mass.new: $m, 'm' }
multi postfix: (Numeric() $m) is export (:mass) { Mass.new: $m, 'μ' }
multi postfix: (Numeric() $m) is export (:mass) { Mass.new: $m, 'n' }
multi postfix: (Numeric() $m) is export (:mass) { Mass.new: $m, 'p' }
multi postfix: (Numeric() $m) is export (:mass) { Mass.new: $m, 'f' }
multi postfix: (Num
Let's first create a postfix operator for each value type:
multi postfix: (Numeric() $m) { Mass.new: $m, 'k' }
multi postfix: (Numeric() $m) { Mass.new: $m }
multi postfix: (Numeric() $m) { Mass.new: $m, 'm' }
Basically, we're saying we want to attach
g onto something. We'd like it to be a number type, so we make have it only act on Numeric types, but if someone passes us a string, the () tells it to coerce it for us. And then we make a Mass object from those the value and the prefix.Now let's take a look at the
Mass class. All we really need to have in it is a weight and probably store its prefix so if we create a kg if we want to display it we can keep it in kg and not switch to mg or something. We'll store things in grams, so on creation we use the prefix to shrink/expand the value. class Mass {
has $.grams;
has $.prefix;
method new ($value, $prefix) {
self.bless:
:grams($value * si-prefix{$prefix}),
:$prefix
}
}
The
si-prefix is just a map of values: my constant \si-prefix := Map.new( 'k', 1000, '', 1, 'm', 0.001 );
By making it independent of the gram, we can use it later for a length / etc class. For that reason, I'd define it outside of the class so that it can be used by others easily. Believe it or not, it is possible to have an empty string as a key. Kind of useful in our case.
At this point, we can start to use our values:
say 500g; # --> Weight.new(grams => 500, prefix => "")
say 2kg; # --> Weight.new(grams => 2000, prefix => "kg")
Okay, not the most useful output, but at least we know everything is working. Let's make a
Str and gist method that makes more sense:method Str {
$!grams / si-prefix{$!prefix}
~ $!prefix
~ 'g'
}
method gist { self.Str }Now all we really have to do is handle the addition:
multi infix: (Mass \a, Mass \b) { Weight.new: a.grams + b.grams }
multi infix: (Mass \a, Mass \b) { Weight.new: a.grams - b.grams }This works great, but does automatically convert everything back to being a
g unit even if both were kg, etc. At this point, it's a bit of taste. You could decide that the first or second unit has precedence, or implement some more complex logic (larger or smaller, or the one that results in no more than three decimal places or no more than three whole units, etc). I'll leave that for you to implement. As I said, this would work great as a module. You can do that like this:
`unit module SI-Operators;
my constant \si-prefix := Map.new(
'Y', 10 24, 'y', 10 -24,
'Z', 10 21, 'z', 10 -21,
'E', 10 18, 'a', 10 -18,
'P', 10 15, 'f', 10 -15,
'T', 10 12, 'p', 10 -12,
'G', 10 9, 'n', 10 -9,
'M', 10 6, 'μ', 10 -6,
'k', 10 3, 'm', 10 -3,
'h', 10 2, 'c', 10 -2,
'da', 10 1, 'd', 10 -1,
'', 1
);
# MASSES
class Mass is export (:mass) {
has $.grams;
has $.prefix;
method new ($number, $prefix = '') {
self.bless:
:grams($number * si-prefix{$prefix}),
:$prefix
}
method Str {
$!grams / si-prefix{$!prefix}
~ $!prefix
~ 'g'
}
method gist { self.Str }
}
multi infix: (Mass \a, Mass \b) is export (:mass) { Mass.new: a.grams + b.grams }
multi infix: (Mass \a, Mass \b) is export (:mass) { Mass.new: a.grams - b.grams }
multi postfix: (Numeric() $m) is export (:mass) { Mass.new: $m, 'Y' }
multi postfix: (Numeric() $m) is export (:mass) { Mass.new: $m, 'Z' }
multi postfix: (Numeric() $m) is export (:mass) { Mass.new: $m, 'E' }
multi postfix: (Numeric() $m) is export (:mass) { Mass.new: $m, 'P' }
multi postfix: (Numeric() $m) is export (:mass) { Mass.new: $m, 'T' }
multi postfix: (Numeric() $m) is export (:mass) { Mass.new: $m, 'G' }
multi postfix: (Numeric() $m) is export (:mass) { Mass.new: $m, 'M' }
multi postfix: (Numeric() $m) is export (:mass) { Mass.new: $m, 'k' }
multi postfix: (Numeric() $m) is export (:mass) { Mass.new: $m, 'h' }
multi postfix: (Numeric() $m) is export (:mass) { Mass.new: $m, 'da' }
multi postfix: (Numeric() $m) is export (:mass) { Mass.new: $m }
multi postfix: (Numeric() $m) is export (:mass) { Mass.new: $m, 'd' }
multi postfix: (Numeric() $m) is export (:mass) { Mass.new: $m, 'c' }
multi postfix: (Numeric() $m) is export (:mass) { Mass.new: $m, 'm' }
multi postfix: (Numeric() $m) is export (:mass) { Mass.new: $m, 'μ' }
multi postfix: (Numeric() $m) is export (:mass) { Mass.new: $m, 'n' }
multi postfix: (Numeric() $m) is export (:mass) { Mass.new: $m, 'p' }
multi postfix: (Numeric() $m) is export (:mass) { Mass.new: $m, 'f' }
multi postfix: (Num
Code Snippets
method Str {
$!grams / si-prefix{$!prefix}
~ $!prefix
~ 'g'
}
method gist { self.Str }multi infix:<+> (Mass \a, Mass \b) { Weight.new: a.grams + b.grams }
multi infix:<-> (Mass \a, Mass \b) { Weight.new: a.grams - b.grams }sub stuff-with-lengths {
use SI-Operators :length;
say 25mm + 78km # 78000.025m
}Context
StackExchange Code Review Q#159150, answer score: 4
Revisions (0)
No revisions yet.