on master help sign in
update interop APIs to modular "to" and "from" style
45a9ea0Harendra Kumar
src/Streamly/Tutorial.hs+40−26
@@ -1324,13 +1324,9 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
1324-- import qualified Streamly.Prelude as S
1324-- import qualified Streamly.Prelude as S
1325-- import qualified Data.Vector.Fusion.Stream.Monadic as V
1325-- import qualified Data.Vector.Fusion.Stream.Monadic as V
1326--
1326--
1327−-- main = do
1327+-- -- | vector to streamly
1328−-- -- streamly to vector
1328+-- fromVector :: (IsStream t, Monad m) => V.Stream m a -> t m a
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
1329+-- fromVector = S.unfoldrM unconsV
1333−--
1334-- where
1330-- where
1335-- unconsV v = do
1331-- unconsV v = do
1336-- r <- V.null v
1332-- r <- V.null v
@@ -1339,6 +1335,14 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
1339-- else do
1335-- else do
1340-- h <- V.head v
1336-- h <- V.head v
1341-- return $ Just (h, V.tail v)
1337-- 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
1342-- @
1346-- @
1343--
1347--
1344-- Interop with @pipes@:
1348-- Interop with @pipes@:
@@ -1349,19 +1353,23 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
1349-- import qualified Pipes as P
1353-- import qualified Pipes as P
1350-- import qualified Pipes.Prelude as P
1354-- import qualified Pipes.Prelude as P
1351--
1355--
1352−-- main = do
1356+-- -- | pipes to streamly
1353−-- -- streamly to pipe
1357+-- fromPipes :: (IsStream t, Monad m) => P.Producer a m r -> t m a
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+-- fromPipes = S.'unfoldrM' unconsP
1358−--
1359-- where
1359-- where
1360-- -- Adapt P.next to return a Maybe instead of Either
1360-- -- Adapt P.next to return a Maybe instead of Either
1361-- unconsP p = P.next p >>= either (\\_ -> return Nothing) (return . Just)
1361-- unconsP p = P.next p >>= either (\\_ -> return Nothing) (return . Just)
1362--
1362--
1363+-- -- | streamly to pipes
1364+-- toPipes :: Monad m => SerialT m a -> P.Producer a m ()
1365+-- toPipes = P.unfoldr unconsS
1366+-- where
1363-- -- Adapt S.uncons to return an Either instead of Maybe
1367-- -- Adapt S.uncons to return an Either instead of Maybe
1364-- unconsS s = S.'uncons' s >>= maybe (return $ Left ()) (return . Right)
1368-- 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
1365-- @
1373-- @
1366--
1374--
1367-- Interop with @streaming@:
1375-- Interop with @streaming@:
@@ -1372,17 +1380,20 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
1372-- import qualified Streaming as SG
1380-- import qualified Streaming as SG
1373-- import qualified Streaming.Prelude as SG
1381-- import qualified Streaming.Prelude as SG
1374--
1382--
1375−-- main = do
1383+-- -- | streaming to streamly
1376−-- -- streamly to streaming
1384+-- fromStreaming :: (IsStream t, Monad m) => SG.Stream (SG.Of a) m r -> t m a
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
1385+-- fromStreaming = S.unfoldrM SG.uncons
1381--
1386--
1387+-- -- | streamly to streaming
1388+-- toStreaming :: Monad m => SerialT m a -> SG.Stream (SG.Of a) m ()
1389+-- toStreaming = SG.unfoldr unconsS
1382-- where
1390-- where
1383−--
1384-- -- Adapt S.uncons to return an Either instead of Maybe
1391-- -- Adapt S.uncons to return an Either instead of Maybe
1385-- unconsS s = S.'uncons' s >>= maybe (return $ Left ()) (return . Right)
1392-- 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
1386-- @
1397-- @
1387--
1398--
1388-- Interop with @conduit@:
1399-- Interop with @conduit@:
@@ -1394,12 +1405,15 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
1394-- import qualified Data.Conduit.List as C
1405-- import qualified Data.Conduit.List as C
1395-- import qualified Data.Conduit.Combinators as C
1406-- import qualified Data.Conduit.Combinators as C
1396--
1407--
1397−-- main = do
1408+-- -- It seems there is no way out of a conduit as it does not provide an
1398−-- -- streamly to conduit
1409+-- -- uncons or a tail function. We can convert streamly to conduit though.
1399−-- C.runConduit (C.unfoldM S.'uncons' (S.'fromFoldable' [1..3]) C..| C.sinkList) >>= print
1400--
1410--
1401−-- -- It seems there is no way out of a conduit as it does not provide an
1411+-- -- | streamly to conduit
1402−-- -- uncons or a tail function.
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
1403-- @
1417-- @
1404
1418
1405-- $comparison
1419-- $comparison