<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">Not directly answering your question, but off the top of my head is: <div class=""><br class=""></div><div class=""> why not demux into a Vector (Sink (Of a) m ()) </div><div class=""><br class=""></div><div class="">Yet I have no idea if there is actually a Sink construct in any stream lib, but the theory is the Vector should contain reactive actions taking elements, rather than containers to collect elements (even thunks of them demand space).</div><div class=""><br class=""></div><div class="">Just my bit of thoughts, I'm definitely novice at it.</div><div class=""><br class=""></div><div class="">Cheers,</div><div class="">Compl</div><div class=""><br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On 2020-04-16, at 00:40, ☂Josh Chia (謝任中) <<a href="mailto:joshchia@gmail.com" class="">joshchia@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">I have a streaming package question.<div class=""><br class=""></div><div class="">Suppose I have a:</div><div class=""><font face="monospace" class="">Stream (Of (Int, a)) m ()</font></div><div class=""><br class=""></div><div class="">I would like to demultiplex it into <font face="monospace" class="">Vector (Stream (Of a) m ())</font> using the <font face="monospace" class="">Int</font> as the index of an item into the <font face="monospace" class="">Vector</font> of output streams.</div><div class=""><br class=""></div><div class="">How can I do this efficiently (constant memory and linear time)?</div><div class=""><br class=""></div><div class="">Does the following work?</div><div class=""><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" class=""><div class=""><div style="line-height:19px" class=""><div class=""><div style="line-height:19px" class=""><div class=""><span style="color:rgb(133,153,0)" class="">import</span> <span style="color:rgb(133,153,0)" class="">qualified</span> Streaming.Prelude <span style="color:rgb(133,153,0)" class="">as</span> SP</div><div class=""><span style="color:rgb(133,153,0)" class=""></span></div></div></div><div class=""><span style="color:rgb(133,153,0)" class="">import</span> <span style="color:rgb(133,153,0)" class="">qualified</span> Data.Vector <span style="color:rgb(133,153,0)" class="">as</span> V</div><div class=""><span style="color:rgb(133,153,0)" class=""></span></div></div></div><div class=""><span style="color:rgb(133,153,0)" class=""><br class=""></span></div><div class=""><span style="color:rgb(133,153,0)" class="">type</span> <span style="font-weight:bold" class="">StreamOf</span> <span style="color:rgb(38,139,210)" class="">a</span> <span style="color:rgb(38,139,210)" class="">m</span> <span style="color:rgb(38,139,210)" class="">r</span> <span style="color:rgb(133,153,0)" class="">=</span> <span style="font-weight:bold" class="">Stream</span> (<span style="font-weight:bold" class="">Of</span> <span style="color:rgb(38,139,210)" class="">a</span>) <span style="color:rgb(38,139,210)" class="">m</span> <span style="color:rgb(38,139,210)" class="">r</span></div><br class=""><div class=""><span style="color:rgb(38,139,210)" class="">demuxStream</span> <span style="color:rgb(133,153,0)" class="">::</span> <span style="color:rgb(133,153,0)" class="">forall</span> <span style="color:rgb(38,139,210)" class="">a</span> <span style="color:rgb(38,139,210)" class="">m</span>. <span style="font-weight:bold" class="">MonadIO</span> <span style="color:rgb(38,139,210)" class="">m</span></div><div class=""> <span style="color:rgb(133,153,0)" class="">=></span> <span style="font-weight:bold" class="">Int</span> <span style="color:rgb(133,153,0)" class="">-></span> <span style="font-weight:bold" class="">StreamOf</span> (<span style="font-weight:bold" class="">Int</span>, <span style="color:rgb(38,139,210)" class="">a</span>) <span style="color:rgb(38,139,210)" class="">m</span> () <span style="color:rgb(133,153,0)" class="">-></span> <span style="color:rgb(38,139,210)" class="">m</span> (<span style="font-weight:bold" class="">Vector</span> (<span style="font-weight:bold" class="">StreamOf</span> <span style="color:rgb(38,139,210)" class="">a</span> <span style="color:rgb(38,139,210)" class="">m</span> ()))</div><div class="">demuxStream numSyms stream <span style="color:rgb(133,153,0)" class="">=</span></div><div class=""> <span style="color:rgb(133,153,0)" class="">let</span> emptyStreams <span style="color:rgb(133,153,0)" class="">=</span> V<span style="color:rgb(133,153,0)" class="">.</span>replicate numSyms (pure <span style="color:rgb(181,137,0)" class="">()</span>)</div><div class=""> processItem v (iD, x) <span style="color:rgb(133,153,0)" class="">=</span> V<span style="color:rgb(133,153,0)" class="">.</span>modify (<span style="color:rgb(133,153,0)" class="">\</span>vm <span style="color:rgb(133,153,0)" class="">-></span> VM<span style="color:rgb(133,153,0)" class="">.</span>modify vm (<span style="color:rgb(133,153,0)" class="">>></span> SP<span style="color:rgb(133,153,0)" class="">.</span>yield x) iD) v</div><div class=""> <span style="color:rgb(133,153,0)" class="">in</span> SP<span style="color:rgb(133,153,0)" class="">.</span>fold_ processItem emptyStreams id stream</div></div></div><div class=""><br class=""></div><div class="">My guess is that it takes more than constant memory as it goes through the entire input stream before returning.</div><div class=""><br class=""></div><div class="">Josh</div></div>
_______________________________________________<br class="">Haskell-Cafe mailing list<br class="">To (un)subscribe, modify options or view archives go to:<br class=""><a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" class="">http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe</a><br class="">Only members subscribed via the mailman list are allowed to post.</div></blockquote></div><br class=""></div></body></html>