File tree Expand file tree Collapse file tree 2 files changed +24
-1
lines changed Expand file tree Collapse file tree 2 files changed +24
-1
lines changed Original file line number Diff line number Diff line change 5
5
module Data.Array.ST
6
6
( STArray (..)
7
7
, Assoc
8
+ , run
8
9
, withArray
9
10
, empty
10
11
, peek
@@ -25,6 +26,7 @@ module Data.Array.ST
25
26
26
27
import Prelude
27
28
29
+ import Control.Monad.ST as ST
28
30
import Control.Monad.ST (ST , kind Region )
29
31
import Data.Maybe (Maybe (..))
30
32
import Unsafe.Coerce (unsafeCoerce )
@@ -41,6 +43,13 @@ foreign import data STArray :: Region -> Type -> Type
41
43
-- | An element and its index.
42
44
type Assoc a = { value :: a , index :: Int }
43
45
46
+ -- | A safe way to create and work with a mutable array before returning an
47
+ -- | immutable array for later perusal. This function avoids copying the array
48
+ -- | before returning it - it uses unsafeFreeze internally, but this wrapper is
49
+ -- | a safe interface to that function.
50
+ run :: forall a . (forall h . ST h (STArray h a )) -> Array a
51
+ run st = ST .run (st >>= unsafeFreeze)
52
+
44
53
-- | Perform an effect requiring a mutable array on a copy of an immutable array,
45
54
-- | safely returning the result as an immutable array.
46
55
withArray
Original file line number Diff line number Diff line change @@ -4,7 +4,7 @@ import Prelude
4
4
5
5
import Control.Monad.ST (ST )
6
6
import Control.Monad.ST as ST
7
- import Data.Array.ST (STArray )
7
+ import Data.Array.ST (STArray , withArray )
8
8
import Data.Array.ST as STA
9
9
import Data.Foldable (all )
10
10
import Data.Maybe (Maybe (..), isNothing )
@@ -18,6 +18,20 @@ run act = ST.run (act >>= STA.unsafeFreeze)
18
18
testArrayST :: Effect Unit
19
19
testArrayST = do
20
20
21
+ log " run should produce an immutable array by running a constructor operation"
22
+
23
+ assert $ STA .run (do
24
+ arr <- STA .empty
25
+ void $ STA .push 1 arr
26
+ void $ STA .push 2 arr
27
+ pure arr) == [1 , 2 ]
28
+
29
+ log " withArray should run an operation on a copy of an array"
30
+
31
+ let original = [1 , 2 , 3 ]
32
+ assert $ ST .run (withArray (STA .push 42 ) original) == [1 , 2 , 3 , 42 ]
33
+ assert $ original == [1 , 2 , 3 ]
34
+
21
35
log " empty should produce an empty array"
22
36
23
37
assert $ run STA .empty == nil
You can’t perform that action at this time.
0 commit comments