Re: Dual Buf and Str question

2022-06-10 Thread Elizabeth Mattijsen
Perhaps https://raku.land/zef:raku-community-modules/Pythonic::Str is what 
you're after?

> On 10 Jun 2022, at 07:54, ToddAndMargo via perl6-users  
> wrote:
> 
> Hi All,
> 
> I can easily  get away with this in Modula2, but
> how can I do this with Raku?
> 
> I wish to create a single variable that can be
> manipulated in two ways:
> 
> 1) as a fixed length string (Str)
> 
> 2) as a fixed length buffer (Buf)
> 
> I can think of ways to do this, but it would
> require separate variable and conversions routines
> back and forth.
> 
> Any words of Raku wisdom?
> 
> Many thanks,
> -T
> 
> -- 
> ~~
> Computers are like air conditioners.
> They malfunction when you open windows
> ~~



Re: Dual Buf and Str question

2022-06-10 Thread ToddAndMargo via perl6-users

On 10 Jun 2022, at 07:54, ToddAndMargo via perl6-users  
wrote:

Hi All,

I can easily  get away with this in Modula2, but
how can I do this with Raku?

I wish to create a single variable that can be
manipulated in two ways:

1) as a fixed length string (Str)

2) as a fixed length buffer (Buf)

I can think of ways to do this, but it would
require separate variable and conversions routines
back and forth.

Any words of Raku wisdom?

Many thanks,
-T


On 6/10/22 01:23, Elizabeth Mattijsen wrote:

Perhaps https://raku.land/zef:raku-community-modules/Pythonic::Str is what 
you're after?


No really.

Maybe if I was to tell you what I am trying to do.

I am trying to do a bitwise XOR on each byte
against another Buf.  Then I want it to act
like a string again.

I want the variable to act as both an array
of characters and a binary array of bytes.




Re: Dual Buf and Str question

2022-06-10 Thread Elizabeth Mattijsen
> On 10 Jun 2022, at 11:20, ToddAndMargo via perl6-users  
> wrote:
> 
>>> On 10 Jun 2022, at 07:54, ToddAndMargo via perl6-users 
>>>  wrote:
>>> 
>>> Hi All,
>>> 
>>> I can easily  get away with this in Modula2, but
>>> how can I do this with Raku?
>>> 
>>> I wish to create a single variable that can be
>>> manipulated in two ways:
>>> 
>>> 1) as a fixed length string (Str)
>>> 
>>> 2) as a fixed length buffer (Buf)
>>> 
>>> I can think of ways to do this, but it would
>>> require separate variable and conversions routines
>>> back and forth.
>>> 
>>> Any words of Raku wisdom?
>>> 
>>> Many thanks,
>>> -T
> 
> On 6/10/22 01:23, Elizabeth Mattijsen wrote:
>> Perhaps https://raku.land/zef:raku-community-modules/Pythonic::Str is what 
>> you're after?
> 
> No really.
> 
> Maybe if I was to tell you what I am trying to do.
> 
> I am trying to do a bitwise XOR on each byte
> against another Buf.  Then I want it to act
> like a string again.
> 
> I want the variable to act as both an array
> of characters and a binary array of bytes.

Convert a string to a Buf:  say "abc".encode.Buf;  # Buf:0x<61 62 63>
Convert a Buf to a Str: say Buf.new(97,98,99).decode;  # abc

Technically, I think the .encode is enough for what you want:

say "abc".encode.does(Blob);  # True

Re: Dual Buf and Str question

2022-06-10 Thread ToddAndMargo via perl6-users

On 6/10/22 02:36, Elizabeth Mattijsen wrote:

On 10 Jun 2022, at 11:20, ToddAndMargo via perl6-users  
wrote:


On 10 Jun 2022, at 07:54, ToddAndMargo via perl6-users  
wrote:

Hi All,

I can easily  get away with this in Modula2, but
how can I do this with Raku?

I wish to create a single variable that can be
manipulated in two ways:

1) as a fixed length string (Str)

2) as a fixed length buffer (Buf)

I can think of ways to do this, but it would
require separate variable and conversions routines
back and forth.

Any words of Raku wisdom?

Many thanks,
-T


On 6/10/22 01:23, Elizabeth Mattijsen wrote:

Perhaps https://raku.land/zef:raku-community-modules/Pythonic::Str is what 
you're after?


No really.

Maybe if I was to tell you what I am trying to do.

I am trying to do a bitwise XOR on each byte
against another Buf.  Then I want it to act
like a string again.

I want the variable to act as both an array
of characters and a binary array of bytes.


Convert a string to a Buf:  say "abc".encode.Buf;  # Buf:0x<61 62 63>
Convert a Buf to a Str: say Buf.new(97,98,99).decode;  # abc

Technically, I think the .encode is enough for what you want:

say "abc".encode.does(Blob);  # True



I like it.  Thank you!



tons of digits

2022-06-10 Thread ToddAndMargo via perl6-users

Hi All,

I forgot how to write out a ton of digits
for the square root of 2 (or any other
irrational number).

-T

--
~~
Computers are like air conditioners.
They malfunction when you open windows
~~


Easier way to load a buffer?

2022-06-10 Thread ToddAndMargo via perl6-users

Hi All,

I am looking for an easier way to load a buffer.

I know about this way

[4] > my Buf $b=Buf.new(0x2A, 0x54, 0xFF, 0x53);
Buf:0x<2A 54 FF 53>

I would like to do it on one big blast:

my Buf $b=Buf.new(0x2A54FF53A5F1D36F1CEA7E61FC37A20D54A77FE7B78);
Cannot unbox 170 bit wide bigint into native integer

But do not know the proper syntax.

Any words of wisdom?  Am I stuck with the hard way?

Many thanks,
-T


Re: Easier way to load a buffer?

2022-06-10 Thread Simon Proctor
So Buf is expecting a list of integers. If' you've got one long one in a
string like "2A54FF53A5F1D36F1CEA7E61FC37A20D54A77FE7B78" you want to split
it into smaller values so something like this?

my $hex = "2A54FF53A5F1D36F1CEA7E61FC37A20D54A77FE7B78";
my Buf $b=Buf.new(comb(2).map( *.parse-base(16) ));

Which does the trick I think.

On Fri, 10 Jun 2022 at 15:49, ToddAndMargo via perl6-users <
perl6-us...@perl.org> wrote:

> Hi All,
>
> I am looking for an easier way to load a buffer.
>
> I know about this way
>
> [4] > my Buf $b=Buf.new(0x2A, 0x54, 0xFF, 0x53);
> Buf:0x<2A 54 FF 53>
>
> I would like to do it on one big blast:
>
> my Buf $b=Buf.new(0x2A54FF53A5F1D36F1CEA7E61FC37A20D54A77FE7B78);
> Cannot unbox 170 bit wide bigint into native integer
>
> But do not know the proper syntax.
>
> Any words of wisdom?  Am I stuck with the hard way?
>
> Many thanks,
> -T
>


-- 
Simon Proctor
Cognoscite aliquid novum cotidie

http://www.khanate.co.uk/


Re: Easier way to load a buffer?

2022-06-10 Thread ToddAndMargo via perl6-users
On Fri, 10 Jun 2022 at 15:49, ToddAndMargo via perl6-users 
mailto:perl6-us...@perl.org>> wrote:


Hi All,

I am looking for an easier way to load a buffer.

I know about this way

[4] > my Buf $b=Buf.new(0x2A, 0x54, 0xFF, 0x53);
Buf:0x<2A 54 FF 53>

I would like to do it on one big blast:

my Buf $b=Buf.new(0x2A54FF53A5F1D36F1CEA7E61FC37A20D54A77FE7B78);
Cannot unbox 170 bit wide bigint into native integer

But do not know the proper syntax.

Any words of wisdom?  Am I stuck with the hard way?

Many thanks,
-T



On 6/10/22 08:36, Simon Proctor wrote:
So Buf is expecting a list of integers. If' you've got one long one in a 
string like "2A54FF53A5F1D36F1CEA7E61FC37A20D54A77FE7B78" you want to 
split it into smaller values so something like this?


my $hex = "2A54FF53A5F1D36F1CEA7E61FC37A20D54A77FE7B78";
my Buf $b=Buf.new(comb(2).map( *.parse-base(16) ));

Which does the trick I think.


What does the * i *.parse-base do?


Re: Easier way to load a buffer?

2022-06-10 Thread Elizabeth Mattijsen



> On 10 Jun 2022, at 22:47, ToddAndMargo via perl6-users  
> wrote:
> 
>>> On Fri, 10 Jun 2022 at 15:49, ToddAndMargo via perl6-users 
>>> mailto:perl6-us...@perl.org>> wrote:
>>>Hi All,
>>>I am looking for an easier way to load a buffer.
>>>I know about this way
>>>[4] > my Buf $b=Buf.new(0x2A, 0x54, 0xFF, 0x53);
>>>Buf:0x<2A 54 FF 53>
>>>I would like to do it on one big blast:
>>>my Buf $b=Buf.new(0x2A54FF53A5F1D36F1CEA7E61FC37A20D54A77FE7B78);
>>>Cannot unbox 170 bit wide bigint into native integer
>>>But do not know the proper syntax.
>>>Any words of wisdom?  Am I stuck with the hard way?
>>>Many thanks,
>>>-T
> 
> On 6/10/22 08:36, Simon Proctor wrote:
>> So Buf is expecting a list of integers. If' you've got one long one in a 
>> string like "2A54FF53A5F1D36F1CEA7E61FC37A20D54A77FE7B78" you want to split 
>> it into smaller values so something like this?
>> my $hex = "2A54FF53A5F1D36F1CEA7E61FC37A20D54A77FE7B78";
>> my Buf $b=Buf.new(comb(2).map( *.parse-base(16) ));
>> Which does the trick I think.
> 
> What does the * i *.parse-base do?

*.parse-base(16) is an example of a WhateverCode object.

It is basically syntactic sugar for easy creation of Callable blocks.  In this 
case,
it created  -> $_ { .parse-base(16) }

The * is interpreted in the Raku Grammar as a sign to create a WhateverCode 
object: it basically
represents the $_ in the signature of the created Block, and of course the $_ 
inside that block.:

Re: Easier way to load a buffer?

2022-06-10 Thread ToddAndMargo via perl6-users
On Fri, 10 Jun 2022 at 15:49, ToddAndMargo via perl6-users 
mailto:perl6-us...@perl.org>> wrote:


Hi All,

I am looking for an easier way to load a buffer.

I know about this way

[4] > my Buf $b=Buf.new(0x2A, 0x54, 0xFF, 0x53);
Buf:0x<2A 54 FF 53>

I would like to do it on one big blast:

my Buf $b=Buf.new(0x2A54FF53A5F1D36F1CEA7E61FC37A20D54A77FE7B78);
Cannot unbox 170 bit wide bigint into native integer

But do not know the proper syntax.

Any words of wisdom?  Am I stuck with the hard way?

Many thanks,
-T


On 6/10/22 08:36, Simon Proctor wrote:
So Buf is expecting a list of integers. If' you've got one long one in a 
string like "2A54FF53A5F1D36F1CEA7E61FC37A20D54A77FE7B78" you want to 
split it into smaller values so something like this?


my $hex = "2A54FF53A5F1D36F1CEA7E61FC37A20D54A77FE7B78";
my Buf $b=Buf.new(comb(2).map( *.parse-base(16) ));

Which does the trick I think.




[0] > my $hex = "2A54FF53A5F1D36F1CEA7E61FC37A20D54A77FE7B78";
2A54FF53A5F1D36F1CEA7E61FC37A20D54A77FE7B78
[1] > my Buf $b=Buf.new(comb(2).map( *.parse-base(16) ));
===SORRY!=== Error while compiling:
Calling comb(Int) will never work with signature of the proto ($, $, $?, *%)
--> my Buf $b=Buf.new(⏏comb(2).map( *.parse-base(16) ));
[1] >






--
~~
Computers are like air conditioners.
They malfunction when you open windows
~~


Re: Easier way to load a buffer?

2022-06-10 Thread Elizabeth Mattijsen



> On 10 Jun 2022, at 23:28, ToddAndMargo via perl6-users  
> wrote:
> 
>>> On Fri, 10 Jun 2022 at 15:49, ToddAndMargo via perl6-users 
>>> mailto:perl6-us...@perl.org>> wrote:
>>>Hi All,
>>>I am looking for an easier way to load a buffer.
>>>I know about this way
>>>[4] > my Buf $b=Buf.new(0x2A, 0x54, 0xFF, 0x53);
>>>Buf:0x<2A 54 FF 53>
>>>I would like to do it on one big blast:
>>>my Buf $b=Buf.new(0x2A54FF53A5F1D36F1CEA7E61FC37A20D54A77FE7B78);
>>>Cannot unbox 170 bit wide bigint into native integer
>>>But do not know the proper syntax.
>>>Any words of wisdom?  Am I stuck with the hard way?
>>>Many thanks,
>>>-T
> 
> On 6/10/22 08:36, Simon Proctor wrote:
>> So Buf is expecting a list of integers. If' you've got one long one in a 
>> string like "2A54FF53A5F1D36F1CEA7E61FC37A20D54A77FE7B78" you want to split 
>> it into smaller values so something like this?
>> my $hex = "2A54FF53A5F1D36F1CEA7E61FC37A20D54A77FE7B78";
>> my Buf $b=Buf.new(comb(2).map( *.parse-base(16) ));
>> Which does the trick I think.
> 
> 
> [0] > my $hex = "2A54FF53A5F1D36F1CEA7E61FC37A20D54A77FE7B78";
> 2A54FF53A5F1D36F1CEA7E61FC37A20D54A77FE7B78
> [1] > my Buf $b=Buf.new(comb(2).map( *.parse-base(16) ));
> ===SORRY!=== Error while compiling:
> Calling comb(Int) will never work with signature of the proto ($, $, $?, *%)
> --> my Buf $b=Buf.new(⏏comb(2).map( *.parse-base(16) ));
> [1] >

You could think: what does "comb(2)" do?  Perhaps look at the documentation?  
And then maybe realize that Simon forgot that the comb(2) should be a method on 
$hex?  Otherwise, how would it know what to parse?

I know it is hard to think when you're in a hurry.  But you should really learn 
to be able to read code, instead of copying examples only and not understanding 
*how* they work.

Re: tons of digits

2022-06-10 Thread Bruce Gray



> On Jun 10, 2022, at 8:01 AM, ToddAndMargo via perl6-users 
>  wrote:
> 
> Hi All,
> I forgot how to write out a ton of digits for the square root of 2 (or any 
> other irrational number).

Hi Todd,

I suspect that this message is what you have misplaced:
https://www.nntp.perl.org/group/perl.perl6.users/2021/10/msg10221.html
Subject: Re: how do I turn a real into and array of Integers?

In that message, I pointed to an integer_root RosettaCode implementation that, 
when used `2` scaled-up by 100²⁰⁰⁰, can easily produce 2000 digits.

Alternately, you can use Math::MPFR from Perl 5 to interface to the GNU MPFR 
Library, which has many custom algorithms for crazy-precision irrationals 
built-in.
raku -e '
sub precise_square_root ( Numeric $N, UInt $decimal_precision --> Str ) {
use Math::MPFR:from qw<:mpfr>;
my $binary_precision = ceiling( $decimal_precision * log2(10) );
my $bn = Rmpfr_init2($binary_precision);
Rmpfr_set_d($bn, $N , MPFR_RNDN);
Rmpfr_sqrt( $bn, $bn, MPFR_RNDN);
return Rmpfr_get_str($bn, 10, 0, MPFR_RNDN); 
}
say precise_square_root(2, 400);
'
Output:
1.41421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623091229702492483605585073721264412149709993583141322266592750559275579995050115278206057147010955997160597027453459686201472851741864088919860955232923048430871432145083976260362799525140798968725339654633180882964062061525835239505474575028775996172983557522033753185701135437460340849884709

-- 
Hope this helps,
Bruce Gray (Util of PerlMonks)