Remix.run Logo
librasteve 5 days ago

This is a great article and I like that you are highlighting the benefits of Dimensions as Types.

While Raku is less popular than Python, it does have deep roots in Haskell and strong types (the first Raku / Perl6 parser - PUGS - was written in Haskell and all the early devs were encouraged to learn Haskell first).

Similar concepts are used in these Raku modules... which provide dimensional analysis and marry types to dimensions.

- https://raku.land/zef:librasteve/Physics::Measure

- https://raku.land/zef:librasteve/Physics::Unit

I had some fun with making this example of using these Raku modules with Jupyter https://rakujourney.wordpress.com/wp-content/uploads/2023/08...

[disclaimer: I am the author of these modules]

Raku is also good at Slangs (Sublanguages) and unicode, so these tools can be used in a very intuitive way:

  #!/usr/bin/env raku
  use Physics::Constants;
  use Physics::Measure :ALL;

  say ~ℏ;     #1.054571817e-34 J.s

  my \λ = 2.5nm;
  say "Wavelength of photon (λ) is " ~λ;

  my \ν = c / λ;
  say "Frequency of photon (ν) is " ~ν.in('petahertz');

  my \Ep = ℎ * ν;
  say "Energy of photon (Ep) is " ~Ep.in('attojoules');

  Wavelength of photon (λ) is 2.5nm
  Frequency of photon (ν) is 119.92PHz
  Energy of photon (Ep) is 79.46aJ