Re: Pack in perl6

2011-02-14 Thread Bruce Gray

On 02/14/2011 02:41 AM, Carl Mäsak wrote:



--snip--

to me that a Buf could be output without first having been converted
to a Str. A conversion may or may not be necessary for it to work.



S32/IO says that IO.write will output a Buf.


On Feb 14, 2011, at 12:37 AM, Richard Hainsworth wrote:
--snip--

I am not too clear on how pack works in perl5.

Is there a work around in perl6 to achieve the same result, viz.,  
not using pack?


This code works for me on the latest released Rakudo;
   use v6;

   my @fake =
   [  0,  0,  0,  0, -1, -1, -1, -1,  -1,  0,  0,  0, -1, -1,  
-1, -1, ], # 0f, 8f
   [ -1,  0, -1,  0, -1,  0, -1,  0,   0,  0, -1,  0, -1,  0,  
-1,  0, ], # aa, 2a
   [  0, -1,  0, -1,  0, -1,  0, -1,  -1, -1,  0, -1,  0,  
-1,  0, -1, ], # 55, d5

   ;
   my ( $h, $w ) = ( @fake.elems, @fake[0].elems );

   sub dot ( $x, $y ) { return @fake[$x][$y] }

   my $fh = open( 'mandelbrot.pbm', :w, :bin );

   for ^$h -> $x {
   for [^$w].map({ -1 * dot($x, $_) }) {
   $fh.write( Buf.new( :2("$^a$^b$^c$^d$^e$^f$^g 
$^h"), :size(8) ) );

   }
   }

   $fh.close;

Output:
   $ hexdump mandelbrot.pbm
   000 0f 8f aa 2a 55 d5
   006


Note that 8 implicit params (like $^h) are needed, only  
because .batch(8) is not yet implemented.

   for [^$w].map({ -1*dot($x, $_) }).batch(8) -> @b { # NYI
   $fh.write( Buf.new( :2( @b.join('') ), :size(8) ) );
   }

Note also that Buf is specced to work differently than as I used it  
here, and so may change in the future.
Buf.new should have a different API, and a Buf with size(1) should (I  
think) autopack 8 bits into a byte on output.


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



Re: Pack in perl6

2011-02-13 Thread Richard Hainsworth

I am not too clear on how pack works in perl5.

Is there a work around in perl6 to achieve the same result, viz., not 
using pack?


Richard

On 02/14/2011 02:41 AM, Carl Mäsak wrote:

Richard (>):

I came across the idiom

print "P4\n$w $h\n";
for my $y (0..$h-1) {
print pack 'B*', pack 'C*', map dot($_, $y), 0..$w-1;
}

in a perl5 program for outputting dots in a graphical format. dot() produces
a 0 or -1.

I found the 'pack' function in perl6, but it does not seem to work the same.

Two differences that I can think of:

* The pack function in Rakudo is not fully implemented. For the cases
"B*" and "C*", I can definitely imagine that it doesn't work yet.
Patches are welcome. I also plan to find some time to work more on it
myself.

* The fundamental difference in Perl 6 would be that a call to 'pack'
would result in a Buf, not a string as in Perl 5. (This is because
Perl 6 makes a clear separation between sequences of bits/bytes, and
strings of characters. 'pack' deals in the former.) It's not immediate
to me that a Buf could be output without first having been converted
to a Str. A conversion may or may not be necessary for it to work.

// Carl


Re: Pack in perl6

2011-02-13 Thread Carl Mäsak
Richard (>):
> I came across the idiom
>
> print "P4\n$w $h\n";
> for my $y (0..$h-1) {
>    print pack 'B*', pack 'C*', map dot($_, $y), 0..$w-1;
> }
>
> in a perl5 program for outputting dots in a graphical format. dot() produces
> a 0 or -1.
>
> I found the 'pack' function in perl6, but it does not seem to work the same.

Two differences that I can think of:

* The pack function in Rakudo is not fully implemented. For the cases
"B*" and "C*", I can definitely imagine that it doesn't work yet.
Patches are welcome. I also plan to find some time to work more on it
myself.

* The fundamental difference in Perl 6 would be that a call to 'pack'
would result in a Buf, not a string as in Perl 5. (This is because
Perl 6 makes a clear separation between sequences of bits/bytes, and
strings of characters. 'pack' deals in the former.) It's not immediate
to me that a Buf could be output without first having been converted
to a Str. A conversion may or may not be necessary for it to work.

// Carl