我有一个 long 大文件流,其内容我想在常量内存中流式传输。
我使用Haskell Pipes library使用以下嵌套类型为此流建模:
Producer (FilePath, Producer ByteString IO ()) IO ()
换句话说,外部生成器遍历文件,而内部生成器遍历文件块。
出于会计目的,我想暂时将此嵌套流展平为具有以下类型的内容:
Producer FileStreamEvent IO ()
其中:
type FileStreamEvent = NestedStreamEvent FilePath ByteString
data NestedStreamEvent a b = NestedStreamOpen a
| NestedStreamChunk b
| NestedStreamClose a
编写一个函数来执行此转换似乎很容易:
flattenNestedStream :: Monad m
=> Producer (a, Producer b m r) m r
-> Producer (NestedStreamEvent a b) m r
flattenNestedStream abs = for abs $ \(a, bs) -> do
yield $ NestedStreamOpen a
for bs $ \b -> yield $ NestedStreamChunk b
yield $ NestedStreamClose a
但是,我不确定如何编写逆变换:
nestFlattenedStream :: Monad m
=> Producer (NestedStreamEvent a b) m r
-> Producer (a, Producer b m r) m r
这样:
(nestFlattenedStream . flattenNestedStream) producer == producer
可以定义函数nestFlattenedStream
吗?