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. For example, I want to accumulate c
in a Map
(essentially to fold the stream) and output b
to stderr
.
How can I achieve this with io-streams
? It seems I cannot call connect
twice. So I have to put it before the partitioning, so the partitioning will operate on OutputStream
in a "contravariant" way:
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
:: 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))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.