Perl6运算符问题

发布于 2024-11-16 14:46:49 字数 461 浏览 2 评论 0原文

我正在看愚蠢/可爱/辉煌的“睡眠排序”,它似乎起源于 4chan。要对整数数组进行排序,其想法大致是

    
    foreach elt in @array
        spawn thread(elt)

thread(n) 执行的位置,

    sleep n
    print n

以便更早地打印较小的值。

我知道有一个 Perl6 实现

@foo = @foo>>.&sleep;

>> 'hypers' 操作符,并且这假设 hypering 是自动并行化的。但 .& 让我感到困惑。

谁能解释一下吗?

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

同展鸳鸯锦 2024-11-23 14:46:49

如果您有一个函数 yourfunc,那么您可以使用与符号 &yourfunc 获取对它的引用。语法 $obj.$function 仅使用一个参数 $obj 调用 $function。因此,人们也可以编写 $function($obj) - 只不过这种语法不允许使用 hyper。

但是,无论谁提出这个“实现”,在三个方面都是错误的:

  • 超级运算符允许编译器生成多个线程来执行每个方法,它不必一次为所有这些方法生成一个线程 - 所以“随机排序”不起作用
  • 超级运算符可能会随机化方法的执行顺序,但它必须保留顺序或返回的项目 - 因此 @foo 根本不会被排序,即使第一点不适用。
  • sleep() 应该返回睡眠的秒数,而不是参数。如果有人在计算期间将计算机设置为睡眠状态,则结果可能会大得多。

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文