Skip to content

Commit 287c0a7

Browse files
authored
Merge pull request #89 from nshepperd/createT
Add 'createT', allowing monadic initialisation of any Traversable con…
2 parents c0dd6e4 + c96a659 commit 287c0a7

File tree

6 files changed

+34
-6
lines changed

6 files changed

+34
-6
lines changed

Data/Vector.hs

Lines changed: 6 additions & 1 deletion
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,
@@ -700,6 +700,11 @@ create :: (forall s. ST s (MVector s a)) -> Vector a
700700
-- NOTE: eta-expanded due to http://hackage.haskell.org/trac/ghc/ticket/4120
701701
create p = G.create p
702702

703+
-- | Execute the monadic action and freeze the resulting vectors.
704+
createT :: Traversable f => (forall s. ST s (f (MVector s a))) -> f (Vector a)
705+
{-# INLINE createT #-}
706+
createT p = G.createT p
707+
703708

704709

705710
-- Restricting memory usage

Data/Vector/Generic.hs

Lines changed: 6 additions & 1 deletion
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,
@@ -716,6 +716,11 @@ create :: Vector v a => (forall s. ST s (Mutable v s a)) -> v a
716716
{-# INLINE create #-}
717717
create p = new (New.create p)
718718

719+
-- | Execute the monadic action and freeze the resulting vectors.
720+
createT :: (Traversable f, Vector v a) => (forall s. ST s (f (Mutable v s a))) -> f (v a)
721+
{-# INLINE createT #-}
722+
createT p = runST (p >>= traverse unsafeFreeze)
723+
719724
-- Restricting memory usage
720725
-- ------------------------
721726

Data/Vector/Primitive.hs

Lines changed: 6 additions & 1 deletion
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,
@@ -616,6 +616,11 @@ create :: Prim 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, Prim 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

Data/Vector/Storable.hs

Lines changed: 6 additions & 1 deletion
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,
@@ -626,6 +626,11 @@ create :: Storable a => (forall s. ST s (MVector s a)) -> Vector a
626626
-- NOTE: eta-expanded due to http://hackage.haskell.org/trac/ghc/ticket/4120
627627
create p = G.create p
628628

629+
-- | Execute the monadic action and freeze the resulting vectors.
630+
createT :: (Traversable f, Storable a) => (forall s. ST s (f (MVector s a))) -> f (Vector a)
631+
{-# INLINE createT #-}
632+
createT p = G.createT p
633+
629634
-- Restricting memory usage
630635
-- ------------------------
631636

Data/Vector/Unboxed.hs

Lines changed: 6 additions & 1 deletion
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,
@@ -595,6 +595,11 @@ create :: Unbox a => (forall s. ST s (MVector s a)) -> Vector a
595595
-- NOTE: eta-expanded due to http://hackage.haskell.org/trac/ghc/ticket/4120
596596
create p = G.create p
597597

598+
-- | Execute the monadic action and freeze the resulting vectors.
599+
createT :: (Traversable f, Unbox a) => (forall s. ST s (f (MVector s a))) -> f (Vector a)
600+
{-# INLINE createT #-}
601+
createT p = G.createT p
602+
598603
-- Restricting memory usage
599604
-- ------------------------
600605

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)
@@ -219,6 +220,9 @@ testPolymorphicFunctions _ = $(testProperties [
219220
prop_iterateN :: P (Int -> (a -> a) -> a -> v a)
220221
= (\n _ _ -> n < 1000) ===> V.iterateN `eq` (\n f -> take n . iterate f)
221222

223+
prop_createT :: P ((a, v a) -> (a, v a))
224+
prop_createT = (\v -> V.createT (traverse V.thaw v)) `eq` id
225+
222226
prop_head :: P (v a -> a) = not . V.null ===> V.head `eq` head
223227
prop_last :: P (v a -> a) = not . V.null ===> V.last `eq` last
224228
prop_index = \xs ->
@@ -660,4 +664,3 @@ tests = [
660664
testGroup "Data.Vector.Unboxed.Vector (Int,Bool,Int)" (testTupleUnboxedVector (undefined :: Data.Vector.Unboxed.Vector (Int,Bool,Int)))
661665

662666
]
663-

0 commit comments

Comments
 (0)