Thanks again. I'm trying to get proficiency in Raku by converting some Python programs mine in Raku.
On Mon, Jun 22, 2020 at 6:41 PM Tobias Boege <t...@taboege.de> wrote: > On Mon, 22 Jun 2020, Aureliano Guedes wrote: > > Thank you for the clarification. > > > > There is a method to set Rat precision at the scope of the program to > apply > > to all *.Rat() cases? > > > > I don't know, based on a quick search in the documentation, of any global > setting. The hacky solution would be to wrap the method like this: > > say pi.Rat.nude; # OUTPUT: (355 113) > > Num.^find_method('Rat').wrap: -> |c { > # Make the default argument 1e-20 > callwith |(|c, 1e-20 if c.elems ≤ 1) > }; > > say pi.Rat.nude; # OUTPUT: (245850922 78256779) > > This program has three parts. First, we print pi.Rat.nude for comparison. > The nude method returns numerator and denominator of a Rat as a list. > The last part is just looking at pi.Rat.nude again to confirm that the > change in the middle has worked and increased the default precision. > > You said you are relatively new to Raku, so let me explain the middle part. > Raku has a special syntax for calling metamethods (i.e. doing > introspection) > on an object or type, which is the .^ operator. I use it to access an > object > representation of the Rat method on the Num type. I can wrap some code of > my liking around this method via its wrap method. > > The `-> |c { … }` syntax creates an anonymous code block that wraps around > the Num.Rat method and it accepts all arguments that may be passed as a > Capture [1] in the variable c. If that Capture does not have a first > parameter, i.e. the precision is not specified, I sneak in my 1e-20. > Afterwards, the `callwith |(…)` part unpacks the (modified) captured > arguments and calls the original method that I am wrapping [2]. > > Effectively, I pass a precision of 1e-20 whenever the caller did not pass > a precision. You can see that the wrapping takes effect immediately in > the last line of the above program. > > Ideally, this could be turned into a module that hides the questionable > means behind a dynamic $*RAT-PRECISION variable or so. That is, unless > someone comes up with an even better solution. > > Best, > Tobias > > [1] https://docs.raku.org/type/Capture > [2] https://docs.raku.org/routine/wrap > > -- > "There's an old saying: Don't change anything... ever!" -- Mr. Monk > -- Aureliano Guedes skype: aureliano.guedes contato: (11) 94292-6110 whatsapp +5511942926110