gpt4 book ai didi

haskell - 如何一个接一个地遍历数据结构的不同部分?

转载 作者:行者123 更新时间:2023-12-04 21:15:49 27 4
gpt4 key购买 nike

Control.Lens.Traversal beside函数遍历 Bitraversable 的两个部分.给出的例子是

>>> ("hello",["world","!!!"])^..beside id traverse
["hello","world","!!!"]

我可以写一个更明确的 beside 版本吗? (我们称之为 bothParts )而不是 Bitraversable约束需要两个 Traversal ?我想像这样使用它:
>>> ("hello",["world","!!!"])^..bothParts _1 _2 id traverse
["hello","world","!!!"]

这已经存在了吗?这是否太不安全而不能合理使用?谢谢!

编辑:

或者可能是这样的:
>>> ("hello",["world","!!!"])^..bothParts _1 (_2.traverse)
["hello","world","!!!"]

最佳答案

您想要的组合器应该使用 2 Traversal s 同时。但是这种组合器会中断Traversal一般法律,特别是“无重复”法:a Traversal应该只遍历每个元素一次。

以下是您可能不想要的示例:

>>> (1, 2) ^.. bothParts _1 _1
[1, 1]

更准确地说,我想引用 Traversal documentation from lens package :

The laws for a Traversal t follow from the laws for Traversable as stated in "The Essence of the Iterator Pattern".


t pure ≡ pure
fmap (t f) . t g ≡ getCompose . t (Compose . fmap f . g)

One consequence of this requirement is that a Traversal needs to leave the same number of elements as a candidate for subsequent Traversal that it started with. Another testament to the strength of these laws is that the caveat expressed in section 5.5 of the "Essence of the Iterator Pattern" about exotic Traversable instances that traverse the same entry multiple times was actually already ruled out by the second law in that same paper!

关于haskell - 如何一个接一个地遍历数据结构的不同部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23834055/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com