Skip to content
Browse files

Add await/withAwait via EitherT

  • Loading branch information...
1 parent 91f1fbd commit 142668221de84cd9a712819c4a1f11e129a06da0 @cdsmith committed Jun 9, 2012
Showing with 23 additions and 0 deletions.
  1. +20 −0 Pipes.hs
  2. +3 −0 README.md
View
20 Pipes.hs
@@ -15,6 +15,8 @@ module Pipes (
runPipe,
PipeC(..),
FinalC(..),
+ await,
+ withAwait,
forP,
mapP,
concatMapP,
@@ -41,6 +43,7 @@ import Control.Exception
import Control.Monad
import Control.Monad.State
import Control.Monad.Trans
+import Control.Monad.Trans.Either
import Data.Monoid
import Data.Typeable
import Data.Void
@@ -140,6 +143,23 @@ instance Monad m => Category (FinalC a m) where
id = FinalC idP
(FinalC p) . (FinalC q) = FinalC (q >+> p)
+instance MonadStream m => MonadStream (EitherT e m) where
+ type Upstream (EitherT e m) = Upstream m
+ type Downstream (EitherT e m) = Downstream m
+ type StreamResult (EitherT e m) = StreamResult m
+
+ yield = lift . yield
+ tryAwait = lift tryAwait
+
+instance MonadUnStream m => MonadUnStream (EitherT e m) where
+ unawait = lift . unawait
+
+await :: MonadStream m => EitherT (StreamResult m) m (Upstream m)
+await = tryAwait >>= either left return
+
+withAwait :: MonadStream m => EitherT (StreamResult m) m (StreamResult m) -> m (StreamResult m)
+withAwait = liftM (either id id) . runEitherT
+
forP :: MonadStream m => (Upstream m -> m r) -> m (StreamResult m)
forP f = tryAwait >>= either return ((>> forP f) . f)
View
3 README.md
@@ -17,6 +17,9 @@ The primitive stream operations are:
- `tryAwait` waits for upstream `yield` or termination
- `yield` yields a value to downstream
+The `await` operation is provided via `EitherT`, and a corresponding `withAwait` to
+unwrap the `EitherT` at the top level.
+
Exceptions and finalization are to be decided.
Note that `simulatePipe` is the fundamental abstraction for running a `Pipe`. The more

0 comments on commit 1426682

Please sign in to comment.
Something went wrong with that request. Please try again.