Safe Haskell | None |
---|---|
Language | Haskell2010 |
Streamly.Compat.Text.Lazy
Synopsis
- chunkReader :: forall (m :: Type -> Type). Monad m => Unfold m Text (Array Word8)
- reader :: forall (m :: Type -> Type). Monad m => Unfold m Text Word8
- toChunks :: forall (m :: Type -> Type). Monad m => Text -> Stream m (Array Word8)
- unsafeFromChunks :: Monad m => Stream m (Array Word8) -> m Text
- unsafeFromChunksIO :: Stream IO (Array Word8) -> IO Text
Documentation
reader :: forall (m :: Type -> Type). Monad m => Unfold m Text Word8 Source #
Unfold a lazy Text
to a stream of Word8
unsafeFromChunks :: Monad m => Stream m (Array Word8) -> m Text Source #
Convert a serial stream of Array
Word8
to a lazy Text
.
This function is unsafe: the caller must ensure that each Array
Word8
element in the stream is a valid UTF-8 encoding.
IMPORTANT NOTE: This function is lazy only for lazy monads (e.g. Identity). For strict monads (e.g. IO) it consumes the entire input before generating the output. For IO monad please use unsafeFromChunksIO instead.
For strict monads like IO you could create a newtype wrapper to make the monad bind operation lazy and lift the stream to that type using hoist, then you can use this function to generate the text lazily. For example you can wrap the IO type to make the bind lazy like this:
newtype LazyIO a = LazyIO { runLazy :: IO a } deriving (Functor, Applicative) liftToLazy :: IO a -> LazyIO a liftToLazy = LazyIO instance Monad LazyIO where return = pure LazyIO a >>= f = LazyIO (unsafeInterleaveIO a >>= unsafeInterleaveIO . runLazy . f)
unsafeFromChunks can then be used as,
{-# INLINE unsafeFromChunksIO #-}
unsafeFromChunksIO :: Stream IO (Array Word8) -> IO Text
unsafeFromChunksIO str = runLazy (unsafeFromChunks (Stream.hoist liftToLazy str))