lazy-io-streams (empty) → 0.1.0.0
raw patch · 5 files changed
+104/−0 lines, 5 filesdep +basedep +bytestringdep +io-streamssetup-changed
Dependencies added: base, bytestring, io-streams
Files
- LICENSE +30/−0
- README.md +13/−0
- Setup.hs +2/−0
- lazy-io-streams.cabal +18/−0
- src/System/IO/Streams/Lazy.hs +41/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2016, Luke Clifton++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Luke Clifton nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ README.md view
@@ -0,0 +1,13 @@+A library to do naughty things with+[io-streams](https://hackage.haskell.org/package/io-streams).++It let's you:++- convert `InputStream`s to lazy lists+- build lazy lists by feeding an `OutputStream`+- convert `InputStream ByteString`s to lazy `ByteStrings`++This can be handy when you are interfacing with other libraries that do+the right thing in the presence of lazy IO, but don't provide an+io-streams interface. Of course, the correct solution is to contribute+the io-streams interface.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ lazy-io-streams.cabal view
@@ -0,0 +1,18 @@+name: lazy-io-streams+version: 0.1.0.0+synopsis: Get lazy with your io-streams+license: BSD3+license-file: LICENSE+author: Luke Clifton+maintainer: ltclifton@gmail.com+build-type: Simple+extra-source-files: README.md+cabal-version: >=1.10++library+ exposed-modules: System.IO.Streams.Lazy+ build-depends: base >=4.8 && <4.10+ , bytestring+ , io-streams+ hs-source-dirs: src+ default-language: Haskell2010
+ src/System/IO/Streams/Lazy.hs view
@@ -0,0 +1,41 @@+{-# OPTIONS_GHC -fno-warn-tabs #-}+-- | This module provides some arguably evil functions+-- for turning your nice io-streams into lazy lists.+module System.IO.Streams.Lazy where++import Prelude hiding (read)+import qualified Data.ByteString as BS+import qualified Data.ByteString.Lazy as BL+import System.IO.Streams (InputStream, OutputStream, read)+import System.IO.Streams.Concurrent (makeChanPipe)+import System.IO.Unsafe (unsafeInterleaveIO)++-- | Convert an input stream into a lazy list.+toLazyList :: InputStream a -> IO [a]+toLazyList is = unsafeInterleaveIO $ do+ ma <- read is+ case ma of+ Nothing -> return []+ Just a -> do+ as <- toLazyList is+ return (a : as)++-- | Create an output stream and a lazy list that contains the values+-- that you feed into that output stream. Make sure you evaluate the list+-- in a separate thread to the one you write to the stream in.+lazyListOutput :: IO (OutputStream a, [a])+lazyListOutput = do+ (is, os) <- makeChanPipe+ as <- toLazyList is+ return (os, as)++-- | Convert an input stream of strict `ByteString`s to a lazy `ByteString`.+toLazyByteString :: InputStream BS.ByteString -> IO BL.ByteString+toLazyByteString is = BL.fromChunks <$> toLazyList is++-- | Create a lazy `ByteString` by feeding an `OutputStream` strict+-- `ByteString`s+lazyByteStringOutput :: IO (OutputStream BS.ByteString, BL.ByteString)+lazyByteStringOutput = do+ (os, ll) <- lazyListOutput+ return (os, BL.fromChunks ll)