Skip to content

Commit c96a659

Browse files
committed
Add 'createT', allowing monadic initialisation of any Traversable container of vectors
1 parent e000d6c commit c96a659

File tree

6 files changed

+34
-13
lines changed

6 files changed

+34
-13
lines changed

Data/Vector.hs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ module Data.Vector (
5656
empty, singleton, replicate, generate, iterateN,
5757

5858
-- ** Monadic initialisation
59-
replicateM, generateM, create,
59+
replicateM, generateM, create, createT,
6060

6161
-- ** Unfolding
6262
unfoldr, unfoldrN,
@@ -687,6 +687,11 @@ create :: (forall s. ST s (MVector s a)) -> Vector a
687687
-- NOTE: eta-expanded due to http://hackage.haskell.org/trac/ghc/ticket/4120
688688
create p = G.create p
689689

690+
-- | Execute the monadic action and freeze the resulting vectors.
691+
createT :: Traversable f => (forall s. ST s (f (MVector s a))) -> f (Vector a)
692+
{-# INLINE createT #-}
693+
createT p = G.createT p
694+
690695

691696

692697
-- Restricting memory usage
@@ -1572,4 +1577,3 @@ unsafeCopy = G.unsafeCopy
15721577
copy :: PrimMonad m => MVector (PrimState m) a -> Vector a -> m ()
15731578
{-# INLINE copy #-}
15741579
copy = G.copy
1575-

Data/Vector/Generic.hs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ module Data.Vector.Generic (
3939
empty, singleton, replicate, generate, iterateN,
4040

4141
-- ** Monadic initialisation
42-
replicateM, generateM, create,
42+
replicateM, generateM, create, createT,
4343

4444
-- ** Unfolding
4545
unfoldr, unfoldrN,
@@ -709,6 +709,11 @@ create :: Vector v a => (forall s. ST s (Mutable v s a)) -> v a
709709
{-# INLINE create #-}
710710
create p = new (New.create p)
711711

712+
-- | Execute the monadic action and freeze the resulting vectors.
713+
createT :: (Traversable f, Vector v a) => (forall s. ST s (f (Mutable v s a))) -> f (v a)
714+
{-# INLINE createT #-}
715+
createT p = runST (p >>= traverse unsafeFreeze)
716+
712717
-- Restricting memory usage
713718
-- ------------------------
714719

@@ -2084,4 +2089,3 @@ dataCast :: (Vector v a, Data a, Typeable1 v, Typeable1 t)
20842089
=> (forall d. Data d => c (t d)) -> Maybe (c (v a))
20852090
{-# INLINE dataCast #-}
20862091
dataCast f = gcast1 f
2087-

Data/Vector/Primitive.hs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ module Data.Vector.Primitive (
4242
empty, singleton, replicate, generate, iterateN,
4343

4444
-- ** Monadic initialisation
45-
replicateM, generateM, create,
45+
replicateM, generateM, create, createT,
4646

4747
-- ** Unfolding
4848
unfoldr, unfoldrN,
@@ -606,6 +606,11 @@ create :: Prim a => (forall s. ST s (MVector s a)) -> Vector a
606606
-- NOTE: eta-expanded due to http://hackage.haskell.org/trac/ghc/ticket/4120
607607
create p = G.create p
608608

609+
-- | Execute the monadic action and freeze the resulting vectors.
610+
createT :: (Traversable f, Prim a) => (forall s. ST s (f (MVector s a))) -> f (Vector a)
611+
{-# INLINE createT #-}
612+
createT p = G.createT p
613+
609614
-- Restricting memory usage
610615
-- ------------------------
611616

@@ -1336,5 +1341,3 @@ unsafeCopy = G.unsafeCopy
13361341
copy :: (Prim a, PrimMonad m) => MVector (PrimState m) a -> Vector a -> m ()
13371342
{-# INLINE copy #-}
13381343
copy = G.copy
1339-
1340-

Data/Vector/Storable.hs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ module Data.Vector.Storable (
3939
empty, singleton, replicate, generate, iterateN,
4040

4141
-- ** Monadic initialisation
42-
replicateM, generateM, create,
42+
replicateM, generateM, create, createT,
4343

4444
-- ** Unfolding
4545
unfoldr, unfoldrN,
@@ -616,6 +616,11 @@ create :: Storable a => (forall s. ST s (MVector s a)) -> Vector a
616616
-- NOTE: eta-expanded due to http://hackage.haskell.org/trac/ghc/ticket/4120
617617
create p = G.create p
618618

619+
-- | Execute the monadic action and freeze the resulting vectors.
620+
createT :: (Traversable f, Storable a) => (forall s. ST s (f (MVector s a))) -> f (Vector a)
621+
{-# INLINE createT #-}
622+
createT p = G.createT p
623+
619624
-- Restricting memory usage
620625
-- ------------------------
621626

@@ -1432,5 +1437,3 @@ unsafeToForeignPtr0 (Vector n fp) = (fp, n)
14321437
unsafeWith :: Storable a => Vector a -> (Ptr a -> IO b) -> IO b
14331438
{-# INLINE unsafeWith #-}
14341439
unsafeWith (Vector _ fp) = withForeignPtr fp
1435-
1436-

Data/Vector/Unboxed.hs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ module Data.Vector.Unboxed (
6262
empty, singleton, replicate, generate, iterateN,
6363

6464
-- ** Monadic initialisation
65-
replicateM, generateM, create,
65+
replicateM, generateM, create, createT,
6666

6767
-- ** Unfolding
6868
unfoldr, unfoldrN,
@@ -585,6 +585,11 @@ create :: Unbox a => (forall s. ST s (MVector s a)) -> Vector a
585585
-- NOTE: eta-expanded due to http://hackage.haskell.org/trac/ghc/ticket/4120
586586
create p = G.create p
587587

588+
-- | Execute the monadic action and freeze the resulting vectors.
589+
createT :: (Traversable f, Unbox a) => (forall s. ST s (f (MVector s a))) -> f (Vector a)
590+
{-# INLINE createT #-}
591+
createT p = G.createT p
592+
588593
-- Restricting memory usage
589594
-- ------------------------
590595

@@ -1431,4 +1436,3 @@ copy = G.copy
14311436

14321437
#define DEFINE_IMMUTABLE
14331438
#include "unbox-tuple-instances"
1434-

tests/Tests/Vector.hs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ testPolymorphicFunctions _ = $(testProperties [
105105
'prop_generate, 'prop_iterateN,
106106

107107
-- Monadic initialisation (FIXME)
108+
'prop_createT,
108109
{- 'prop_replicateM, 'prop_generateM, 'prop_create, -}
109110

110111
-- Unfolding (FIXME)
@@ -217,6 +218,9 @@ testPolymorphicFunctions _ = $(testProperties [
217218
prop_iterateN :: P (Int -> (a -> a) -> a -> v a)
218219
= (\n _ _ -> n < 1000) ===> V.iterateN `eq` (\n f -> take n . iterate f)
219220

221+
prop_createT :: P ((a, v a) -> (a, v a))
222+
prop_createT = (\v -> V.createT (traverse V.thaw v)) `eq` id
223+
220224
prop_head :: P (v a -> a) = not . V.null ===> V.head `eq` head
221225
prop_last :: P (v a -> a) = not . V.null ===> V.last `eq` last
222226
prop_index = \xs ->
@@ -649,4 +653,3 @@ tests = [
649653
testGroup "Data.Vector.Unboxed.Vector (Int,Bool,Int)" (testTupleUnboxedVector (undefined :: Data.Vector.Unboxed.Vector (Int,Bool,Int)))
650654

651655
]
652-

0 commit comments

Comments
 (0)