Arrow equivalent of mapM?

删除回忆录丶 提交于 2020-01-12 12:53:06

问题


I'm trying to grok & work with Arrows, and am having some difficulty. I have a context where I need an Arrow [a] [b], and I want to write an Arrow a b and map/sequence it inside the arrow, a la mapM. Specifically, the arrow is a Hakyll Compiler, but I don't think that matters much for the answer.

Given an arrow

myInnerArrow :: Arrow a => a b c

How can I lift this into an arrow

myOuterArrow :: Arrow a => a [b] [c]

?

I have scoured the base library, particularly in Data.List and Control.Arrow, but I cannot find anything that looks like it will do the job. Does it exist under a name I do not expect? Is it provided by some other library? Is it impossible to write for some reason?


回答1:


You can't without choice. The lifting function will have this type:

mapA :: (ArrowChoice a) => a b c -> a [b] [c]

The easiest way to implement is by using proc notation:

mapA c =
    proc xs' ->
        case xs' of
            [] -> returnA -< []
            (x:xs) -> uncurry (:) ^<< c *** mapA c -< (x, xs)

Untested code, but should work. Note however that a function that generic is going to be really slow. I recommend writing this mapping function for your arrow specifically.



来源:https://stackoverflow.com/questions/11303642/arrow-equivalent-of-mapm

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