如何变异STVector? [英] How to mutate an STVector?

查看:60
本文介绍了如何变异STVector?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MVector 有两种样式,分别是 IOVector STVector .我想编写一些使用 STVector 的函数,以便尽管使用 Data.Vector.Algorithms 中的快速可变矢量算法,也可以从纯代码中调用它们.

借助相关线程,我已经到了这一步:我可以将一个不变的 Vector 粘贴到一个可变的 ST 上下文中:

 导入Control.Monad.ST导入Data.Vector导入Data.Vector.Algorithms.Intro(排序)x = fromList [1,4,2] :: Vector IntverboseCopy :: Vector IntverboseCopy = runST $ do v<-解冻x冻结v 

我只需要在解冻和冻结之间运行 sort .

也许令人惊讶的是,我不必 import Data.Vector.Mutable ,这就是定义 STVector 的地方.也许我应该使用类型签名来指定要 thaw 生成 STVector ,但是我不知道如何:如果更改 thaw 到此:

  v<-解冻x :: Data.Vector.Mutable.STVector s Int 

我收到此错误:

 •无法匹配预期的类型'MVector(原始0.6.3.0:Control.Monad.Primitive.PrimState(ST s))诠释实际类型为"Int"•在冻结"的第一个参数中,即"v" 

解决方案

您应该可以写:

  verboseCopy :: Vector IntverboseCopy = runST $ do v<-解冻x排序v冻结v 

给予:

 >verboseCopy[1,2,4]> 

sort v 会对可变向量 v 产生副作用,因此不需要保存"或捕获"排序结果,如果那就是你所担心的.

您无需显式键入 v .Haskell将其推断为可变向量,并将其适当地视作 IOVector STVector ,具体取决于您是在IO还是ST monad中使用它.

为您提供信息,出现错误的原因是您提供的类型是 v 的类型,但已将其应用于 thaw x 类型更复杂.如果您写:

  verboseCopy :: Vector IntverboseCopy = runST $ do v<-解冻x :: ST s(STVector s Int)排序v冻结v 

然后将输入check.但是,这又是不必要的,并且完全不会改变行为.Haskell已经为您找到了这种类型.

MVector comes in two flavors, IOVector and STVector. I want to write some functions that use STVector, so that they can be called from pure code despite using the fast mutable-vector algorithms in Data.Vector.Algorithms.

With the help of a related thread, I've gotten partway there: I can stick an immutable Vector into a mutable ST context:

import Control.Monad.ST
import Data.Vector
import Data.Vector.Algorithms.Intro (sort)

x = fromList [1,4,2] :: Vector Int

verboseCopy :: Vector Int
verboseCopy = runST $ do v <- thaw x
                         freeze v

I just need to run sort between the thaw and the freeze.

Perhaps surprisingly, I did not have to import Data.Vector.Mutable, which is where STVector is defined. Maybe I should use a type signature to specify that I want thaw to produce an STVector, but I don't know how: If I change the thaw line to this:

v <- thaw x :: Data.Vector.Mutable.STVector s Int

I get this error:

• Couldn't match expected type ‘MVector
                                  (primitive-0.6.3.0:Control.Monad.Primitive.PrimState (ST s))
                                  Int’
              with actual type ‘Int’
• In the first argument of ‘freeze’, namely ‘v’

解决方案

You should be fine to write:

verboseCopy :: Vector Int
verboseCopy = runST $ do v <- thaw x
                         sort v
                         freeze v

giving:

> verboseCopy
[1,2,4]
>

The sort v performs the sort as a side effect on the mutable vector v, so there's no need to "save" or "capture" the sort result, if that's what you were worried about.

You don't need to explicitly type v. Haskell will infer it to be a mutable vector and will treat it appropriately as an IOVector or STVector depending on whether you are using it within the IO or ST monad.

For your information, the reason you're getting the error is that the type you're supplying is for v, but you've applied it to thaw x which has a more complex type. If you write:

verboseCopy :: Vector Int
verboseCopy = runST $ do v <- thaw x :: ST s (STVector s Int)
                         sort v
                         freeze v

then it'll type check. However, again, this is unnecessary and won't change the behavior at all. Haskell has already figured out this type for you.

这篇关于如何变异STVector?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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