primitive icon indicating copy to clipboard operation
primitive copied to clipboard

Offer an extremely general function for building immutable arrays using mutation

Open treeowl opened this issue 7 years ago • 0 comments

This may look a bit strange....

create :: (forall h f g.
            Applicative h => (forall x. f x -> h (g x)) -> t f -> h (t g))
       -> (forall s. ST s (t (MutableArray s)))
       -> t Array
create f m = runST $ m >>= f unsafeFreezeArray

Using something like Vinyl, users can use this function to use a single ST action to safely produce multiple arrays, of multiple types, along with arbitrary additional context. It's much easier to understand with the Traversable class from rank2classes:

runArraysHet :: Rank2.Traversable t
             => (forall s. ST s (t (MutableArray s)))
             -> t Array
runArraysHet m = runST $ m >>= Rank2.traverse unsafeFreezeArray

but primitive obviously has no business depending on rank2classes.

treeowl avatar Mar 23 '18 05:03 treeowl