Perl6 hyper » operator doesn't work like map

我的未来我决定 提交于 2019-12-22 04:47:09

问题


As far as I undertand it, the hyper operator » is a shortcut for map(). Why does the following return two different results and in the second example .sum seems not to be applied?

say ([1,2], [2, 2], [3, 3]).map({.sum});
# (3 4 6)
say ([1,2], [2, 2], [3, 3])».sum;
# ([1 2] [2 2] [3 3])

回答1:


Hyperops descend recursively into sublists. Also they are candidates for autothreading (NYI) what means their operations are out of order.

Also there was a bug that is corrected with https://github.com/rakudo/rakudo/commit/c8c27e93d618bdea7de3784575d867d9e7a2f6cb .

say ([1,2], [2, 2], [3, 3])».sum;
# (3 4 6)



回答2:


TL;DR You've almost certainly encountered a bug. That said, map and the » hyperop have major differences.

map returns a Seq. This Seq yields the results of applying user supplied code to each of the elements of a user supplied data structure:

  • one level deep (traversal of the data structure is shallow -- map doesn't recursively descend in to sub structures of the data structure's top level)
  • one at a time (everything is done sequentially, nothing in parallel)
  • lazily (map returns immediately; the user supplied code is applied to the user supplied data structure to generate results later as needed to pull values from the Seq)

The » hyperop returns the data structure operand on its left after first applying the unary operation on its right to the elements of that data structure:

  • only one level deep or descending to leaves as dictated by the unary operation
  • in parallel batches, at least semantically (it's the programmer's responsibility to pick an unary operation that will yield correct results when applied to multiple elements in parallel in arbitrary order)
  • eagerly (unlike a map call, a hyperoperation only returns when the unary operator has been applied to the entire data structure)

If you're applying an unary operator that is "nodal" (so hyperoperation will choose not to descend) or the data structure being operated on is only one level deep (so there are no lower level leaves for the hyperoperation to descend to) then the differences between hyperoperation and map with an unary operator are just the sequential/parallel and lazy/eager aspects.

It seems pretty clear to me that sum ought to be a nodal operator otherwise it will descend in to sub structures until it arrives at individual leaves and will thus end up being applied to a bunch of single values which is pointless. ETA: Looks like it's fixed now.



来源:https://stackoverflow.com/questions/38741605/perl6-hyper-operator-doesnt-work-like-map

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!