Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Various cleanups.

  • Loading branch information...
commit 1f7e151afd7cc607cad623647b64e6345b6aa59c 1 parent c3f0d38
@pcapriotti authored
View
21 Control/Pipe/Binary.hs
@@ -7,9 +7,6 @@ module Control.Pipe.Binary (
handleWriter,
-- ** Chunked Byte Stream Manipulation
- take,
- takeWhile,
- dropWhile,
lines,
bytes,
) where
@@ -19,9 +16,8 @@ import Control.Monad.IO.Class
import Control.Pipe
import Control.Pipe.Class
import Control.Pipe.Exception
-import Control.Pipe.Combinators (tryAwait, feed)
+import Control.Pipe.Combinators (feed)
import qualified Data.ByteString as B
-import Data.Monoid
import Data.Word
import System.IO
import Prelude hiding (take, takeWhile, dropWhile, lines, catch)
@@ -69,19 +65,6 @@ handleWriter :: (MonadStream m, MonadIO (BaseMonad m))
=> Handle -> m B.ByteString Void r r
handleWriter h = forP $ liftIO . B.hPut h
--- | Act as an identity for the first 'n' bytes, then terminate returning the
--- unconsumed portion of the last chunk.
-take :: MonadStream m => Int -> m B.ByteString B.ByteString u B.ByteString
-take = C.take
-
--- | Act as an identity as long as the given predicate holds, then terminate
--- returning the unconsumed portion of the last chunk.
-takeWhile :: MonadStream m => (Word8 -> Bool) -> m B.ByteString B.ByteString u B.ByteString
-takeWhile = C.takeWhile
-
--- | Drop bytes as long as the given predicate holds, then act as an identity.
-dropWhile :: MonadStream m => (Word8 -> Bool) -> m B.ByteString B.ByteString r r
-dropWhile = C.dropWhile
-- | Split the chunked input stream into lines, and yield them individually.
lines :: MonadStream m => m B.ByteString B.ByteString r r
@@ -89,4 +72,4 @@ lines = C.splitOn (10 :: Word8)
-- | Yield individual bytes of the chunked input stream.
bytes :: MonadStream m => m B.ByteString Word8 r r
-bytes = forP $ B.foldl (\p c -> p >> yield c) (return ())
+bytes = C.elems
View
18 Control/Pipe/Chunked.hs
@@ -1,6 +1,13 @@
{-# LANGUAGE FlexibleContexts #-}
-module Control.Pipe.Chunked where
+module Control.Pipe.Chunked
+ ( gtake
+ , take
+ , takeWhile
+ , dropWhile
+ , splitOn
+ , elems
+ ) where
import Control.Monad
import Control.Pipe
@@ -11,7 +18,7 @@ import Data.Generator
import Data.Generator.Length
import Data.Monoid
import Data.Monoid.MonoidEq
-import Data.Monoid.MonoidSplit
+import Data.Monoid.MonoidSplit hiding (take)
import Prelude hiding (null, take, drop, takeWhile, dropWhile, length)
@@ -28,14 +35,19 @@ gtake done update s
then gtake done update (update chunk' s)
else return leftover
+-- | Act as an identity for the first 'n' elements of the chunk, then terminate
+-- returning the unconsumed portion of the last chunk.
take :: (MonadStream m, MonoidEq a, GeneratorLength a, MonoidSplit Int a)
=> Int -> m a a u a
take = gtake (<= 0) (\chunk n -> n - length chunk)
+-- | Act as an identity as long as the given predicate holds, then terminate
+-- returning the unconsumed portion of the last chunk.
takeWhile :: (MonadStream m, MonoidEq a, MonoidSplit (c -> Bool) a)
=> (c -> Bool) -> m a a u a
takeWhile = gtake (const False) (const id)
+-- | Drop elemetns as long as the given predicate holds, then act as an identity
dropWhile :: (MonadStream m, MonoidEq a, MonoidSplit (c -> Bool) a)
=> (c -> Bool) -> m a a r r
dropWhile p = do
@@ -43,6 +55,7 @@ dropWhile p = do
yield leftover
idP
+-- | Split stream on the given element
splitOn :: (MonadStream m, MonoidEq a, MonoidSplit c a, MonoidSplit Int a)
=> c -> m a a r r
splitOn c = go mempty
@@ -61,5 +74,6 @@ splitOn c = go mempty
split_chunk (drop (1::Int) rest) mempty
where (line, rest) = split c chunk
+-- | Yield individual elements of the stream
elems :: (MonadStream m, Generator a) => m a (Elem a) r r
elems = forP $ \chunk -> mapM_ yield (reduce chunk)
View
8 Control/Pipe/Text.hs
@@ -2,6 +2,7 @@ module Control.Pipe.Text
( decode
, encode
, lines
+ , characters
) where
import Data.Bits
@@ -46,14 +47,21 @@ decodeChunk on_err prefix' chunk = (text, rest)
text = T.decodeUtf8With on_err $ prefix' <> prefix'' <> chunk''
+-- | Convert a utf-8 encoded byte stream into chunks of text
decode :: MonadStream m => T.OnDecodeError -> m ByteString Text r r
decode on_err = withDefer $ go mempty
where
go prefix = await >>= \chunk -> case decodeChunk on_err prefix chunk of
(text, prefix') -> yield text >> go prefix'
+-- | Convert chunked text into a utf-8 encoded byte stream
encode :: MonadStream m => m Text ByteString r r
encode = pipe T.encodeUtf8
+-- | Split the chunked input stream into lines, and yield them individually.
lines :: MonadStream m => m Text Text r r
lines = C.splitOn '\n'
+
+-- | Yield individual characters of the chunked input stream.
+characters :: MonadStream m => m Text Char r r
+characters = C.elems
View
2  Control/Pipe/Zip.hs
@@ -71,7 +71,7 @@ p1 *+* p2 = (continue p1 *** continue p2) >+> both
continue p = do
r <- p >+> pipe Right
yield $ Left r
- discard
+ _ <- discard
return r
both = withDefer go
where
View
1  Data/Generator/Length.hs
@@ -2,7 +2,6 @@
module Data.Generator.Length where
import qualified Data.ByteString as B
-import qualified Data.Foldable as F
import Data.Generator
import Data.Semigroup.Reducer
import qualified Data.Text as T
View
2  pipes-extra.cabal
@@ -27,7 +27,7 @@ Library
pipes-core (== 0.2.*),
bytestring (== 0.9.*),
text (== 0.11.*),
- reducers (== 3.0.*)
+ reducers (== 0.3.*)
Exposed-Modules:
Control.Pipe.Binary,
Control.Pipe.Chunked,
Please sign in to comment.
Something went wrong with that request. Please try again.