Skip to content

Rename Data.Array.ST.empty #191

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

Merged
merged 2 commits into from
Dec 23, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 5 additions & 5 deletions src/Data/Array.purs
Original file line number Diff line number Diff line change
Expand Up @@ -887,10 +887,10 @@ group' = group <<< sort
groupBy :: forall a. (a -> a -> Boolean) -> Array a -> Array (NonEmptyArray a)
groupBy op xs =
ST.run do
result <- STA.empty
result <- STA.new
iter <- STAI.iterator (xs !! _)
STAI.iterate iter \x -> void do
sub <- STA.empty
sub <- STA.new
_ <- STA.push x sub
STAI.pushWhile (op x) iter sub
grp <- STA.unsafeFreeze sub
Expand Down Expand Up @@ -952,7 +952,7 @@ nubBy comp xs = case head indexedAndSorted of
-- |
nubByEq :: forall a. (a -> a -> Boolean) -> Array a -> Array a
nubByEq eq xs = ST.run do
arr <- STA.empty
arr <- STA.new
ST.foreach xs \x -> do
e <- not <<< Exports.any (_ `eq` x) <$> (STA.unsafeFreeze arr)
when e $ void $ STA.push x arr
Expand Down Expand Up @@ -1101,8 +1101,8 @@ zip = zipWith Tuple
unzip :: forall a b. Array (Tuple a b) -> Tuple (Array a) (Array b)
unzip xs =
ST.run do
fsts <- STA.empty
snds <- STA.empty
fsts <- STA.new
snds <- STA.new
iter <- STAI.iterator (xs !! _)
STAI.iterate iter \(Tuple fst snd) -> do
void $ STA.push fst fsts
Expand Down
2 changes: 1 addition & 1 deletion src/Data/Array/ST.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";

exports.empty = function () {
exports["new"] = function () {
return [];
};

Expand Down
9 changes: 7 additions & 2 deletions src/Data/Array/ST.purs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Data.Array.ST
, Assoc
, run
, withArray
, new
, empty
, peek
, poke
Expand All @@ -33,6 +34,7 @@ import Prelude
import Control.Monad.ST as ST
import Control.Monad.ST (ST, Region)
import Data.Maybe (Maybe(..))
import Prim.TypeError (class Warn, Text)

-- | A reference to a mutable array.
-- |
Expand Down Expand Up @@ -75,8 +77,11 @@ foreign import unsafeFreeze :: forall h a. STArray h a -> ST h (Array a)
-- | array must not be used afterward.
foreign import unsafeThaw :: forall h a. Array a -> ST h (STArray h a)

-- | Create an empty mutable array.
foreign import empty :: forall h a. ST h (STArray h a)
-- | Create a new, empty mutable array.
foreign import new :: forall h a. ST h (STArray h a)

empty :: forall h a. Warn (Text "'Data.Array.ST.empty' is deprecated, use 'Data.Array.ST.new' instead") => ST h (STArray h a)
empty = new

-- | Create a mutable copy of an immutable array.
foreign import thaw :: forall h a. Array a -> ST h (STArray h a)
Expand Down
22 changes: 11 additions & 11 deletions test/Test/Data/Array/ST.purs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ testArrayST = do
log "run should produce an immutable array by running a constructor operation"

assert $ STA.run (do
arr <- STA.empty
arr <- STA.new
void $ STA.push 1 arr
void $ STA.push 2 arr
pure arr) == [1, 2]
Expand All @@ -30,7 +30,7 @@ testArrayST = do

log "empty should produce an empty array"

assert $ STA.run STA.empty == nil
assert $ STA.run STA.new == nil

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

Expand Down Expand Up @@ -62,13 +62,13 @@ testArrayST = do
log "pop should return Nothing when given an empty array"

assert $ isNothing $ ST.run (do
arr <- STA.empty
arr <- STA.new
STA.pop arr)

log "push should append a value to the end of the array"

assert $ STA.run (do
arr <- STA.empty
arr <- STA.new
void $ STA.push 1 arr
void $ STA.push 2 arr
pure arr) == [1, 2]
Expand All @@ -87,7 +87,7 @@ testArrayST = do
log "pushAll should append multiple values to the end of the array"

assert $ STA.run (do
arr <- STA.empty
arr <- STA.new
void $ STA.pushAll [1, 2] arr
pure arr) == [1, 2]

Expand All @@ -105,15 +105,15 @@ testArrayST = do
log "peek should return Nothing when peeking a value outside the array bounds"

assert $ isNothing $ ST.run (do
arr <- STA.empty
arr <- STA.new
STA.peek 0 arr)

assert $ isNothing $ ST.run (do
arr <- STA.thaw [1]
STA.peek 1 arr)

assert $ isNothing $ ST.run (do
arr <- STA.empty
arr <- STA.new
STA.peek (-1) arr)

log "peek should return the value at the specified index"
Expand All @@ -139,7 +139,7 @@ testArrayST = do
log "poke should return false when attempting to modify a value outside the array bounds"

assert $ not $ ST.run (do
arr <- STA.empty
arr <- STA.new
STA.poke 0 10 arr)

assert $ not $ ST.run (do
Expand Down Expand Up @@ -180,13 +180,13 @@ testArrayST = do
log "shift should return Nothing when given an empty array"

assert $ isNothing $ ST.run (do
arr <- STA.empty
arr <- STA.new
STA.shift arr)

log "unshift should append a value to the front of the array"

assert $ STA.run (do
arr <- STA.empty
arr <- STA.new
void $ STA.unshift 1 arr
void $ STA.unshift 2 arr
pure arr) == [2, 1]
Expand All @@ -205,7 +205,7 @@ testArrayST = do
log "unshiftAll should append multiple values to the front of the array"

assert $ STA.run (do
arr <- STA.empty
arr <- STA.new
void $ STA.unshiftAll [1, 2] arr
pure arr) == [1, 2]

Expand Down