Skip to content

Commit b363605

Browse files
mhuisipaf31
authored andcommitted
prevent runtime error for partial functions for ST arrays (#111)
* prevent runtime error for partial functions for ST arrays * add Data.Array.ST.Partial tests
1 parent e300101 commit b363605

File tree

4 files changed

+49
-4
lines changed

4 files changed

+49
-4
lines changed

src/Data/Array/ST/Partial.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
"use strict";
22

3-
exports.peekSTArray = function (xs) {
3+
exports.peekSTArrayImpl = function (xs) {
44
return function (i) {
55
return function () {
66
return xs[i];
77
};
88
};
99
};
1010

11-
exports.pokeSTArray = function (xs) {
11+
exports.pokeSTArrayImpl = function (xs) {
1212
return function (i) {
1313
return function (a) {
1414
return function () {

src/Data/Array/ST/Partial.purs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,33 @@ import Data.Array.ST (STArray)
1313
import Data.Unit (Unit)
1414

1515
-- | Read the value at the specified index in a mutable array.
16-
foreign import peekSTArray
16+
peekSTArray
1717
:: forall a h r
1818
. Partial
1919
=> STArray h a
2020
-> Int
2121
-> Eff (st :: ST h | r) a
22+
peekSTArray = peekSTArrayImpl
23+
24+
foreign import peekSTArrayImpl
25+
:: forall a h r
26+
. STArray h a
27+
-> Int
28+
-> Eff (st :: ST h | r) a
2229

2330
-- | Change the value at the specified index in a mutable array.
24-
foreign import pokeSTArray
31+
pokeSTArray
2532
:: forall a h r
2633
. Partial
2734
=> STArray h a
2835
-> Int
2936
-> a
3037
-> Eff (st :: ST h | r) Unit
38+
pokeSTArray = pokeSTArrayImpl
39+
40+
foreign import pokeSTArrayImpl
41+
:: forall a h r
42+
. STArray h a
43+
-> Int
44+
-> a
45+
-> Eff (st :: ST h | r) Unit

test/Test/Data/Array/ST/Partial.purs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module Test.Data.Array.ST.Partial (testArraySTPartial) where
2+
3+
import Prelude
4+
5+
import Control.Monad.Eff (Eff)
6+
import Control.Monad.Eff.Console (log, CONSOLE)
7+
import Control.Monad.ST (pureST)
8+
9+
import Data.Array.ST (thaw, unsafeFreeze)
10+
import Data.Array.ST.Partial (peekSTArray, pokeSTArray)
11+
12+
import Partial.Unsafe (unsafePartial)
13+
14+
import Test.Assert (assert, ASSERT)
15+
16+
testArraySTPartial :: forall eff. Eff (console :: CONSOLE, assert :: ASSERT | eff) Unit
17+
testArraySTPartial = do
18+
19+
log "peekSTArray should return the value at the specified index"
20+
assert $ 2 == pureST do
21+
a <- thaw [1, 2, 3]
22+
unsafePartial $ peekSTArray a 1
23+
24+
log "pokeSTArray should modify the value at the specified index"
25+
assert $ [1, 4, 3] == pureST do
26+
a <- thaw [1, 2, 3]
27+
unsafePartial $ pokeSTArray a 1 4
28+
unsafeFreeze a

test/Test/Main.purs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ import Test.Assert (ASSERT)
99
import Test.Data.Array (testArray)
1010
import Test.Data.Array.Partial (testArrayPartial)
1111
import Test.Data.Array.ST (testArrayST)
12+
import Test.Data.Array.ST.Partial (testArraySTPartial)
1213

1314
main :: forall eff. Eff (console :: CONSOLE, assert :: ASSERT | eff) Unit
1415
main = do
1516
testArray
1617
testArrayST
1718
testArrayPartial
19+
testArraySTPartial

0 commit comments

Comments
 (0)