mirror of
https://github.com/ilyakooo0/streamly.git
synced 2024-08-15 11:20:22 +03:00
Move the interop section from Tutorial.hs to streamly-examples repo
This commit is contained in:
parent
d598832906
commit
580a842ae4
@ -1419,112 +1419,6 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
|
||||
-- for serial operations even though it needs to deal with the concurrency
|
||||
-- capability.
|
||||
|
||||
-- $interop
|
||||
--
|
||||
-- We can use @unfoldr@ and @uncons@ to convert one streaming type to another.
|
||||
--
|
||||
-- Interop with @vector@:
|
||||
--
|
||||
-- @
|
||||
-- import "Streamly.Prelude" (SerialT)
|
||||
-- import qualified "Streamly.Prelude" as Stream
|
||||
-- import qualified Data.Vector.Fusion.Stream.Monadic as Vector
|
||||
--
|
||||
-- -- | vector to streamly
|
||||
-- fromVector :: (Stream.IsStream t, Monad m) => Vector.Stream m a -> t m a
|
||||
-- fromVector = Stream.unfoldrM unconsV
|
||||
-- where
|
||||
-- unconsV v = do
|
||||
-- r <- Vector.null v
|
||||
-- if r
|
||||
-- then return Nothing
|
||||
-- else do
|
||||
-- h <- Vector.head v
|
||||
-- return $ Just (h, Vector.tail v)
|
||||
--
|
||||
-- -- | streamly to vector
|
||||
-- toVector :: Monad m => SerialT m a -> Vector.Stream m a
|
||||
-- toVector = Vector.unfoldrM (Stream.uncons . Stream.adapt)
|
||||
--
|
||||
-- main = do
|
||||
-- Stream.toList (fromVector (Vector.fromList [1..3])) >>= print
|
||||
-- Vector.toList (toVector (Stream.fromFoldable [1..3])) >>= print
|
||||
-- @
|
||||
--
|
||||
-- Interop with @pipes@:
|
||||
--
|
||||
-- @
|
||||
-- import "Streamly.Prelude" (IsStream, MonadAsync, SerialT)
|
||||
-- import qualified "Streamly.Prelude" as Stream
|
||||
-- import qualified Pipes as Pipe
|
||||
-- import qualified Pipes.Prelude as Pipe
|
||||
--
|
||||
-- -- | pipes to streamly
|
||||
-- fromPipes :: (IsStream t, MonadAsync m) => Pipe.Producer a m () -> t m a
|
||||
-- fromPipes = Stream.'unfoldrM' unconsP
|
||||
-- where
|
||||
-- -- Adapt Pipe.next to return a Maybe instead of Either
|
||||
-- unconsP p = either (const Nothing) Just <$> Pipe.next p
|
||||
--
|
||||
-- -- | streamly to pipes
|
||||
-- toPipes :: Monad m => SerialT m a -> Pipe.Producer a m ()
|
||||
-- toPipes = Pipe.unfoldr unconsEither
|
||||
-- where
|
||||
-- -- Adapt Stream.uncons to return an Either instead of Maybe
|
||||
-- unconsEither :: Monad m => SerialT m a -> m (Either () (a, SerialT m a))
|
||||
-- unconsEither s = maybe (Left ()) Right <$> Stream.'uncons' s
|
||||
--
|
||||
-- main = do
|
||||
-- Stream.'toList' (fromPipes (Pipe.each [1..3])) >>= print
|
||||
-- Pipe.toListM (toPipes (Stream.'fromFoldable' [1..3])) >>= print
|
||||
-- @
|
||||
--
|
||||
-- Interop with @streaming@:
|
||||
--
|
||||
-- @
|
||||
-- import "Streamly.Prelude" (IsStream, MonadAsync, SerialT)
|
||||
-- import qualified "Streamly.Prelude" as Stream
|
||||
-- import qualified Streaming as Streaming
|
||||
-- import qualified Streaming.Prelude as Streaming
|
||||
--
|
||||
-- -- | streaming to streamly
|
||||
-- fromStreaming :: (IsStream t, MonadAsync m) => Streaming.Stream (Streaming.Of a) m () -> t m a
|
||||
-- fromStreaming = Stream.'unfoldrM' Streaming.uncons
|
||||
--
|
||||
-- -- | streamly to streaming
|
||||
-- toStreaming :: Monad m => SerialT m a -> Streaming.Stream (Streaming.Of a) m ()
|
||||
-- toStreaming = Streaming.unfoldr unconsEither
|
||||
-- where
|
||||
-- -- Adapt Stream.uncons to return an Either instead of Maybe
|
||||
-- unconsEither :: Monad m => SerialT m a -> m (Either () (a, SerialT m a))
|
||||
-- unconsEither s = maybe (Left ()) Right <$> Stream.'uncons' s
|
||||
--
|
||||
-- main = do
|
||||
-- Stream.'toList' (fromStreaming (Streaming.each [1..3])) >>= print
|
||||
-- Streaming.toList (toStreaming (Stream.'fromFoldable' [1..3])) >>= print
|
||||
-- @
|
||||
--
|
||||
-- Interop with @conduit@:
|
||||
--
|
||||
-- @
|
||||
-- import "Streamly.Prelude" (IsStream, MonadAsync, SerialT)
|
||||
-- import qualified "Streamly.Prelude" as Stream
|
||||
-- import qualified Data.Conduit as Conduit
|
||||
-- import qualified Data.Conduit.List as Conduit
|
||||
--
|
||||
-- -- | conduit to streamly
|
||||
-- fromConduit :: (IsStream t, MonadAsync m) => Conduit.ConduitT () a m () -> t m a
|
||||
-- fromConduit = Stream.unfoldrM Conduit.unconsM . Conduit.sealConduitT
|
||||
--
|
||||
-- -- | streamly to conduit
|
||||
-- toConduit :: Monad m => SerialT m a -> Conduit.ConduitT i a m ()
|
||||
-- toConduit = Conduit.unfoldM Stream.'uncons'
|
||||
--
|
||||
-- main = do
|
||||
-- Stream.'toList' (fromConduit (Conduit.sourceList [1..3])) >>= print
|
||||
-- Conduit.runConduit (toConduit (Stream.'fromFoldable' [1..3]) Conduit..| Conduit.consume) >>= print
|
||||
-- @
|
||||
|
||||
-- $comparison
|
||||
--
|
||||
-- List transformers and logic programming monads also provide a product style
|
||||
|
Loading…
Reference in New Issue
Block a user