我正在尝试学习io-stream以将Data.Vector.Unboxed流式传输到磁盘上的文件;但是Int和ByteString之间存在类型不匹配。我不太确定如何对齐允许流式传输的输入和输出类型。
import qualified Data.Vector.Unboxed as V
import System.IO.Streams.Core
import System.IO.Streams.File
import System.IO.Streams.Vector
new :: V.Vector Int
new = V.generate 1000000 (\i -> 1)
main :: IO ()
main = do
withFileAsOutput "test.dat" $ \os -> writeVector new os
这是类型不匹配错误:
iostream.hs:12:66:
Couldn't match type `Data.ByteString.Internal.ByteString'
with `Int'
Expected type: OutputStream Int
Actual type: OutputStream Data.ByteString.Internal.ByteString
In the second argument of `writeVector', namely `os'
In the expression: writeVector new os
In the second argument of `($)', namely
`\ os -> writeVector new os'
答案 0 :(得分:2)
使用pipes
:
import Data.ByteString (hPut)
import qualified Data.Vector.Unboxed as V
import Pipes
import Pipes.Binary (encode)
import qualified System.IO as IO
new :: V.Vector Int
new = V.generate 1000000 (\i -> 1)
main = IO.withFile "test.dat" IO.WriteMode $ \handle ->
runEffect $ for (V.mapM_ encode new) (lift . hPut handle)
答案 1 :(得分:1)
contramap让我们从ByteString的OutputStream中获取Int的OutputStream。
您只需提供转换功能,可以使用序列化类Binary完成。
import qualified Data.Vector.Unboxed as V
import System.IO.Streams.Core
import System.IO.Streams.File
import System.IO.Streams.Vector
import System.IO.Streams.Combinators as SC
import Data.ByteString.Lazy as LBS
import Data.ByteString as BS
import Data.Binary (Binary, put, encode)
import Data.Binary.Put (runPut)
new :: V.Vector Int
new = V.generate 1000000 (\i -> 1)
toBS :: Binary a => a -> BS.ByteString
toBS = LBS.toStrict . encode -- Data.Binary.encode = runPut . put
main :: IO ()
main = do
withFileAsOutput "test.dat" $ \bsOStream -> do
intOStream <- SC.contramap toBS bsOStream
writeVector new intOStream
答案 2 :(得分:1)
import qualified Data.Vector.Unboxed as V
import System.IO.Streams as S
import Data.ByteString.Lazy (toStrict)
import Data.Binary (encode)
new :: V.Vector Int
new = V.generate 100 (\i -> 1)
main :: IO ()
main = S.withFileAsOutput "test.dat" (\outStream -> do
inVectorStream <- S.fromVector new
inByteStringStream <- S.map (toStrict . encode) inVectorStream
S.connect inByteStringStream outStream)
答案 3 :(得分:0)
替代方法,不使用Systems.IO.Streams.Vector,但将整个向量序列化为ByteString OutputStream。
使用此版本,数据将更容易恢复。
{-# LANGUAGE PackageImports #-}
import qualified Data.Vector.Unboxed as V
import System.IO.Streams.Core
import System.IO.Streams.File
import System.IO.Streams.Vector
import Data.ByteString.Lazy as LBS
import Data.ByteString as BS
import Data.Binary (put, Binary)
import Data.Binary.Put (runPut)
import "vector-binary-instances" Data.Vector.Binary () -- binary instances
new :: V.Vector Int
new = V.generate 1000000 (\i -> 1)
toBS :: Binary a => a -> BS.ByteString
toBS = (LBS.toStrict . runPut . put)
main :: IO ()
main = do
withFileAsOutput "test.dat" $ \bsOStream -> do
write (Just $ toBS new) bsOStream