diff --git a/CHANGELOG.md b/CHANGELOG.md index 9540992..4626209 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ Notable changes to this project are documented in this file. The format is based Breaking changes: New features: +- Add `ST.clone` (#243 by @Bgbagan) Bugfixes: diff --git a/src/Data/Array/ST.js b/src/Data/Array/ST.js index 2795f00..60f250c 100644 --- a/src/Data/Array/ST.js +++ b/src/Data/Array/ST.js @@ -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; diff --git a/src/Data/Array/ST.purs b/src/Data/Array/ST.purs index 5ff3c1a..158e410 100644 --- a/src/Data/Array/ST.purs +++ b/src/Data/Array/ST.purs @@ -24,6 +24,7 @@ module Data.Array.ST , sortWith , freeze , thaw + , clone , unsafeFreeze , unsafeThaw , toAssocArray @@ -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) diff --git a/test/Test/Data/Array/ST.purs b/test/Test/Data/Array/ST.purs index 6e42fc2..d408ec1 100644 --- a/test/Test/Data/Array/ST.purs +++ b/test/Test/Data/Array/ST.purs @@ -45,6 +45,14 @@ 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.poke 0 4 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]