Skip to content

Instantly share code, notes, and snippets.

@mwotton
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mwotton/ef339396a7a9cb34287d to your computer and use it in GitHub Desktop.
Save mwotton/ef339396a7a9cb34287d to your computer and use it in GitHub Desktop.
streamFiles :: IO () -> [FilePath] -> Source IO (Flush Builder)
streamFiles ready filepaths = do
--liftIO $ ready
yield $ Chunk $ fromLazyByteString "{"
mapM_ (\x -> do
DCC.sourceFile x $= transducer
yield $ Chunk $ fromLazyByteString ",\n")
filepaths
yield $ Chunk $ fromLazyByteString "}"
transducer :: ConduitM Text (Flush Builder) m1 a0
transducer = undefined
src/Net/Tribe.hs:264:13:
No instance for (MonadResource IO)
arising from a use of ‘DCC.sourceFile’
In the first argument of ‘($=)’, namely ‘DCC.sourceFile x’
In a stmt of a 'do' block: DCC.sourceFile x $= transducer
In the expression:
do { DCC.sourceFile x $= transducer;
yield $ Chunk $ fromLazyByteString "," }
{-# LANGUAGE OverloadedStrings #-}
module Test where
import Blaze.ByteString.Builder (Builder)
import Blaze.ByteString.Builder (fromLazyByteString)
import Conduit (Flush)
import Conduit (Source)
import Conduit (Flush (..))
import Conduit (yield)
import Conduit (($=))
import Conduit (ConduitM)
import qualified Conduit as DCC
import Data.Text (Text)
import Filesystem.Path.CurrentOS (FilePath)
import Prelude hiding (FilePath)
streamFiles :: IO () -> [FilePath] -> Source IO (Flush Builder)
streamFiles ready filepaths = do
--liftIO $ ready
yield $ Chunk $ fromLazyByteString "{"
mapM_ (\x -> do
DCC.sourceFile x $= transducer
yield $ Chunk $ fromLazyByteString ",\n")
filepaths
yield $ Chunk $ fromLazyByteString "}"
transducer :: ConduitM Text (Flush Builder) m1 a0
transducer = undefined
@mwotton
Copy link
Author

mwotton commented Jun 4, 2014

streamFiles ::  IO () -> [FilePath] -> Source IO (Flush Builder)
streamFiles ready filepaths  = do
  --liftIO $ ready
  yield $ Chunk $ fromLazyByteString "{"
  mapM_ (\x -> do
            -- my god this is a huge hack, but i cannot for the life
            -- of me make DCC.sourceFile do something useful.
            f <- liftIO $ BL.readFile (show x)
            yield $ Chunk $ fromLazyByteString f
            yield $ Chunk $ fromLazyByteString ",\n")
    filepaths
  yield $ Chunk $ fromLazyByteString "}"

does what i want, but not relying on lazy bytestrings is half the reason to use conduit in the first place.

@erikd
Copy link

erikd commented Jun 4, 2014

{-# LANGUAGE OverloadedStrings #-}
module Test where

import Blaze.ByteString.Builder (Builder)
import Blaze.ByteString.Builder (fromLazyByteString)
import Data.Conduit (Flush)
import Data.Conduit (Source)
import Data.Conduit (Flush (..))
import Data.Conduit (yield)
import Data.Conduit (($=))
import Data.Conduit (ConduitM)
import qualified Data.Conduit.Binary as DCC
import Data.ByteString (ByteString)
import Control.Monad.Trans.Resource


streamFiles :: MonadResource m => IO () -> [FilePath] -> Source m (Flush Builder)
streamFiles ready filepaths  = do
  --liftIO $ ready
  yield $ Chunk $ fromLazyByteString "{"
  mapM_ (\x -> do
            _ <- DCC.sourceFile x $= transducer
            yield $ Chunk $ fromLazyByteString ",\n")
        filepaths
  yield $ Chunk $ fromLazyByteString "}"

transducer :: ConduitM ByteString (Flush Builder) m1 a0
transducer = undefined

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment