@@ -18,7 +18,7 @@ module Data.Array.ST
18
18
) where
19
19
20
20
import Prelude
21
- import Control.Monad.Eff (Eff )
21
+ import Control.Monad.Eff (Eff , Pure , runPure )
22
22
import Control.Monad.ST (ST )
23
23
import Data.Maybe (Maybe (..))
24
24
import Data.Newtype (class Newtype , unwrap )
@@ -43,25 +43,41 @@ derive instance newtypeId :: Newtype (Id a f) _
43
43
-- | Freeze a mutable array, creating an immutable array. Use this function as you would use
44
44
-- | `runST` to freeze a mutable reference.
45
45
-- |
46
- -- | The rank-2 type prevents the reference from escaping the scope of `runSTArray`.
46
+ -- | The rank-2 type prevents the reference from escaping the scope of `runSTArray'`,
47
+ -- | and the closed row on the `Eff` computation prevents the reference from
48
+ -- | escaping into other parts of your program via native effects such as `setTimeout`.
49
+ -- |
50
+ -- | You can also return an immutable copy of an `STArray` from an `ST` computation
51
+ -- | by using `freeze` combined with `runST`. However, when possible, you should
52
+ -- | prefer this function, because it is `O(1)`. By contrast, `freeze` must copy the
53
+ -- | underlying array and is therefore `O(n)`.
47
54
runSTArray
48
- :: forall a r
49
- . (forall h . Eff (st :: ST h | r ) (STArray h a ))
50
- -> Eff r ( Array a )
51
- runSTArray a = map unwrap (runSTArray' (map Id a))
55
+ :: forall a
56
+ . (forall h . Eff (st :: ST h ) (STArray h a ))
57
+ -> Array a
58
+ runSTArray a = unwrap (runSTArray' (map Id a))
52
59
53
60
-- | Freeze all mutable arrays in some structure, creating a version of the
54
61
-- | same structure where all mutable arrays are replaced with immutable
55
62
-- | arrays. Use this function as you would use `runST` to freeze a mutable
56
63
-- | reference.
57
64
-- |
58
- -- | The rank-2 type prevents the reference from escaping the scope of `runSTArray'`.
65
+ -- | The rank-2 type prevents the reference from escaping the scope of `runSTArray'`,
66
+ -- | and the closed row on the `Eff` computation prevents the reference from
67
+ -- | escaping into other parts of your program via native effects such as `setTimeout`.
68
+ -- |
69
+ -- | You can also return an immutable copy of an `STArray` from an `ST` computation
70
+ -- | by using `freeze` combined with `runST`. However, when possible, you should
71
+ -- | prefer this function, because it is `O(1)`. By contrast, `freeze` must copy the
72
+ -- | underlying array and is therefore `O(n)`.
59
73
runSTArray'
60
- :: forall f r
61
- . (forall h . Eff (st :: ST h | r ) (f (STArray h )))
62
- -> Eff r (f Array )
63
- runSTArray' =
64
- unsafeCoerce
74
+ :: forall f
75
+ . (forall h . Eff (st :: ST h ) (f (STArray h )))
76
+ -> f Array
77
+ runSTArray' x = runPure (go x)
78
+ where
79
+ go :: (forall h . Eff (st :: ST h ) (f (STArray h ))) -> Pure (f Array )
80
+ go = unsafeCoerce
65
81
66
82
-- | Create an empty mutable array.
67
83
foreign import emptySTArray :: forall a h r . Eff (st :: ST h | r ) (STArray h a )
0 commit comments