@@ -20,7 +20,7 @@ module Data.Primitive.Array (
2020 Array (.. ), MutableArray (.. ),
2121
2222 newArray , readArray , writeArray , indexArray , indexArrayM ,
23- freezeArray , thawArray , runArray , runArrays , runArraysHetOf ,
23+ freezeArray , thawArray , runArray , runArrays , runArraysHetOf , runArraysHetOfThen ,
2424 unsafeFreezeArray , unsafeThawArray , sameMutableArray ,
2525 copyArray , copyMutableArray ,
2626 cloneArray , cloneMutableArray ,
@@ -29,6 +29,7 @@ module Data.Primitive.Array (
2929 unsafeTraverseArray
3030) where
3131
32+ import Data.Functor.Compose
3233import Control.Monad.Primitive
3334
3435import GHC.Base ( Int (.. ) )
@@ -850,7 +851,24 @@ runArrays m = runST $ m >>= traverse unsafeFreezeArray
850851-- @
851852runArraysHetOf
852853 :: (forall h f g .
853- (Applicative h => (forall x . f x -> h (g x )) -> t f -> h (u g ))) -- ^ A rank-2 traversal
854- -> (forall s . ST s (t (MutableArray s ))) -- ^ An 'ST' action producing a rank-2 container of 'MutableArray's.
854+ (Applicative h => (forall x . f x -> h (g x )) -> t f -> h (u g )))
855+ -- ^ A rank-2 traversal
856+ -> (forall s . ST s (t (MutableArray s )))
857+ -- ^ An 'ST' action producing a rank-2 container of 'MutableArray's.
855858 -> u Array
856- runArraysHetOf f m = runST $ m >>= f unsafeFreezeArray
859+ runArraysHetOf trav m = runST $ m >>= trav unsafeFreezeArray
860+
861+ -- | Similar to 'runArraysHetOf', but takes a function to traverse over the
862+ -- generated 'Array's as it freezes them.
863+ runArraysHetOfThen
864+ :: Applicative q
865+ => (forall h f g .
866+ (Applicative h => (forall x . f x -> h (g x )) -> t f -> h (u g )))
867+ -- ^ A rank-2 traversal
868+ -> (forall x . Array x -> q (r x ))
869+ -- ^ A function to traverse over the container of 'Array's
870+ -> (forall s . ST s (t (MutableArray s )))
871+ -- ^ An 'ST' action producing a rank-2 container of 'MutableArray's.
872+ -> q (u r )
873+ runArraysHetOfThen trav post m =
874+ runST $ m >>= getCompose . trav (Compose . fmap post . unsafeFreezeArray)
0 commit comments