Gaal Yahas writes:
> It looks like Duff's Device <http://www.lysator.liu.se/c/duffs-device.html>
> won't be possible in Perl6. This is a shame.
>
> sub duff ($from) {
> # real life would use reference here, this is a demo
> # dummy: simulate write to serial i/o port
> my $to;
> my $i = 0;
>
> my ($n, $count);
> $count = $from.chars;
>
> $n = ($count + 7) / 8; # use integer in effect
* There's no such thing as `use integer`. You just declare $n and
$count to be ints.
my int ($n, $count);
>
> %MY::Â{"l" ~ $count % 8}Â.goto();
> l0: do { $to ~= (substr $from, $i++, 1);
> l7: $to ~= (substr $from, $i++, 1);
> l6: $to ~= (substr $from, $i++, 1);
> l5: $to ~= (substr $from, $i++, 1);
> l4: $to ~= (substr $from, $i++, 1);
> l3: $to ~= (substr $from, $i++, 1);
> l2: $to ~= (substr $from, $i++, 1);
> l1: $to ~= (substr $from, $i++, 1);
> } while (--$n>0);
* `do ... while` is now named `loop ... while`.
> return $to;
> }
>
> In Pugs at least, labels may only only be assigned to blocks.
I believe Perl 6 hasn't changed its policy on labels, so you should be
able to write that in Perl 6. But your behavior might be undefined.
It's weird to jump into the middle of a loop. We may only allow you to
jump outwards from your dynamic scope.
Don't cry though. Duff's device is an optimization technique, which
would probably do nothing for a Perl 6 program. You have to optimize
dynamic languages in very different ways from how you optimize hardware.
Luke