News for hackers. Stuff that matters.
Re:Raku vs. Perl by wodenokoto on Saturday April 18, 2020 @09:25AM (#22907543)
Can someone give a little more background why a unit conversion library needs a parser?

Like an ELI5 for someone not into neither Perl or Raku and haven’t met the units library before.

Re:Raku vs. Perl by b2gills on Monday April 27, 2020 @03:48PM (#22999259)
You really wouldn't really need to in Raku.

Mainly because you can add things to the existing parser instead. (See postfix and infix below.)

    role Physics::Datum {
      has Real $.value is required;
    }
    role Physics::Distance {
      also does Physics::Datum;
    }
    role Physics::Volume {
      also does Physics::Datum;
    }

    class Physics::Volume::Gallon {
      also does Physics::Volume;
    }

    class Physics::Distance::Mile {
      also does Physics::Distance;
    }

    role Physics::Per[ ::N-Type, ::D-Type ] {
      has N-Type $.nu is required;
      has D-Type $.de is required;

      # could have a submethod TWEAK to reduce the fraction
    }

    constant gallon = Physics::Volume::Gallon.new( value => 1 );

    sub postfix:<miles> ( Real $value ){
      Physics::Distance::Meter.new( :$value )
    }

    sub infix:<per> ( ::NT Physics::Datum $nu, ::DT Physics::Datum $de ){
      Physics::Per[NT,DT].new( :$nu, :$de )
    }


    my $mpg = 5miles per gallon;
Which would result in the following data structure.

    Physics::Per[ Physics::Distance::Mile, Physics::Volume::Gallon ].new(
      nu => Physics::Distance::Mile.new( value => 5 ),
      de => Physics::Volume::Gallon.new( value => 1 ),
    )
I think the main reason it was written that way was it was copied from Perl. A lot of the above would be more difficult or slower in Perl.

(This was mostly a quick throwaway example of how it could be done.)

Re:Raku vs. Perl by riffraff on Saturday April 18, 2020 @09:36AM (#22907606)
you would define values like

   weight = unit("10 kg")
   speed = unit("2.5 furlong per week")
Re:Raku vs. Perl by wodenokoto on Saturday April 18, 2020 @10:02AM (#22907779)
My imaginary units library would have been like

    Unit(float, unit_as_string)
The free text version is pretty cool though
Re:Raku vs. Perl by choward on Saturday April 18, 2020 @11:00AM (#22908189)
I would parse the free text version into something like your version. It could be used for user input from a text input field or the command line.
Re:Raku vs. Perl by iso-8859-1 on Monday April 20, 2020 @11:13PM (#22931468)
But what does strings have to do with it? Why can't those expressions be plain Perl, maybe with some overloaded operators?

For example,

    import week, custom, si, type from stdlib.units
    weight = 10 * si.kg
    speed = 2.5 * (custom("furlong", type.LENGTH) / week)
Re:Raku vs. Perl by p6steve on Tuesday April 21, 2020 @10:16AM (#22935138)
this is also important - but imo, and in practice from perl5, the Units library experience is a lot richer if you can do both - especially for entry level programmers such as high school students - more on this to come in v2
Re:Raku vs. Perl by Symbiote on Saturday April 18, 2020 @02:00PM (#22909642)
Even further, presumably things like kg·m·s⁻², which (perhaps) this library can equate to Newtons.
Re:Raku vs. Perl by p6steve on Tuesday April 21, 2020 @10:18AM (#22935151)
this works now x for input and output ... the challenge for this kind of thing in bare code is that you need a LOT of namespace ... .oO maybe override the dot?