Re: split and nils?

2019-02-07 Thread Brad Gilbert
First off a Str is a singular value, not a list.

Which is a good thing.

my $a = "abc";
my $b = "\c[COMBINING ACUTE ACCENT]";

say $a.chars; # 3
say $b.chars; # 1

If they were a list, then combining them should create something that
is 4 chars long, but it doesn't.

say ($a ~ $b).chars; # 3
say "$a$b".chars; # 3

Also let's look at the Unicode names for the characters

.say for $a.uninames;
# LATIN SMALL LETTER A
# LATIN SMALL LETTER B
# LATIN SMALL LETTER C

.say for $b.uninames;
# COMBINING ACUTE ACCENT

Now what is the Unicode names for the combined Str?

.say for ($a ~ $b).uninames;
# LATIN SMALL LETTER A
# LATIN SMALL LETTER B
# LATIN SMALL LETTER C WITH ACUTE

---

The `@` is also perfectly consistent.

my $a = [1,2];
my $b = 'abc';
my $c = 45;
my $d = List;
my $e = List.new;

say $a.perl;   # $[1, 2] # The `$` makes it act like a singular value
say $b.perl;   # "abc"
say $c.perl;   # 45
say $d.perl;   # List
say $e.perl;   # $( )

say @$a.perl;  # [1, 2]
say @$b.perl;  # ("abc",)
say @$c.perl;  # (45,)
say @$d.perl;  # (List,)
say @$e.perl;  # ()

The `@$…` is short for `@($…)`

say @('abc').perl; # ("abc",)
say @(45).perl; #(45,)
say @(List).perl; # (List,)
say @(List.new).perl; # ()

It removes the item context from something that is [an instance of] a list, or
it creates a single element list with that value.

Basically @(…) always returns a list, and singular items act like a
list with a single value in them.

say 123.elems; # 1
say "abc".elems; # 1

say "".elems; # 1

---

Since a Str is an opaque object, the internals can store them however they like.

Someone posted Perl6 code and the C[++] equivalent in #perl6 once.
They reported that the Perl6 code was faster.
My guess is that since C[++] treats strings as an array it has to copy
the strings repeatedly.

So Perl6 is faster because a Str is a singular object.

The way MoarVM deals with strings structurally similar to the
following Perl6ish pseudo code:
(There are mistakes, but they should not detract from it I hope. Some
of the mistakes are even intentional.)

role STRING {}

# strings that are valid ASCII
class STRING_RAW8 does STRING {
has int32 $.length;
has Buf[int8] $.buffer;
}
# strings which contain Unicode outside of the ASCII range
class STRING_NFG32 does STRING {
has int32 $.length;
has Buf[int32] $.buffer;
}
# strings made from other strings
class STRING_CONCAT does STRING {
has STRING @.a;
}
# a string made out of part of another string
class STRING_SUBSTR does STRING {
has STRING $.ref;
has int32 $.position;
has int32 $.length;
}

So when you write something like this:

us v6;
my $a = "123";
my $b = "⅒";
my $c = "$a and $b";

That turns into this:

# pseudo Perl6ish
my Str $a = STRING_RAW8( "123" );
my Str $b = STRING_NFG32( "⅒" );
my Str $TEMP = STRING_RAW8( " and " );
my Str $c = STRING_CONCAT( $a, $TEMP, $b );

If you then get a substring out of it:

us v6;
my Str $d = $c.substr(0,2);

It does something like

# pseudo Perl6ish
my STRING $d = STRING_SUBSTR( $c, 0, 2 );

# the whole structure
STRING_SUBSTR(
STRING_CONCAT(
STRING_RAW8( "123" ),
STRING_RAW8( " and " ),
STRING_NFG32( "⅒" )
),
0,
2
)

At no point was the contents of the STRING ever copied.
In fact it didn't have to read the contents of the STRING at all.

(In the Real World, string concatenation does have to look at the
first and last characters of each segment for ones that will combine.)

---

Basically C has to copy the contents of strings while MoarVM can just
copy pointers to string objects.

If Perl6 treated strings as an Array then some of this performance
improvement wouldn't quite work as well.

Let's pretend that it acts like an Array:

us v6;
my Str $e = $c[0,1];

That would result in the following Per6ish VM code:

# pseudo Perl6ish
my $e = STRING_CONCAT( STRING_SUBSTR( $c, 0, 1 ), STRING_SUBSTR(
$c, 1, 1 ) );

# the whole structure
STRING_CONCAT(
STRING_SUBSTR(
STRING_CONCAT(
STRING_RAW8( "123" ),
STRING_RAW8( " and " ),
STRING_NFG32( "⅒" )
),
0,
1
),
STRING_SUBSTR(
STRING_CONCAT(
STRING_RAW8( "123" ),
STRING_RAW8( " and " ),
STRING_NFG32( "⅒" )
),
1,
1
)
)

Translating that back into real Perl6

use v6;
my Str $e = substr( $c, 0, 1 ) ~ substring( $c, 1, 1 );

So if Perl6 did treat Str as an Array, then it would be slower, and
use more memory.
It also might not be able handle Unicode correctly.

Also 

print hex?

2019-02-07 Thread ToddAndMargo via perl6-users

Hi All,

What is the easiest way to get both print and say to print $i below in Hex?

$ p6 'my Buf $x=Buf.new(0xAE,0x5D); my int16 $i=0x5DAE; say $x; say $i;'

Buf:0x
23982

Many thanks,
-T


Re: print hex?

2019-02-07 Thread Curt Tilmes
On Thu, Feb 7, 2019 at 2:14 PM ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> What is the easiest way to get both print and say to print $i below in Hex?
>
> $ p6 'my Buf $x=Buf.new(0xAE,0x5D); my int16 $i=0x5DAE; say $x; say $i;'
>
> Buf:0x
> 23982


say $i.base(16)


Re: print hex?

2019-02-07 Thread Todd Chester via perl6-users




On 2/7/19 11:22 AM, Curt Tilmes wrote:
On Thu, Feb 7, 2019 at 2:14 PM ToddAndMargo via perl6-users 
mailto:perl6-users@perl.org>> wrote:


What is the easiest way to get both print and say to print $i below
in Hex?

$ p6 'my Buf $x=Buf.new(0xAE,0x5D); my int16 $i=0x5DAE; say $x; say $i;'

Buf:0x
23982


say $i.base(16)




Hi Curt,

   A thing of beauty!  Thank you!

-T

$ p6 'my Buf $x=Buf.new(0xAE,0x5D); my int16 $i=0x5DAE; say $x; say 
$i.base(0x10); print $i.base(16) ~ "\n"'


Buf:0x
5DAE
5DAE


buf to integer?

2019-02-07 Thread Todd Chester via perl6-users

Hi All,

I am dealing with a Buf what includes 32 bit integers, but
they are entered somewhat backwards as view with hexedit:

AE 5D 5C 72 represents the number 725C5DAE

This is what I have come up with to convert this type of
number in a buffer to and integer

$ p6 'my Buf $x=Buf.new(0xAE,0x5D,0x5C,0x72); my int32 $i=$x[3] +< 0x18 
+  $x[2] +< 0x10  +  $x[1] +< 0x08  +  $x[0];  say $x; say $i.base(0x10);'


Buf:0x
725C5DAE


Is there a more "elegant" way to do this?

Many thanks,
-T


Re: shift left syntax?

2019-02-07 Thread Todd Chester via perl6-users




On 2/7/19 10:36 PM, yary wrote:

perl6 -e 'my $i = 0x5DAE; $i +<= 1; say $i.base(0x10);'

BB5C

-y




Hi Yary,

$ p6 'my Buf $x=Buf.new(0xAE,0x5D); my int32 $i=0x5DAE; say $x; say 
$i.base(0x10); $i +< 0x01; say $i.base(0x10);'


WARNINGS for -e:
Useless use of "+<" in expression "$i +< 0x01" in sink context (line 1)
Buf:0x
5DAE
5DAE


Ah poop!  I forgot the =

$ p6 'my int32 $i=0x5DAE; say $i.base(0x10); $i +<= 0x01; say 
$i.base(0x10);'

5DAE
BB5C


Thank you!

-T


shift left syntax?

2019-02-07 Thread Todd Chester via perl6-users

Hi All,

Is this the only way to shift left?

  $i = $i +< 0x01

$ p6 'my int32 $i=0x5DAE; say $i.base(0x10); $i = $i +< 0x01; say 
$i.base(0x10);'


5DAE
BB5C


Does we have any of those fancy += ~= ways of doing it?

Many thanks,
-T