Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl6 operator question

Tags:

operators

raku

I was looking at the silly/cute/brilliant "sleep sort" that seems to have originated over at 4chan. To sort an array of ints, the idea is roughly

    
    foreach elt in @array
        spawn thread(elt)

where thread(n) does

    sleep n
    print n

so the smaller values get printed earlier.

There's a Perl6 implementation

@foo = @foo>>.&sleep;

I get that >> 'hypers' the operator, and that this assumes hypering is automatically parallelized. But the .& confuses me.

Can anyone explain this?

thanks

like image 663
jonathan Avatar asked Jun 22 '11 20:06

jonathan


1 Answers

If you have a function yourfunc, then you can grab a reference to it with the ampersand, &yourfunc. The syntax $obj.$function just invokes $function with one argument, $obj. So one could just as well write $function($obj) - except that this syntax doesn't allow the use of a hyper.

But whoever came up with this "implementation" was wrong on three accounts:

  • The hyper operator allows the compiler to spawn a number of threads for executing each method, it doesn't have to spawn a thread for all of them at once - so the "random sort" can't work
  • The hyper operator may randomize the order of execution of the methods, but it has to preserve the order or the returned items - so @foo will not be sorted at all, even if the first point didn't apply.
  • sleep() is supposed to return the number of seconds slept, not the argument. If somebody sets the computer to sleep during the calculation, the result might be a much higher number.
like image 152
moritz Avatar answered Sep 28 '22 06:09

moritz