<div dir="ltr">I have a streaming package question.<div><br></div><div>Suppose I have a:</div><div><font face="monospace">Stream (Of (Int, a)) m ()</font></div><div><br></div><div>I would like to demultiplex it into <font face="monospace">Vector (Stream (Of a) m ())</font> using the <font face="monospace">Int</font> as the index of an item into the <font face="monospace">Vector</font> of output streams.</div><div><br></div><div>How can I do this efficiently (constant memory and linear time)?</div><div><br></div><div>Does the following work?</div><div><div style="color:rgb(147,161,161);background-color:rgb(0,24,30);font-family:"Droid Sans Mono",monospace,monospace,"Droid Sans Fallback";font-size:14px;line-height:19px;white-space:pre"><div><div style="line-height:19px"><div><div style="line-height:19px"><div><span style="color:rgb(133,153,0)">import</span> <span style="color:rgb(133,153,0)">qualified</span>         Streaming.Prelude            <span style="color:rgb(133,153,0)">as</span> SP</div><div><span style="color:rgb(133,153,0)"></span></div></div></div><div><span style="color:rgb(133,153,0)">import</span> <span style="color:rgb(133,153,0)">qualified</span>         Data.Vector                  <span style="color:rgb(133,153,0)">as</span> V</div><div><span style="color:rgb(133,153,0)"></span></div></div></div><div><span style="color:rgb(133,153,0)"><br></span></div><div><span style="color:rgb(133,153,0)">type</span> <span style="font-weight:bold">StreamOf</span> <span style="color:rgb(38,139,210)">a</span> <span style="color:rgb(38,139,210)">m</span> <span style="color:rgb(38,139,210)">r</span> <span style="color:rgb(133,153,0)">=</span> <span style="font-weight:bold">Stream</span> (<span style="font-weight:bold">Of</span> <span style="color:rgb(38,139,210)">a</span>) <span style="color:rgb(38,139,210)">m</span> <span style="color:rgb(38,139,210)">r</span></div><br><div><span style="color:rgb(38,139,210)">demuxStream</span> <span style="color:rgb(133,153,0)">::</span> <span style="color:rgb(133,153,0)">forall</span> <span style="color:rgb(38,139,210)">a</span> <span style="color:rgb(38,139,210)">m</span>. <span style="font-weight:bold">MonadIO</span> <span style="color:rgb(38,139,210)">m</span></div><div>            <span style="color:rgb(133,153,0)">=></span> <span style="font-weight:bold">Int</span> <span style="color:rgb(133,153,0)">-></span> <span style="font-weight:bold">StreamOf</span> (<span style="font-weight:bold">Int</span>, <span style="color:rgb(38,139,210)">a</span>) <span style="color:rgb(38,139,210)">m</span> () <span style="color:rgb(133,153,0)">-></span> <span style="color:rgb(38,139,210)">m</span> (<span style="font-weight:bold">Vector</span> (<span style="font-weight:bold">StreamOf</span> <span style="color:rgb(38,139,210)">a</span> <span style="color:rgb(38,139,210)">m</span> ()))</div><div>demuxStream numSyms stream <span style="color:rgb(133,153,0)">=</span></div><div>  <span style="color:rgb(133,153,0)">let</span> emptyStreams <span style="color:rgb(133,153,0)">=</span> V<span style="color:rgb(133,153,0)">.</span>replicate numSyms (pure <span style="color:rgb(181,137,0)">()</span>)</div><div>      processItem v (iD, x) <span style="color:rgb(133,153,0)">=</span> V<span style="color:rgb(133,153,0)">.</span>modify (<span style="color:rgb(133,153,0)">\</span>vm <span style="color:rgb(133,153,0)">-></span> VM<span style="color:rgb(133,153,0)">.</span>modify vm (<span style="color:rgb(133,153,0)">>></span> SP<span style="color:rgb(133,153,0)">.</span>yield x) iD) v</div><div>  <span style="color:rgb(133,153,0)">in</span> SP<span style="color:rgb(133,153,0)">.</span>fold_ processItem emptyStreams id stream</div></div></div><div><br></div><div>My guess is that it takes more than constant memory as it goes through the entire input stream before returning.</div><div><br></div><div>Josh</div></div>