Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Data/Array/ST.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ export const freezeImpl = copyImpl;

export const thawImpl = copyImpl;

export const cloneImpl = copyImpl;

export const sortByImpl = (function () {
function mergeFromTo(compare, fromOrdering, xs1, xs2, from, to) {
var mid;
Expand Down
10 changes: 10 additions & 0 deletions src/Data/Array/ST.purs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module Data.Array.ST
, sortWith
, freeze
, thaw
, clone
, unsafeFreeze
, unsafeThaw
, toAssocArray
Expand Down Expand Up @@ -95,6 +96,15 @@ thaw = runSTFn1 thawImpl
-- | Create a mutable copy of an immutable array.
foreign import thawImpl :: forall h a. STFn1 (Array a) h (STArray h a)

-- | Make a mutable copy of a mutable array.
clone
:: forall h a
. STArray h a
-> ST h (STArray h a)
clone = runSTFn1 cloneImpl

foreign import cloneImpl :: forall h a. STFn1 (STArray h a) h (STArray h a)

-- | Sort a mutable array in place. Sorting is stable: the order of equal
-- | elements is preserved.
sort :: forall a h. Ord a => STArray h a -> ST h (STArray h a)
Expand Down
7 changes: 7 additions & 0 deletions test/Test/Data/Array/ST.purs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ testArrayST = do
arr <- STA.thaw [1, 2, 3]
STA.freeze arr) == [1, 2, 3]

log "clone should produce a shallow copy of an STArray"

assert $ ST.run (do
arr <- STA.thaw [1, 2, 3]
arr2 <- STA.clone arr
STA.freeze arr2) == [1, 2, 3]

log "unsafeThaw should produce an STArray from a standard array"

assert $ STA.run (STA.unsafeThaw [1, 2, 3]) == [1, 2, 3]
Expand Down