develooper Front page | perl.perl6.users | Postings from November 2021

Re: how do I turn a real into and array of Integers?

Thread Previous | Thread Next
From:
William Michels via perl6-users
Date:
November 2, 2021 01:56
Subject:
Re: how do I turn a real into and array of Integers?
Message ID:
CAA99HCw534xjQoGsKSMB6R1mQiG1OFoF-6Ad1x7CDfN4NPj5pQ@mail.gmail.com
You did great for not knowing Raku!

~$ raku -e "say sqrt(2).split(/\.|''/);"
( 1  4 1 4 2 1 3 5 6 2 3 7 3 0 9 5 1 )
~$ raku -e "say sqrt(2).split(/\.|''/).raku;"
("", "1", "", "4", "1", "4", "2", "1", "3", "5", "6", "2", "3", "7", "3",
"0", "9", "5", "1", "").Seq
~$ raku -e "say sqrt(2).split(/\.|''/, :skip-empty);"
(1 4 1 4 2 1 3 5 6 2 3 7 3 0 9 5 1)
~$ raku -e "say sqrt(2).split(/\.|''/, :skip-empty).join(', ');"
1, 4, 1, 4, 2, 1, 3, 5, 6, 2, 3, 7, 3, 0, 9, 5, 1

I moved the sqrt(2) call to the head of the method chain, then visualized
elements using the `.raku` method (`.perl` works also, but don't tell
anyone). You can see an empty element at the beginning/end, as well as
where the decimal point used to reside. Including :skip-empty in your
`.split` call sets it to True, removing empty elements.

For Mac/Linux people (swapped single/double quotes):

~$ raku -e 'say sqrt(2).split(/\.|""/);'
( 1  4 1 4 2 1 3 5 6 2 3 7 3 0 9 5 1 )
~$ raku -e 'say sqrt(2).split(/\.|""/).raku;'
("", "1", "", "4", "1", "4", "2", "1", "3", "5", "6", "2", "3", "7", "3",
"0", "9", "5", "1", "").Seq
~$ raku -e 'say sqrt(2).split(/\.|""/, :skip-empty);'
(1 4 1 4 2 1 3 5 6 2 3 7 3 0 9 5 1)
~$ raku -e 'say sqrt(2).split(/\.|""/, :skip-empty).join(", ");'
1, 4, 1, 4, 2, 1, 3, 5, 6, 2, 3, 7, 3, 0, 9, 5, 1

HTH, Bill.

On Sun, Oct 31, 2021 at 5:51 AM sisyphus <sisyphus359@gmail.com> wrote:
>
>
>
> On Sun, Oct 31, 2021 at 10:10 PM ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:
>>
>> On 10/31/21 01:43, Shlomi Fish wrote:
>>
>> >
>> >> ("" ~ sqrt(2)).comb().grep(* ne ".").map(+*)
>> > (1 4 1 4 2 1 3 5 6 2 3 7 3 0 9 5 1)
>>
>> Cool!
>>
>> my Int @x = ("" ~ sqrt(2)).comb().grep(* ne ".").map(+*)
>> [1 4 1 4 2 1 3 5 6 2 3 7 3 0 9 5 1]
>>
>> Is there a way to set how many digits I get?
>
>
> In perl we can do:
> C:\>perl -e "@x = split(/\.|/, sqrt(2)); print for @x";
> 14142135623731
> C:\>perl -e "@x = split(/\.|/, sprintf('%.7g', sqrt(2))); print for @x";
> 1414214
> C:\>perl -e "@x = split(/\.|/, sprintf('%.8g', sqrt(2))); print for @x";
> 14142136
>
> But the same split() expression is unacceptable to Raku.
> Closest I could get with that approach:
>
> C:\>raku -e "say split(/\.|''/, sqrt 2);"
> ( 1  4 1 4 2 1 3 5 6 2 3 7 3 0 9 5 1 )
>
> C:\_32>raku -e "say split(/\.|''/, sprintf('%.5g', sqrt 2));"
> ( 1  4 1 4 2 )
>
> It's that additional space between the first 2 digits that has me beat.
> (I don't know raku at all.)
>
> Cheers,
> Rob
>
>

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About