diff --git a/src/Data/Array/ST.purs b/src/Data/Array/ST.purs index ef8b0e16..baac4e2f 100644 --- a/src/Data/Array/ST.purs +++ b/src/Data/Array/ST.purs @@ -100,11 +100,13 @@ foreign import pokeSTArray :: forall a h r . STArray h a -> Int -> a -> Eff (st :: ST h | r) Boolean --- | Append an element to the end of a mutable array. +-- | Append an element to the end of a mutable array. Returns the new length of +-- | the array. pushSTArray :: forall a h r. STArray h a -> a -> Eff (st :: ST h | r) Int pushSTArray arr a = pushAllSTArray arr [a] -- | Append the values in an immutable array to the end of a mutable array. +-- | Returns the new length of the mutable array. foreign import pushAllSTArray :: forall a h r . STArray h a diff --git a/test/Test/Data/Array/ST.purs b/test/Test/Data/Array/ST.purs index 337b59b4..78f80c73 100644 --- a/test/Test/Data/Array/ST.purs +++ b/test/Test/Data/Array/ST.purs @@ -42,6 +42,12 @@ testArrayST = do void $ pushSTArray arr 4 pure arr) == [1, 2, 3, 4] + log "pushSTArray should return the new length of the array" + + assert $ pureST (do + arr <- thaw [unit, unit, unit] + pushSTArray arr unit) == 4 + log "pushAllSTArray should append multiple values to the end of the array" assert $ run (do @@ -54,6 +60,12 @@ testArrayST = do void $ pushAllSTArray arr [4, 5, 6] pure arr) == [1, 2, 3, 4, 5, 6] + log "pushAllSTArray should return the new length of the array" + + assert $ pureST (do + arr <- thaw [unit, unit, unit] + pushAllSTArray arr [unit, unit]) == 5 + log "peekSTArray should return Nothing when peeking a value outside the array bounds" assert $ isNothing $ pureST (do