@Michael 的回答很好。我只是想说明一些
正在发生的使用模式。
(.lhs 可在http://lpaste.net/165352 获得)
前几个导入:
{-# LANGUAGE OverloadedStrings, NoMonomorphismRestriction #-}
import Pipes
import qualified Pipes.Prelude as PP
import qualified Pipes.Group as PG
import qualified Pipes.ByteString as PB
import qualified Pipes.GZip as GZip
import qualified Data.ByteString as BS
import Lens.Family (view, over)
import Control.Monad
import System.IO
如果您查看 Pipes.ByteString 和 Pipes.GZip 中的函数
你会看到它们都变成了以下类型的模式:
- 制片人...-> FreeT(制片人...)...
- FreeT(生产者 ...)... -> 生产者 ...
- Lens'(制片人...)(FreeT(制片人...)...)
- 制片人...->制片人...
每个类别的功能示例:
PB.words
PG.concats
-
PB.lines, PB.chunksOf, PB.splits, ...
-
GZip.compress, GZip.decompress
以下是如何使用PB.words 将输入流拆分为单词:
prod = yield "this is\na test\nof the pipes\nprocessing\nsystem"
t1 = runEffect $ (PG.concats . PB.words) prod >-> PP.print
使用类型 3 的函数——例如PB.lines,只需使用view
Lens' 获取类型 1 的函数,然后与 PG.concats 组合:
t2a = runEffect $ (PG.concats . view PB.lines) prod >-> PP.print
t2b h = (PG.concats . view PB.lines) (PB.fromHandle h) >-> PP.print
run2 = withFile "input" ReadMode (runEffect . t2b)
对于 Producer -> Producer 函数,只需使用普通函数应用即可:
t3 h = GZip.decompress (PB.fromHandle h) >-> PP.print
run3 = withFile "input.gz" ReadMode (runEffect . t3)
t4 h = GZip.decompress (PB.fromHandle h) >-> PP.map BS.length >-> PP.print
run4 = withFile "big.gz" ReadMode (runEffect . t4)
要先解压再按行分割,我们嵌套函数
应用:
t5 h = (PG.concats . view PB.lines) ( GZip.decompress (PB.fromHandle h) )
>-> PP.map BS.length >-> PP.print
run5 = withFile "input.gz" ReadMode (runEffect . t5)