On 2020-04-16, at 00:40, ☂Josh Chia (謝任中) <joshchia@gmail.com> wrote:I have a streaming package question._______________________________________________Suppose I have a:Stream (Of (Int, a)) m ()I would like to demultiplex it into Vector (Stream (Of a) m ()) using the Int as the index of an item into the Vector of output streams.How can I do this efficiently (constant memory and linear time)?Does the following work?import qualified Streaming.Prelude as SPimport qualified Data.Vector as Vtype StreamOf a m r = Stream (Of a) m rdemuxStream :: forall a m. MonadIO m=> Int -> StreamOf (Int, a) m () -> m (Vector (StreamOf a m ()))demuxStream numSyms stream =let emptyStreams = V.replicate numSyms (pure ())processItem v (iD, x) = V.modify (\vm -> VM.modify vm (>> SP.yield x) iD) vin SP.fold_ processItem emptyStreams id streamMy guess is that it takes more than constant memory as it goes through the entire input stream before returning.Josh
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.