[英]io-streams partitionEithers
I have a worker function 我有工人职能
worker :: a -> Either b c
and I want to apply it to a stream of a
and produce 2 streams of b
and c
and process those streams further. 我想将它应用到流a
和产生的2个流b
和c
,并进一步处理这些流。 For example, I want to accumulate c
in a Map
(essentially to fold the stream) and output b
to stderr
. 例如,我想将c
累积在Map
(本质上是折叠流),然后将b
输出到stderr
。
How can I achieve this with io-streams
? 如何使用io-streams
实现此目标? It seems I cannot call connect
twice. 看来我不能打电话给connect
两次。 So I have to put it before the partitioning, so the partitioning will operate on OutputStream
in a "contravariant" way: 因此,我必须将其放在分区之前,以便分区将以“相反”的方式在OutputStream
上运行:
contrapartitionEithers
:: OutputStream b -> OutputStream c -> IO (OutputStream (Either b c))
Is it implementable? 它可以实现吗? If no, how can do the task at hand? 如果没有,该怎么办? If yes, is is somehow "dual" to System.IO.Streams.zip
? 如果是,是不是对System.IO.Streams.zip
是“双重”?
System.IO.Streams.zip
:: InputStream a -> InputStream b -> IO (InputStream (a, b))
Sometimes the right question contains the answer. 有时正确的问题包含答案。 So it's possible indeed: 因此确实有可能:
contrapartitionEithers
:: OutputStream b -> OutputStream c -> IO (OutputStream (Either b c))
contrapartitionEithers b c = makeOutputStream $ maybe
(writeTo b Nothing >> writeTo c Nothing)
(either (writeTo b . Just) (writeTo c . Just))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.