Safe Haskell | None |
---|
List conversions and utilities.
- fromList :: [c] -> IO (InputStream c)
- toList :: InputStream a -> IO [a]
- outputToList :: (OutputStream a -> IO b) -> IO [a]
- writeList :: [a] -> OutputStream a -> IO ()
- chunkList :: Int -> InputStream a -> IO (InputStream [a])
- concatLists :: InputStream [a] -> IO (InputStream a)
- listOutputStream :: IO (OutputStream c, IO [c])
List conversions
fromList :: [c] -> IO (InputStream c)Source
Transforms a list into an InputStream
that produces no side effects.
ghci> is <- Streams.fromList
[1, 2] ghci>replicateM
3 (Streams.read
is) [Just 1, Just 2, Nothing]
toList :: InputStream a -> IO [a]Source
Drains an InputStream
, converting it to a list. N.B. that this function
reads the entire InputStream
strictly into memory and as such is not
recommended for streaming applications or where the size of the input is not
bounded or known.
ghci> is <- Streams.fromList
[1, 2] ghci> Streams.toList
is [1, 2]
outputToList :: (OutputStream a -> IO b) -> IO [a]Source
Given an IO action that requires an OutputStream
, creates one and
captures all the output the action sends to it as a list.
Example:
ghci> import Control.Applicative ghci> (connect
$fromList
["a", "b", "c"]) >>=outputToList
["a","b","c"]
writeList :: [a] -> OutputStream a -> IO ()Source
Feeds a list to an OutputStream
. Does not write an end-of-stream to
the stream.
ghci> os <- Streams.unlines
Streams.stdout
>>= Streams.contramap
(S.pack . show) :: IO (OutputStream
Int) ghci> Streams.writeList
[1, 2] os 1 2 ghci> Streams.writeList
[3, 4] os 3 4
Utility
:: Int | chunk size |
-> InputStream a | stream to process |
-> IO (InputStream [a]) |
concatLists :: InputStream [a] -> IO (InputStream a)Source
Given an input stream containing lists, produces a new input stream that
will yield the concatenation of these lists. See concat
.
Example:
ghci> Streams.fromList
[[1,2,3::Int], [4,5,6]] >>= Streams.concatLists
>>= Streams.toList
[1,2,3,4,5,6]
listOutputStream :: IO (OutputStream c, IO [c])Source
listOutputStream
returns an OutputStream
which stores values fed into
it and an action which flushes all stored values to a list.
The flush action resets the store.
Note that this function will buffer any input sent to it on the heap. Please don't use this unless you're sure that the amount of input provided is bounded and will fit in memory without issues.
ghci> (os, flush) <- Streams.listOutputStream
:: IO (OutputStream
Int, IO [Int]) ghci> Streams.writeList
[1, 2] os ghci> flush [1, 2] ghci> Streams.writeList
[3, 4] os ghci> flush [3, 4]