简体   繁体   中英

Streaming Data.Vector to File using io-streams

I am trying to learn io-streams to stream a Data.Vector.Unboxed to a file on disk; however there is a type mismatch between Int and ByteString. I am not too sure how to align the types of the input and output that will allow to stream it.

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

Here's the type mismatch error:

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'

This is pretty easy to do using 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)

contramap from System.IO.Streams.Combinators lets get an OutputStream of Int from an OutputStream of ByteString.

You only have to supply a conversion function, which can be done with the serialisation class 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
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)

Alternative without using Systems.IO.Streams.Vector stuff but serializing the whole vector to the ByteString OutputStream.

With this version, the data will be easier to recover.

{-# 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM