on master help sign in
Move the interop section from Tutorial.hs to streamly-examples repo
580a842Ranjeet Kumar Ranjan
src/Streamly/Tutorial.hs−106
@@ -1419,112 +1419,6 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
1419-- for serial operations even though it needs to deal with the concurrency
1419-- for serial operations even though it needs to deal with the concurrency
1420-- capability.
1420-- capability.
1421
1421
1422−-- $interop
1423−--
1424−-- We can use @unfoldr@ and @uncons@ to convert one streaming type to another.
1425−--
1426−-- Interop with @vector@:
1427−--
1428−-- @
1429−-- import "Streamly.Prelude" (SerialT)
1430−-- import qualified "Streamly.Prelude" as Stream
1431−-- import qualified Data.Vector.Fusion.Stream.Monadic as Vector
1432−--
1433−-- -- | vector to streamly
1434−-- fromVector :: (Stream.IsStream t, Monad m) => Vector.Stream m a -> t m a
1435−-- fromVector = Stream.unfoldrM unconsV
1436−-- where
1437−-- unconsV v = do
1438−-- r <- Vector.null v
1439−-- if r
1440−-- then return Nothing
1441−-- else do
1442−-- h <- Vector.head v
1443−-- return $ Just (h, Vector.tail v)
1444−--
1445−-- -- | streamly to vector
1446−-- toVector :: Monad m => SerialT m a -> Vector.Stream m a
1447−-- toVector = Vector.unfoldrM (Stream.uncons . Stream.adapt)
1448−--
1449−-- main = do
1450−-- Stream.toList (fromVector (Vector.fromList [1..3])) >>= print
1451−-- Vector.toList (toVector (Stream.fromFoldable [1..3])) >>= print
1452−-- @
1453−--
1454−-- Interop with @pipes@:
1455−--
1456−-- @
1457−-- import "Streamly.Prelude" (IsStream, MonadAsync, SerialT)
1458−-- import qualified "Streamly.Prelude" as Stream
1459−-- import qualified Pipes as Pipe
1460−-- import qualified Pipes.Prelude as Pipe
1461−--
1462−-- -- | pipes to streamly
1463−-- fromPipes :: (IsStream t, MonadAsync m) => Pipe.Producer a m () -> t m a
1464−-- fromPipes = Stream.'unfoldrM' unconsP
1465−-- where
1466−-- -- Adapt Pipe.next to return a Maybe instead of Either
1467−-- unconsP p = either (const Nothing) Just <$> Pipe.next p
1468−--
1469−-- -- | streamly to pipes
1470−-- toPipes :: Monad m => SerialT m a -> Pipe.Producer a m ()
1471−-- toPipes = Pipe.unfoldr unconsEither
1472−-- where
1473−-- -- Adapt Stream.uncons to return an Either instead of Maybe
1474−-- unconsEither :: Monad m => SerialT m a -> m (Either () (a, SerialT m a))
1475−-- unconsEither s = maybe (Left ()) Right <$> Stream.'uncons' s
1476−--
1477−-- main = do
1478−-- Stream.'toList' (fromPipes (Pipe.each [1..3])) >>= print
1479−-- Pipe.toListM (toPipes (Stream.'fromFoldable' [1..3])) >>= print
1480−-- @
1481−--
1482−-- Interop with @streaming@:
1483−--
1484−-- @
1485−-- import "Streamly.Prelude" (IsStream, MonadAsync, SerialT)
1486−-- import qualified "Streamly.Prelude" as Stream
1487−-- import qualified Streaming as Streaming
1488−-- import qualified Streaming.Prelude as Streaming
1489−--
1490−-- -- | streaming to streamly
1491−-- fromStreaming :: (IsStream t, MonadAsync m) => Streaming.Stream (Streaming.Of a) m () -> t m a
1492−-- fromStreaming = Stream.'unfoldrM' Streaming.uncons
1493−--
1494−-- -- | streamly to streaming
1495−-- toStreaming :: Monad m => SerialT m a -> Streaming.Stream (Streaming.Of a) m ()
1496−-- toStreaming = Streaming.unfoldr unconsEither
1497−-- where
1498−-- -- Adapt Stream.uncons to return an Either instead of Maybe
1499−-- unconsEither :: Monad m => SerialT m a -> m (Either () (a, SerialT m a))
1500−-- unconsEither s = maybe (Left ()) Right <$> Stream.'uncons' s
1501−--
1502−-- main = do
1503−-- Stream.'toList' (fromStreaming (Streaming.each [1..3])) >>= print
1504−-- Streaming.toList (toStreaming (Stream.'fromFoldable' [1..3])) >>= print
1505−-- @
1506−--
1507−-- Interop with @conduit@:
1508−--
1509−-- @
1510−-- import "Streamly.Prelude" (IsStream, MonadAsync, SerialT)
1511−-- import qualified "Streamly.Prelude" as Stream
1512−-- import qualified Data.Conduit as Conduit
1513−-- import qualified Data.Conduit.List as Conduit
1514−--
1515−-- -- | conduit to streamly
1516−-- fromConduit :: (IsStream t, MonadAsync m) => Conduit.ConduitT () a m () -> t m a
1517−-- fromConduit = Stream.unfoldrM Conduit.unconsM . Conduit.sealConduitT
1518−--
1519−-- -- | streamly to conduit
1520−-- toConduit :: Monad m => SerialT m a -> Conduit.ConduitT i a m ()
1521−-- toConduit = Conduit.unfoldM Stream.'uncons'
1522−--
1523−-- main = do
1524−-- Stream.'toList' (fromConduit (Conduit.sourceList [1..3])) >>= print
1525−-- Conduit.runConduit (toConduit (Stream.'fromFoldable' [1..3]) Conduit..| Conduit.consume) >>= print
1526−-- @
1527−
1528-- $comparison
1422-- $comparison
1529--
1423--
1530-- List transformers and logic programming monads also provide a product style
1424-- List transformers and logic programming monads also provide a product style