2016-03-20 30 views
0

我有下面的代碼:Haskell Pipes:如何對生產者的輸出進行排序?

import Control.Monad (unless) 
import Pipes 
import qualified Pipes.Prelude as P 
import System.FilePath.Posix ((</>)) 
import System.Posix.Directory (DirStream, openDirStream, readDirStream) 

produceFiles :: DirStream -> Producer FilePath IO() 
produceFiles ds = do 
    path <- lift $ readDirStream ds 
    yield path 
    unless (path == "") $ produceFiles ds 

getDC :: FilePath -> Producer FilePath IO() 
getDC top = do 
    ds <- lift $ openDirStream top 
    produceFiles ds 

runTest top = runEffect $ getDC top >-> P.map (top</>) >-> P.stdoutLn 

它打印目錄top的所有文件。在打印之前如何對輸出進行排序?我是否需要編寫一個消費者,將輸出「排走」到列表中,然後對其進行排序?我正在使用管道-4.1.4。

回答

3

toListM from Pipes.Prelude將生產者轉換爲列表。我們可以利用這一點,沒有pipes繼續算賬:

runTest top = do 
    ds <- P.toListM (getDC top >-> P.map (top</>)) 
    mapM_ print $ sort ds 

以上的比特管道狀的使用通常的單子運營商:

runTest top = P.toListM (getDC top >-> P.map (top</>)) >>= mapM_ print . sort 

抓住所有的Producer內容給我們帶來的外部流的抽象,這是爲什麼toListM返回一個普通的列表,而不是一個管道。

1

是的,您需要首先排出輸出,或者將其列入其他結構的列表中。排序本質上是非流式的,因爲它可能是,例如,進入的最後一個元素應該是第一個出去。

相關問題