- streaming pipes. , , . conduit , pipes conduit , streaming; , . streaming ; IO [a] , , . , a Stream (Of Integer) IO () Integer, , -.
req , , , , .
import Streaming
import qualified Streaming.Prelude as S
import Streaming.Prelude (for, each)
req :: Integer -> Stream (Of Integer) IO ()
req x = do -- this 'stream' is just a list of Integers arising in IO
liftIO $ putStr "Sending request #" >> print x
each [x..x+2]
req' :: Stream (Of Integer) IO ()
req' = for (S.each [1..]) req -- An infinite succession of requests
-- each yielding three numbers. Here we are not
-- actually using IO to get each but we could.
main = S.print $ S.take 4 req'
-- >>> main
-- Sending request #1
-- 1
-- 2
-- 3
-- Sending request #2
-- 2
, ""; , , req ! S.take req', ; . . Stream (Of Int) IO ()
type List a = Stream (Of a) IO ()
Haskell, , , . , API Data.List, , , IO . ( , , splitAt, partition chunksOf, , , , , conduit.)
pipes
import Pipes
import qualified Pipes.Prelude as P
req :: Integer -> Producer Integer IO ()
req x = do
liftIO $ putStr "Sending request #" >> print x
each [x..x+2]
req' = for (each [1..]) req
main = runEffect $ req' >-> P.take 4 >-> P.print
-- >>> main
-- Sending request
-- 1
-- 2
-- 3
-- Sending request
-- 2
, take print , , Data.List. , , . take ing print ing - , , , , - ( - , , - >-> .|, , map.)
, , req
req x = do
liftIO $ putStr "Sending request #" >> print x
yield x -- yield a >> yield b == each [a,b]
yield (x+1)
yield (x+2)
streaming pipes conduit. yield a >> rest a:rest. , yield a ( do) IO, . a <- liftIO readLn; yield a
mapM replicateM traverse sequence - - , . sequence , , . ( sequence = mapM id; mapM f = sequence . map f) , ,
>>> sequence [getChar,getChar,getChar] >>= mapM_ print
abc'a' -- here and below I just type abc, ghci prints 'a' 'b' 'c'
'b'
'c'
,
>>> S.mapM_ print $ S.sequence $ S.each [getChar,getChar,getChar]
a'a'
b'b'
c'c'
>>> replicateM 3 getChar >>= mapM_ print
abc'a'
'b'
'c'
- - , , Char .
>>> S.mapM_ print $ S.replicateM 3 getChar
a'a'
b'b'
c'c'
. , . replicateM_, mapM_ sequence_ , . , , . - sequence , ,
>>> sequence [Just 1, Just 2, Just 3]
Just [1,2,3]
>>> sequence [Just 1, Just 2, Nothing]
Nothing
Maybe Int, , , Nothing. sequence, mapM, replicateM, traverse , Maybe IO.
, , , :
main = S.toList_ (S.take 4 req') >>= print
-- >>> main
-- Sending request #1
-- Sending request #2
-- [1,2,3,2]
, pipes:
main = P.toListM (req' >-> P.take 4) >>= print
-- >>> main
-- Sending request #1
-- Sending request #2
-- [1,2,3,2]
, , IO ,
main = do
ls <- S.toList_ $ S.print $ S.copy $ S.take 4 req'
print ls
-- >>> main
-- Sending request #1
-- 1
-- 2
-- 3
-- Sending request #2
-- 2
-- [1,2,3,2]
"" . , , pipes conduit, .