Skip to content

Commit 88fe038

Browse files
authored
Add length to Data.Array.ST (#239)
* Add length to Data.Array.ST * Add changelog entry
1 parent dd7beb7 commit 88fe038

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Breaking changes:
88

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

1213
Bugfixes:
1314

src/Data/Array/ST.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ export const poke = function (i) {
2727
};
2828
};
2929

30+
export const length = function (xs) {
31+
return function () {
32+
return xs.length;
33+
};
34+
};
35+
3036
export const popImpl = function (just) {
3137
return function (nothing) {
3238
return function (xs) {

src/Data/Array/ST.purs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module Data.Array.ST
1111
, peek
1212
, poke
1313
, modify
14+
, length
1415
, pop
1516
, push
1617
, pushAll
@@ -149,6 +150,9 @@ foreign import peekImpl
149150
-- | Change the value at the specified index in a mutable array.
150151
foreign import poke :: forall h a. Int -> a -> STArray h a -> ST h Boolean
151152

153+
-- | Get the number of elements in a mutable array.
154+
foreign import length :: forall h a. STArray h a -> ST h Int
155+
152156
-- | Remove the last element from an array and return that element.
153157
pop :: forall h a. STArray h a -> ST h (Maybe a)
154158
pop = popImpl Just Nothing

test/Test/Data/Array/ST.purs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,23 @@ testArrayST = do
4949

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

52+
log "length should return the number of items in an STArray"
53+
54+
assert $ ST.run (do
55+
arr <- STA.thaw nil
56+
length <- STA.length arr
57+
pure $ length == 0)
58+
59+
assert $ ST.run (do
60+
arr <- STA.thaw [1]
61+
length <- STA.length arr
62+
pure $ length == 1)
63+
64+
assert $ ST.run (do
65+
arr <- STA.thaw [1, 2, 3, 4, 5]
66+
length <- STA.length arr
67+
pure $ length == 5)
68+
5269
log "pop should remove elements from an STArray"
5370

5471
assert $ STA.run (do

0 commit comments

Comments
 (0)