Atomic Units?

One of the most exciting parts of blogging about and hacking on perl6* is that there’s a community out there and there’s (always) more than one way to do it!

For Physics::Measure I have been working on a design for a ‘nano-slang’ that can provide a shortcut for the usual new Class declaration… quite long winded for my target audience of physics undergraduates and high school students.

#Instances the usual way

my Unit $u .=new(name => ‘m’, unitsof => ‘Distance’); #Unit m

my Distance $a .=new(value => 10, units => $u);           #Distance 10 m

So, duly inspired by Rakudo perl6 going atomic ⚛ applying unicode to some threading constructs, I started with the notion of the ‘libra’ operator ♎️ as shorthand to declare and load Measure instances.

#Introducing the libra operator ♎️ as shorthand to declare and load

my $b ♎️ ’50 m’;   #Distance 50 m

$b ♎️ ‘3 yards’;     #Distance 3 yards

As you can see, the gap created between ♎️ and ; is a slang zone that can consume strings and numeric literals. Here’s something a bit more elaborate:

#Normalization with the .norm method

my $p ♎️ ’27 kg m^2 / s^3′;   #Power 27 kg m^2 / s^3

$p .= norm;                              #Power 27 W

A few design ideas drew me in this direction:

  • Physics::Measure overloads the main arithmetic operators (+,-,*,/,**,sqrt) and I felt that the use of ♎️ unicode would be a good convention to warn coders that there is something substantive happening to the core raku language.
  • It is verboten in Rakudo to overload assignment ‘=’ … quite right too! But I wanted something like a combined variable declaration and (default) assignment for the congenitally lazy coder (and the libra kinda looks like an equals sign).
  • Resistance is one of the Units implemented by Physics::Measure – and the Ohm symbol (Ω) looks a lot like ♎️.
  • There are a couple hundred definition lines in Physics::Measure covering metric SI units, cgi, feet and inches, US and imperial, plus some fun ones, so it wasn’t practical to inject infix/postfix equivalents 1:1 without overwhelming the raku module namespace.
  • I was born in October 😉

# Resistance

[‘Ω’, ‘Ohm:s’,],       ’kg m^2 / A^2 s^3′,

[‘kilohm:s’,],          ’kilo Ohm’,

[‘megohm:s’,],       ’mega Ohm’,

HOWEVER!!

Others have proposed a much more direct approach to generate and combine Measure objects – by the use of a simple postfix syntax – thank you!

Something like:

say 500g; # –> Weight.new(grams => 500, prefix => “”)

say 2kg;  # –> Weight.new(grams => 2000, prefix => “kg”)

Watch this space! Or even better zef Physics::Measure and give it a try…

~p6steve

* soon to be Rakudo?!

Advertisements

One thought on “Atomic Units?”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s