on master help sign in
update interop APIs to modular "to" and "from" style
commit45a9ea010055f083f67ecddd8b841306f2cadcda authorHarendra Kumar <harendra.kumar@gmail.com> dateSat 19 May 2018, 05:39 (8 years ago) parentf849361
1 file changed+40−26
src/Streamly/Tutorial.hs+40−26
src/Streamly/Tutorial.hs+40−26
@@ -1324,13 +1324,9 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
13241324-- import qualified Streamly.Prelude as S
13251325-- import qualified Data.Vector.Fusion.Stream.Monadic as V
13261326--
1327−-- main = do
1328−-- -- streamly to vector
1329−-- V.toList (V.unfoldrM S.uncons (S.fromFoldable [1..3])) >>= print
1330−--
1331−-- -- vector to streamly
1332−-- S.toList (S.unfoldrM unconsV (V.fromList [1..3])) >>= print
1333−--
1327+-- -- | vector to streamly
1328+-- fromVector :: (IsStream t, Monad m) => V.Stream m a -> t m a
1329+-- fromVector = S.unfoldrM unconsV
13341330-- where
13351331-- unconsV v = do
13361332-- r <- V.null v
@@ -1339,6 +1335,14 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
13391335-- else do
13401336-- h <- V.head v
13411337-- return $ Just (h, V.tail v)
1338+--
1339+-- -- | streamly to vector
1340+-- toVector :: Monad m => SerialT m a -> V.Stream m a
1341+-- toVector = V.unfoldrM (S.uncons . adapt)
1342+--
1343+-- main = do
1344+-- S.toList (fromVector (V.fromList [1..3])) >>= print
1345+-- V.toList (toVector (S.fromFoldable [1..3])) >>= print
13421346-- @
13431347--
13441348-- Interop with @pipes@:
@@ -1349,19 +1353,23 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
13491353-- import qualified Pipes as P
13501354-- import qualified Pipes.Prelude as P
13511355--
1352−-- main = do
1353−-- -- streamly to pipe
1354−-- P.toListM (P.unfoldr unconsS (S.'fromFoldable' [1..3])) >>= print
1355−--
1356−-- -- pipe to streamly
1357−-- S.'toList' (S.'unfoldrM' unconsP (P.each [1..3])) >>= print
1358−--
1356+-- -- | pipes to streamly
1357+-- fromPipes :: (IsStream t, Monad m) => P.Producer a m r -> t m a
1358+-- fromPipes = S.'unfoldrM' unconsP
13591359-- where
13601360-- -- Adapt P.next to return a Maybe instead of Either
13611361-- unconsP p = P.next p >>= either (\\_ -> return Nothing) (return . Just)
13621362--
1363+-- -- | streamly to pipes
1364+-- toPipes :: Monad m => SerialT m a -> P.Producer a m ()
1365+-- toPipes = P.unfoldr unconsS
1366+-- where
13631367-- -- Adapt S.uncons to return an Either instead of Maybe
13641368-- unconsS s = S.'uncons' s >>= maybe (return $ Left ()) (return . Right)
1369+--
1370+-- main = do
1371+-- S.'toList' (fromPipes (P.each [1..3])) >>= print
1372+-- P.toListM (toPipes (S.'fromFoldable' [1..3])) >>= print
13651373-- @
13661374--
13671375-- Interop with @streaming@:
@@ -1372,17 +1380,20 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
13721380-- import qualified Streaming as SG
13731381-- import qualified Streaming.Prelude as SG
13741382--
1375−-- main = do
1376−-- -- streamly to streaming
1377−-- SG.toList (SG.unfoldr unconsS (S.'fromFoldable' [1..3])) >>= print
1378−--
1379−-- -- streaming to streamly
1380−-- S.'toList' (S.unfoldrM SG.uncons (SG.each [1..3])) >>= print
1383+-- -- | streaming to streamly
1384+-- fromStreaming :: (IsStream t, Monad m) => SG.Stream (SG.Of a) m r -> t m a
1385+-- fromStreaming = S.unfoldrM SG.uncons
13811386--
1387+-- -- | streamly to streaming
1388+-- toStreaming :: Monad m => SerialT m a -> SG.Stream (SG.Of a) m ()
1389+-- toStreaming = SG.unfoldr unconsS
13821390-- where
1383−--
13841391-- -- Adapt S.uncons to return an Either instead of Maybe
13851392-- unconsS s = S.'uncons' s >>= maybe (return $ Left ()) (return . Right)
1393+--
1394+-- main = do
1395+-- S.toList (fromStreaming (SG.each [1..3])) >>= print
1396+-- SG.toList (toStreaming (S.fromFoldable [1..3])) >>= print
13861397-- @
13871398--
13881399-- Interop with @conduit@:
@@ -1394,12 +1405,15 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
13941405-- import qualified Data.Conduit.List as C
13951406-- import qualified Data.Conduit.Combinators as C
13961407--
1397−-- main = do
1398−-- -- streamly to conduit
1399−-- C.runConduit (C.unfoldM S.'uncons' (S.'fromFoldable' [1..3]) C..| C.sinkList) >>= print
1408+-- -- It seems there is no way out of a conduit as it does not provide an
1409+-- -- uncons or a tail function. We can convert streamly to conduit though.
14001410--
1401−-- -- It seems there is no way out of a conduit as it does not provide an
1402−-- -- uncons or a tail function.
1411+-- -- | streamly to conduit
1412+-- toConduit :: Monad m => SerialT m a -> C.ConduitT i a m ()
1413+-- toConduit s = C.unfoldM S.'uncons' s
1414+--
1415+-- main = do
1416+-- C.runConduit (toConduit (S.'fromFoldable' [1..3]) C..| C.sinkList) >>= print
14031417-- @
14041418
14051419-- $comparison