等价于mapM的箭头? [英] Arrow equivalent of mapM?

查看:151
本文介绍了等价于mapM的箭头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力克服&和Arrows一起工作,并且遇到一些困难。我有一个需要 Arrow [a] [b] 的上下文,我想写一个 Arrow ab 并将其映射/排列在箭头内,即 mapM 。具体来说,箭头是一个Hakyll 编译器,但我认为这对于答案来说不重要。



给定一个箭头

  myInnerArrow :: Arrow a => abc 

我怎样才能把它放到一个箭头里

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



<我已经搜遍了基础库,特别是在 Data.List Control.Arrow 中,但是我找不到任何东西看起来它会做这项工作。它是否以我不指望的名字存在?它是否由其他图书馆提供?是否因为某些原因而不能编写代码? 解决方案

你不能没有选择。提升函数将具有以下类型:

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

最简单的实现方法是使用 proc 表示法:

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

未经测试的代码,但应该有效。但请注意,泛型将会非常慢的函数。我建议特别为你的箭头编写这个映射函数。


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?

解决方案

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.

这篇关于等价于mapM的箭头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆