Reputation: 627
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'
Upvotes: 0
Views: 187
Reputation: 607
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)
Upvotes: 1
Reputation: 6738
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
Upvotes: 1
Reputation: 6738
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
Upvotes: 0
Reputation: 35089
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)
Upvotes: 2