【问题标题】:Is there something in between Traversable and MonoTraversable?Traversable 和 MonoTraversable 之间有什么关系吗?
【发布时间】:2019-09-04 19:00:35
【问题描述】:

我有一个看起来很像某种遍历的数据类型和伴随的函数。这是一个简化的示例:

data Foo x = MkFoo (Bar x) (Bar x)

almostTraverse :: Applicative f => (Bar a -> f (Bar b)) -> Foo a -> f (Foo b)
almostTraverse f (MkFoo x y) = MkFoo <$> f x <*> f y

假设Bar 是某种不透明类型,不一定是函子。是否有一些类型类是almostTraverse 的泛化?不是otraverse from MonoTraversable,因为applicative里面的结果类型不必和输入类型完全一样,也不是Traversable里面的traverse,因为传入的函数需要请注意Bar,尽管它不在类型参数中。

【问题讨论】:

    标签: haskell types traversal typeclass parametric-polymorphism


    【解决方案1】:

    镜头包将其称为Traversal

    almostTraverse :: Traversal (Foo a) (Foo b) (Bar a) (Bar b)
    

    您可以考虑创建Each 类型类的实例,如

    instance Each (Foo a) (Foo b) (Bar a) (Bar b) where
        each f (MkFoo x y) = liftA2 MkFoo (f x) (f y)
    

    【讨论】:

    • 我重定向了链接,这样它们就不会进入大量的Control.Lens.Combinators。希望没关系。
    猜你喜欢
    • 1970-01-01
    • 2017-09-15
    • 1970-01-01
    • 1970-01-01
    • 2013-10-30
    • 2015-01-02
    • 2012-07-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多