<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>