Skip to content

Commit d521e17

Browse files
authored
Add clone to Data.Array.ST (#243)
* Add copy to Data.Array.ST * rename copy with clone * modify the original array in the clone test to prove that the cloned array is a copy * update CHANGELOG.md
1 parent 7286121 commit d521e17

File tree

4 files changed

+21
-0
lines changed

4 files changed

+21
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Notable changes to this project are documented in this file. The format is based
77
Breaking changes:
88

99
New features:
10+
- Add `ST.clone` (#243 by @Bgbagan)
1011

1112
Bugfixes:
1213

src/Data/Array/ST.js

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ export const freezeImpl = copyImpl;
5353

5454
export const thawImpl = copyImpl;
5555

56+
export const cloneImpl = copyImpl;
57+
5658
export const sortByImpl = (function () {
5759
function mergeFromTo(compare, fromOrdering, xs1, xs2, from, to) {
5860
var mid;

src/Data/Array/ST.purs

+10
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ module Data.Array.ST
2424
, sortWith
2525
, freeze
2626
, thaw
27+
, clone
2728
, unsafeFreeze
2829
, unsafeThaw
2930
, toAssocArray
@@ -95,6 +96,15 @@ thaw = runSTFn1 thawImpl
9596
-- | Create a mutable copy of an immutable array.
9697
foreign import thawImpl :: forall h a. STFn1 (Array a) h (STArray h a)
9798

99+
-- | Make a mutable copy of a mutable array.
100+
clone
101+
:: forall h a
102+
. STArray h a
103+
-> ST h (STArray h a)
104+
clone = runSTFn1 cloneImpl
105+
106+
foreign import cloneImpl :: forall h a. STFn1 (STArray h a) h (STArray h a)
107+
98108
-- | Sort a mutable array in place. Sorting is stable: the order of equal
99109
-- | elements is preserved.
100110
sort :: forall a h. Ord a => STArray h a -> ST h (STArray h a)

test/Test/Data/Array/ST.purs

+8
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ testArrayST = do
4545
arr <- STA.thaw [1, 2, 3]
4646
STA.freeze arr) == [1, 2, 3]
4747

48+
log "clone should produce a shallow copy of an STArray"
49+
50+
assert $ ST.run (do
51+
arr <- STA.thaw [1, 2, 3]
52+
arr2 <- STA.clone arr
53+
_ <- STA.poke 0 4 arr
54+
STA.freeze arr2) == [1, 2, 3]
55+
4856
log "unsafeThaw should produce an STArray from a standard array"
4957

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

0 commit comments

Comments
 (0)