Skip to content

Add length to Data.Array.ST #239

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 3 commits into from
Jan 17, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Breaking changes:

New features:
- Add `prependArray` (#224 by @JordanMartinez)
- Add `Data.Array.ST.length` (#239 by @Blugatroff)

Bugfixes:

Expand Down
6 changes: 6 additions & 0 deletions src/Data/Array/ST.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ export const poke = function (i) {
};
};

export const length = function (xs) {
return function () {
return xs.length;
};
};

export const popImpl = function (just) {
return function (nothing) {
return function (xs) {
Expand Down
4 changes: 4 additions & 0 deletions src/Data/Array/ST.purs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module Data.Array.ST
, peek
, poke
, modify
, length
, pop
, push
, pushAll
Expand Down Expand Up @@ -149,6 +150,9 @@ foreign import peekImpl
-- | Change the value at the specified index in a mutable array.
foreign import poke :: forall h a. Int -> a -> STArray h a -> ST h Boolean

-- | Get the number of elements in a mutable array.
foreign import length :: forall h a. STArray h a -> ST h Int

-- | Remove the last element from an array and return that element.
pop :: forall h a. STArray h a -> ST h (Maybe a)
pop = popImpl Just Nothing
Expand Down
17 changes: 17 additions & 0 deletions test/Test/Data/Array/ST.purs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,23 @@ testArrayST = do

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

log "length should return the number of items in an STArray"

assert $ ST.run (do
arr <- STA.thaw nil
length <- STA.length arr
pure $ length == 0)

assert $ ST.run (do
arr <- STA.thaw [1]
length <- STA.length arr
pure $ length == 1)

assert $ ST.run (do
arr <- STA.thaw [1, 2, 3, 4, 5]
length <- STA.length arr
pure $ length == 5)

log "pop should remove elements from an STArray"

assert $ STA.run (do
Expand Down