Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add clone to Data.Array.ST #243

Merged
merged 4 commits into from
Nov 2, 2023
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]
garyb marked this conversation as resolved.
Show resolved Hide resolved

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

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