Skip to content

Commit 26ce85b

Browse files
committed
Add more bells and whistles
1 parent d01fdd2 commit 26ce85b

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

Data/Primitive/Array.hs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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
3233
import Control.Monad.Primitive
3334

3435
import GHC.Base ( Int(..) )
@@ -850,7 +851,24 @@ runArrays m = runST $ m >>= traverse unsafeFreezeArray
850851
-- @
851852
runArraysHetOf
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)

Data/Primitive/SmallArray.hs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ module Data.Primitive.SmallArray
5757
, runSmallArray
5858
, runSmallArrays
5959
, runSmallArraysHetOf
60+
, runSmallArraysHetOfThen
6061
, unsafeThawSmallArray
6162
, sizeofSmallArray
6263
, sizeofSmallMutableArray
@@ -107,6 +108,7 @@ import qualified Data.Primitive.Array as Array
107108
#if MIN_VERSION_base(4,9,0) || MIN_VERSION_transformers(0,4,0)
108109
import Data.Functor.Classes (Eq1(..),Ord1(..),Show1(..),Read1(..))
109110
#endif
111+
import Data.Functor.Compose
110112

111113
#if HAVE_SMALL_ARRAY
112114
data SmallArray a = SmallArray (SmallArray# a)
@@ -995,7 +997,24 @@ runSmallArrays m = runST $ m >>= traverse unsafeFreezeSmallArray
995997
-- @
996998
runSmallArraysHetOf
997999
:: (forall h f g.
998-
(Applicative h => (forall x. f x -> h (g x)) -> t f -> h (u g))) -- ^ A rank-2 traversal
999-
-> (forall s. ST s (t (SmallMutableArray s))) -- ^ An 'ST' action producing a rank-2 container of 'MutableArray's.
1000+
(Applicative h => (forall x. f x -> h (g x)) -> t f -> h (u g)))
1001+
-- ^ A rank-2 traversal
1002+
-> (forall s. ST s (t (SmallMutableArray s)))
1003+
-- ^ An 'ST' action producing a rank-2 container of 'MutableArray's.
10001004
-> u SmallArray
10011005
runSmallArraysHetOf f m = runST $ m >>= f unsafeFreezeSmallArray
1006+
1007+
-- | Similar to 'runSmallArraysHetOf', but takes a function to traverse over the
1008+
-- generated 'SmallArray's as it freezes them.
1009+
runSmallArraysHetOfThen
1010+
:: Applicative q
1011+
=> (forall h f g.
1012+
(Applicative h => (forall x. f x -> h (g x)) -> t f -> h (u g)))
1013+
-- ^ A rank-2 traversal
1014+
-> (forall x. SmallArray x -> q (r x))
1015+
-- ^ A function to traverse over a container of 'Array's
1016+
-> (forall s. ST s (t (SmallMutableArray s)))
1017+
-- ^ An 'ST' action producing a rank-2 container of 'MutableArray's.
1018+
-> q (u r)
1019+
runSmallArraysHetOfThen trav post m =
1020+
runST $ m >>= getCompose . trav (Compose . fmap post . unsafeFreezeSmallArray)

0 commit comments

Comments
 (0)