Perl6 operator question
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 th开发者_Python百科at this assumes hypering is automatically parallelized. But the .&
confuses me.
Can anyone explain this?
thanks
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.
精彩评论