[英]How to find the end of Pipe
在下面的代碼中我該怎么做
更改stdoutCharConsumer
以便在打印輸入流中的所有數據后打印換行符
實施mixin
和mixin'
沒有進入Pipes.Internal? 可能嗎? 我需要像生產者的next
功能一樣。
我使用Pipes 4.1.0
#!/usr/bin/env runhaskell
{-# OPTIONS_GHC -Wall #-}
import Pipes
digits, characters :: Monad m => Producer Char m ()
digits = each "0123456789"
characters = each "abcdefghijklmnopqrstuvwxyz"
interleave :: Monad m => Producer a m () -> Producer a m () -> Producer a m ()
interleave a b = do
n <- lift $ next a
case n of
Left () -> b
Right (x, a') -> do
yield x
interleave b a'
stdoutCharConsumer :: Consumer Char IO ()
stdoutCharConsumer = await >>= liftIO . putChar >> stdoutCharConsumer
-- first element of the mixin should go first
mixin :: Monad m => Producer b m () -> Pipe a b m ()
mixin = undefined
-- first element of the pipe should go first
mixin' :: Monad m => Producer b m () -> Pipe a b m ()
mixin' = undefined
main :: IO ()
main = do
-- this prints "a0b1c2d3e4f5g6h7i8j9klmnopqrstuvwxyz"
runEffect $ interleave characters digits >-> stdoutCharConsumer
putStrLn ""
-- this prints "0a1b2c3d4e5f6g7h8i9jklmnopqrstuvwxyz"
runEffect $ interleave digits characters >-> stdoutCharConsumer
putStrLn ""
-- should print "0a1b2c3d4e5f6g7h8i9jklmnopqrstuvwxyz"
runEffect $ characters >-> mixin digits >-> stdoutCharConsumer
putStrLn ""
-- should print "a1b2c3d4e5f6g7h8i9jklmnopqrstuvwxyz"
runEffect $ digits >-> mixin characters >-> stdoutCharConsumer
putStrLn ""
-- should print "a1b2c3d4e5f6g7h8i9jklmnopqrstuvwxyz"
runEffect $ characters >-> mixin' digits >-> stdoutCharConsumer
putStrLn ""
-- should print "0a1b2c3d4e5f6g7h8i9jklmnopqrstuvwxyz"
runEffect $ digits >-> mixin' characters >-> stdoutCharConsumer
putStrLn ""
UPD:現在我讀到基於拉/推的流后,我認為即使使用Pipes.Internal也是不可能的。 這是真的嗎?
Consumers
和Pipes
都不知道上游輸入結束。 你需要Parser
來自pipes-parse
。
與Consumer
相比, Parser
對Producer
更直接的了解; 它們的draw
函數(大致類似於await
)在找到輸入結束時返回Nothing
。
import qualified Pipes.Parse as P
stdoutCharParser :: P.Parser Char IO ()
stdoutCharParser = P.draw >>= \ma ->
case ma of
Nothing -> liftIO (putStrLn "\n")
Just c -> liftIO (putChar c) >> stdoutCharParser
要運行解析器,我們調用evalStateT
而不是runEffect
:
P.evalStateT stdoutCharParser (interleave characters digits)
至於mixin
和mixin'
,我懷疑他們不可能按照預期寫作。 原因是Pipe
必須知道上游終止,以便知道何時產生作為參數傳遞的Producer
的剩余值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.