Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add await/withAwait via EitherT

  • Loading branch information...
commit 142668221de84cd9a712819c4a1f11e129a06da0 1 parent 91f1fbd
Chris Smith authored

Showing 2 changed files with 23 additions and 0 deletions. Show diff stats Hide diff stats

  1. +20 0 Pipes.hs
  2. +3 0  README.md
20 Pipes.hs
@@ -15,6 +15,8 @@ module Pipes (
15 15 runPipe,
16 16 PipeC(..),
17 17 FinalC(..),
  18 + await,
  19 + withAwait,
18 20 forP,
19 21 mapP,
20 22 concatMapP,
@@ -41,6 +43,7 @@ import Control.Exception
41 43 import Control.Monad
42 44 import Control.Monad.State
43 45 import Control.Monad.Trans
  46 +import Control.Monad.Trans.Either
44 47 import Data.Monoid
45 48 import Data.Typeable
46 49 import Data.Void
@@ -140,6 +143,23 @@ instance Monad m => Category (FinalC a m) where
140 143 id = FinalC idP
141 144 (FinalC p) . (FinalC q) = FinalC (q >+> p)
142 145
  146 +instance MonadStream m => MonadStream (EitherT e m) where
  147 + type Upstream (EitherT e m) = Upstream m
  148 + type Downstream (EitherT e m) = Downstream m
  149 + type StreamResult (EitherT e m) = StreamResult m
  150 +
  151 + yield = lift . yield
  152 + tryAwait = lift tryAwait
  153 +
  154 +instance MonadUnStream m => MonadUnStream (EitherT e m) where
  155 + unawait = lift . unawait
  156 +
  157 +await :: MonadStream m => EitherT (StreamResult m) m (Upstream m)
  158 +await = tryAwait >>= either left return
  159 +
  160 +withAwait :: MonadStream m => EitherT (StreamResult m) m (StreamResult m) -> m (StreamResult m)
  161 +withAwait = liftM (either id id) . runEitherT
  162 +
143 163 forP :: MonadStream m => (Upstream m -> m r) -> m (StreamResult m)
144 164 forP f = tryAwait >>= either return ((>> forP f) . f)
145 165
3  README.md
Source Rendered
@@ -17,6 +17,9 @@ The primitive stream operations are:
17 17 - `tryAwait` waits for upstream `yield` or termination
18 18 - `yield` yields a value to downstream
19 19
  20 +The `await` operation is provided via `EitherT`, and a corresponding `withAwait` to
  21 +unwrap the `EitherT` at the top level.
  22 +
20 23 Exceptions and finalization are to be decided.
21 24
22 25 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.