<div dir="auto"><div>I did some work on this sort of thing for primitive, which didn't want it. But maybe array does. If I don't link to it in the next day, please ping me.<br><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Aug 22, 2019, 1:25 AM Zemyla <<a href="mailto:zemyla@gmail.com">zemyla@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto">The "runSTArray" and "runSTUArray" functions allow efficiently working with Arrays in the ST monad before turning them immutable; however, they don't allow any way to return supplemental or alternative information with the array. There are many times when I've wanted to get an (Array i e, w) or a Maybe (UArray i e), but I couldn't, and had to use the far-more-inefficient freezeArray and hope it inlined properly.<div dir="auto"><br></div><div dir="auto">What I want are functions that generalize the return types given:</div><div dir="auto"><br></div><div dir="auto">runSTArrayTrav :: Traversable t => (forall s. ST s (t (STArray s i e))) -> t (Array i e)</div><div dir="auto">runSTArrayTrav m = runST $ m >>= traverse unsafeFreezeSTArray</div><div dir="auto"><br></div><div dir="auto">runSTUArrayTrav :: Traversable t => (forall s. ST s (t (STUArray s i e))) -> t (UArray i e)</div><div dir="auto">runSTUArrayTrav m = runST $ m >>= traverse unsafeFreezeSTUArray</div><div dir="auto"><br></div><div dir="auto">And then an even more generalized version, which takes a sort of Lens-like iterator, and allows returning multiple arrays of different kinds, types, and indices:</div><div dir="auto"><br></div><div dir="auto">runSTArrayWith :: (forall f s. Applicative f => (forall i e. STArray s i e -> f (Array i e)) -> (forall i e. STUArray s i e -> f (UArray i e)) -> u s -> f v) -> (forall s. ST s (u s)) -> v</div><div dir="auto">runSTArrayWith tr m = runST $ m >>= tr unsafeFreezeSTArray unsafeFreezeSTUArray</div><div dir="auto"><br></div><div dir="auto">The advantage of the runSTArrayTrav/runSTUArrayTrav functions, if they're subsets of the runSTArrayWith function, is that it works with standard things like (,) and Either, and doesn't require wrapping it in a newtype so that the s is at the end.</div><div dir="auto"><br></div><div dir="auto">The names of the functions are up for debate, and I know there will be one, because naming things is one of the two hard problems in computer science, along with cache invalidation and off-by-one errors.</div></div>
_______________________________________________<br>
Libraries mailing list<br>
<a href="mailto:Libraries@haskell.org" target="_blank" rel="noreferrer">Libraries@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries" rel="noreferrer noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries</a><br>
</blockquote></div></div></div>