Permalink
Browse files

Add unfoldEither and unfoldEitherM functions

This provides the ability to produce a Producer-like Conduit that has
a return value.
  • Loading branch information...
ivan-m committed Jun 9, 2017
1 parent 4c09139 commit fd80de5162f91e362e0ef5e6ceefbc06a4c11e75
Showing with 72 additions and 1 deletion.
  1. +4 −0 conduit/ChangeLog.md
  2. +26 −0 conduit/Data/Conduit/Internal/List/Stream.hs
  3. +41 −0 conduit/Data/Conduit/List.hs
  4. +1 −1 conduit/conduit.cabal
View
@@ -1,3 +1,7 @@
## 1.2.11
* Add `unfoldEither` and `unfoldEitherM` to `Data.Conduit.List`
## 1.2.10
* Add `PrimMonad` instances for `ConduitM` and `Pipe`
@@ -23,6 +23,19 @@ unfoldS f s0 _ =
Just (x, s') -> Emit s' x
{-# INLINE unfoldS #-}
unfoldEitherS :: Monad m
=> (b -> Either r (a, b))
-> b
-> StreamConduitM i a m r
unfoldEitherS f s0 _ =
Stream step (return s0)
where
step s = return $
case f s of
Left r -> Stop r
Right (x, s') -> Emit s' x
{-# INLINE unfoldEitherS #-}
unfoldMS :: Monad m
=> (b -> m (Maybe (a, b)))
-> b
@@ -37,6 +50,19 @@ unfoldMS f s0 _ =
Just (x, s') -> Emit s' x
{-# INLINE unfoldMS #-}
unfoldEitherMS :: Monad m
=> (b -> m (Either r (a, b)))
-> b
-> StreamConduitM i a m r
unfoldEitherMS f s0 _ =
Stream step (return s0)
where
step s = do
ms' <- f s
return $ case ms' of
Left r -> Stop r
Right (x, s') -> Emit s' x
{-# INLINE unfoldEitherMS #-}
sourceListS :: Monad m => [a] -> StreamProducer m a
sourceListS xs0 _ =
Stream (return . step) (return xs0)
@@ -19,7 +19,9 @@ module Data.Conduit.List
sourceList
, sourceNull
, unfold
, unfoldEither
, unfoldM
, unfoldEitherM
, enumFromTo
, iterate
, replicate
@@ -73,6 +75,7 @@ import qualified Prelude
import Prelude
( ($), return, (==), (-), Int
, (.), id, Maybe (..), Monad
, Either (..)
, Bool (..)
, (>>)
, (>>=)
@@ -114,6 +117,25 @@ unfoldC f =
{-# INLINE unfoldC #-}
STREAMING(unfold, unfoldC, unfoldS, f x)
-- | Generate a source from a seed value with a return value.
--
-- Subject to fusion
--
-- @since 1.2.11
unfoldEither, unfoldEitherC :: Monad m
=> (b -> Either r (a, b))
-> b
-> ConduitM i a m r
unfoldEitherC f =
go
where
go seed =
case f seed of
Right (a, seed') -> yield a >> go seed'
Left r -> return r
{-# INLINE unfoldEitherC #-}
STREAMING(unfoldEither, unfoldEitherC, unfoldEitherS, f x)
-- | A monadic unfold.
--
-- Subject to fusion
@@ -133,6 +155,25 @@ unfoldMC f =
Nothing -> return ()
STREAMING(unfoldM, unfoldMC, unfoldMS, f seed)
-- | A monadic unfoldEither.
--
-- Subject to fusion
--
-- @since 1.2.11
unfoldEitherM, unfoldEitherMC :: Monad m
=> (b -> m (Either r (a, b)))
-> b
-> ConduitM i a m r
unfoldEitherMC f =
go
where
go seed = do
mres <- lift $ f seed
case mres of
Right (a, seed') -> yield a >> go seed'
Left r -> return r
STREAMING(unfoldEitherM, unfoldEitherMC, unfoldEitherMS, f seed)
-- | Yield the values from the list.
--
-- Subject to fusion
View
@@ -1,5 +1,5 @@
Name: conduit
Version: 1.2.10
Version: 1.2.11
Synopsis: Streaming data processing library.
description:
`conduit` is a solution to the streaming data problem, allowing for production,

0 comments on commit fd80de5

Please sign in to comment.