Skip to content

Commit f33f34e

Browse files
committed
Shorten ST names, put STArray arguments at the end
1 parent 0244214 commit f33f34e

File tree

8 files changed

+149
-152
lines changed

8 files changed

+149
-152
lines changed

src/Data/Array.purs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ import Data.Foldable (foldl, foldr, foldMap, fold, intercalate, elem, notElem, f
128128
import Data.Maybe (Maybe(..), maybe, isJust, fromJust)
129129
import Data.Traversable (scanl, scanr) as Exports
130130
import Data.Traversable (sequence, traverse)
131-
import Data.Tuple (Tuple(..), uncurry)
131+
import Data.Tuple (Tuple(..))
132132
import Data.Unfoldable (class Unfoldable, unfoldr)
133133
import Partial.Unsafe (unsafePartial)
134134
import Unsafe.Coerce (unsafeCoerce)
@@ -658,7 +658,7 @@ mapWithIndex f xs =
658658
-- |
659659
updateAtIndices :: forall t a. Foldable t => t (Tuple Int a) -> Array a -> Array a
660660
updateAtIndices us xs =
661-
ST.run (STA.withArray (\res -> traverse_ (uncurry $ STA.pokeSTArray res) us) xs)
661+
ST.run (STA.withArray (\res -> traverse_ (\(Tuple i a) -> STA.poke i a res) us) xs)
662662

663663
-- | Apply a function to the element at the specified indices,
664664
-- | creating a new array. Out-of-bounds indices will have no effect.
@@ -671,7 +671,7 @@ updateAtIndices us xs =
671671
-- |
672672
modifyAtIndices :: forall t a. Foldable t => t Int -> (a -> a) -> Array a -> Array a
673673
modifyAtIndices is f xs =
674-
ST.run (STA.withArray (\res -> traverse_ (\i -> STA.modifySTArray res i f) is) xs)
674+
ST.run (STA.withArray (\res -> traverse_ (\i -> STA.modify i f res) is) xs)
675675

676676
--------------------------------------------------------------------------------
677677
-- Sorting ---------------------------------------------------------------------
@@ -860,14 +860,14 @@ group' = group <<< sort
860860
groupBy :: forall a. (a -> a -> Boolean) -> Array a -> Array (NonEmptyArray a)
861861
groupBy op xs =
862862
ST.run do
863-
result <- STA.emptySTArray
863+
result <- STA.empty
864864
iter <- STAI.iterator (xs !! _)
865865
STAI.iterate iter \x -> void do
866-
sub <- STA.emptySTArray
866+
sub <- STA.empty
867867
STAI.pushWhile (op x) iter sub
868-
_ <- STA.pushSTArray sub x
868+
_ <- STA.push x sub
869869
grp <- STA.unsafeFreeze sub
870-
STA.pushSTArray result ((unsafeCoerce :: Array ~> NonEmptyArray) grp)
870+
STA.push ((unsafeCoerce :: Array ~> NonEmptyArray) grp) result
871871
STA.unsafeFreeze result
872872

873873
-- | Remove the duplicates from an array, creating a new array.
@@ -1035,12 +1035,12 @@ zip = zipWith Tuple
10351035
unzip :: forall a b. Array (Tuple a b) -> Tuple (Array a) (Array b)
10361036
unzip xs =
10371037
ST.run do
1038-
fsts <- STA.emptySTArray
1039-
snds <- STA.emptySTArray
1038+
fsts <- STA.empty
1039+
snds <- STA.empty
10401040
iter <- STAI.iterator (xs !! _)
10411041
STAI.iterate iter \(Tuple fst snd) -> do
1042-
void $ STA.pushSTArray fsts fst
1043-
void $ STA.pushSTArray snds snd
1042+
void $ STA.push fst fsts
1043+
void $ STA.push snd snds
10441044
fsts' <- STA.unsafeFreeze fsts
10451045
snds' <- STA.unsafeFreeze snds
10461046
pure $ Tuple fsts' snds'

src/Data/Array/ST.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
"use strict";
22

3-
exports.emptySTArray = function () {
3+
exports.empty = function () {
44
return [];
55
};
66

7-
exports.peekSTArrayImpl = function (just) {
7+
exports.peekImpl = function (just) {
88
return function (nothing) {
9-
return function (xs) {
10-
return function (i) {
9+
return function (i) {
10+
return function (xs) {
1111
return function () {
1212
return i >= 0 && i < xs.length ? just(xs[i]) : nothing;
1313
};
@@ -16,9 +16,9 @@ exports.peekSTArrayImpl = function (just) {
1616
};
1717
};
1818

19-
exports.pokeSTArray = function (xs) {
20-
return function (i) {
21-
return function (a) {
19+
exports.poke = function (i) {
20+
return function (a) {
21+
return function (xs) {
2222
return function () {
2323
var ret = i >= 0 && i < xs.length;
2424
if (ret) xs[i] = a;
@@ -28,18 +28,18 @@ exports.pokeSTArray = function (xs) {
2828
};
2929
};
3030

31-
exports.pushAllSTArray = function (xs) {
32-
return function (as) {
31+
exports.pushAll = function (as) {
32+
return function (xs) {
3333
return function () {
3434
return xs.push.apply(xs, as);
3535
};
3636
};
3737
};
3838

39-
exports.spliceSTArray = function (xs) {
40-
return function (i) {
41-
return function (howMany) {
42-
return function (bs) {
39+
exports.splice = function (i) {
40+
return function (howMany) {
41+
return function (bs) {
42+
return function (xs) {
4343
return function () {
4444
return xs.splice.apply(xs, [i, howMany].concat(bs));
4545
};

src/Data/Array/ST.purs

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ module Data.Array.ST
66
( STArray(..)
77
, Assoc
88
, withArray
9-
, emptySTArray
10-
, peekSTArray
11-
, pokeSTArray
12-
, pushSTArray
13-
, modifySTArray
14-
, pushAllSTArray
15-
, spliceSTArray
9+
, empty
10+
, peek
11+
, poke
12+
, push
13+
, modify
14+
, pushAll
15+
, splice
1616
, freeze
1717
, thaw
1818
, unsafeFreeze
@@ -61,7 +61,7 @@ unsafeThaw :: forall h a. Array a -> ST h (STArray h a)
6161
unsafeThaw = pure <<< (unsafeCoerce :: Array a -> STArray h a)
6262

6363
-- | Create an empty mutable array.
64-
foreign import emptySTArray :: forall h a. ST h (STArray h a)
64+
foreign import empty :: forall h a. ST h (STArray h a)
6565

6666
-- | Create a mutable copy of an immutable array.
6767
thaw :: forall h a. Array a -> ST h (STArray h a)
@@ -74,59 +74,54 @@ freeze = copyImpl
7474
foreign import copyImpl :: forall h a b. a -> ST h b
7575

7676
-- | Read the value at the specified index in a mutable array.
77-
peekSTArray
77+
peek
7878
:: forall h a
79-
. STArray h a
80-
-> Int
79+
. Int
80+
-> STArray h a
8181
-> ST h (Maybe a)
82-
peekSTArray = peekSTArrayImpl Just Nothing
82+
peek = peekImpl Just Nothing
8383

84-
foreign import peekSTArrayImpl
84+
foreign import peekImpl
8585
:: forall h a r
8686
. (a -> r)
8787
-> r
88-
-> STArray h a
8988
-> Int
89+
-> STArray h a
9090
-> (ST h r)
9191

9292
-- | Change the value at the specified index in a mutable array.
93-
foreign import pokeSTArray
94-
:: forall h a
95-
. STArray h a -> Int -> a -> ST h Boolean
93+
foreign import poke :: forall h a. Int -> a -> STArray h a -> ST h Boolean
9694

9795
-- | Append an element to the end of a mutable array. Returns the new length of
9896
-- | the array.
99-
pushSTArray :: forall h a. STArray h a -> a -> ST h Int
100-
pushSTArray arr a = pushAllSTArray arr [a]
97+
push :: forall h a. a -> STArray h a -> ST h Int
98+
push a = pushAll [a]
10199

102100
-- | Append the values in an immutable array to the end of a mutable array.
103101
-- | Returns the new length of the mutable array.
104-
foreign import pushAllSTArray
102+
foreign import pushAll
105103
:: forall h a
106-
. STArray h a
107-
-> Array a
104+
. Array a
105+
-> STArray h a
108106
-> ST h Int
109107

110108
-- | Mutate the element at the specified index using the supplied function.
111-
modifySTArray :: forall h a. STArray h a -> Int -> (a -> a) -> ST h Boolean
112-
modifySTArray xs i f = do
113-
entry <- peekSTArray xs i
109+
modify :: forall h a. Int -> (a -> a) -> STArray h a -> ST h Boolean
110+
modify i f xs = do
111+
entry <- peek i xs
114112
case entry of
115-
Just x -> pokeSTArray xs i (f x)
113+
Just x -> poke i (f x) xs
116114
Nothing -> pure false
117115

118116
-- | Remove and/or insert elements from/into a mutable array at the specified index.
119-
foreign import spliceSTArray
117+
foreign import splice
120118
:: forall h a
121-
. STArray h a
122-
-> Int
119+
. Int
123120
-> Int
124121
-> Array a
122+
-> STArray h a
125123
-> ST h (Array a)
126124

127125
-- | Create an immutable copy of a mutable array, where each element
128126
-- | is labelled with its index in the original array.
129-
foreign import toAssocArray
130-
:: forall h a
131-
. STArray h a
132-
-> ST h (Array (Assoc a))
127+
foreign import toAssocArray :: forall h a. STArray h a -> ST h (Array (Assoc a))

src/Data/Array/ST/Iterator.purs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import Control.Monad.ST (ST)
1414
import Control.Monad.ST as ST
1515
import Control.Monad.ST.Ref (STRef)
1616
import Control.Monad.ST.Ref as STRef
17-
import Data.Array.ST (STArray, pushSTArray)
17+
import Data.Array.ST (STArray)
18+
import Data.Array.ST as STA
1819

1920
import Data.Maybe (Maybe(..), isNothing)
2021

@@ -69,7 +70,7 @@ pushWhile p iter array = do
6970
mx <- peek iter
7071
case mx of
7172
Just x | p x -> do
72-
_ <- pushSTArray array x
73+
_ <- STA.push x array
7374
void $ next iter
7475
_ ->
7576
void $ STRef.write true break

src/Data/Array/ST/Partial.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
"use strict";
22

3-
exports.peekSTArrayImpl = function (xs) {
4-
return function (i) {
3+
exports.peekImpl = function (i) {
4+
return function (xs) {
55
return function () {
66
return xs[i];
77
};
88
};
99
};
1010

11-
exports.pokeSTArrayImpl = function (xs) {
12-
return function (i) {
13-
return function (a) {
11+
exports.pokeImpl = function (i) {
12+
return function (a) {
13+
return function (xs) {
1414
return function () {
1515
xs[i] = a;
1616
return {};

src/Data/Array/ST/Partial.purs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,38 @@
33
-- | This module is particularly helpful when performance is very important.
44

55
module Data.Array.ST.Partial
6-
( peekSTArray
7-
, pokeSTArray
6+
( peek
7+
, poke
88
) where
99

1010
import Control.Monad.ST (ST)
1111
import Data.Array.ST (STArray)
1212
import Data.Unit (Unit)
1313

1414
-- | Read the value at the specified index in a mutable array.
15-
peekSTArray
15+
peek
1616
:: forall h a
1717
. Partial
18-
=> STArray h a
19-
-> Int
18+
=> Int
19+
-> STArray h a
2020
-> ST h a
21-
peekSTArray = peekSTArrayImpl
21+
peek = peekImpl
2222

23-
foreign import peekSTArrayImpl :: forall h a. STArray h a -> Int -> ST h a
23+
foreign import peekImpl :: forall h a. Int -> STArray h a -> ST h a
2424

2525
-- | Change the value at the specified index in a mutable array.
26-
pokeSTArray
26+
poke
2727
:: forall h a
2828
. Partial
29-
=> STArray h a
30-
-> Int
29+
=> Int
3130
-> a
31+
-> STArray h a
3232
-> ST h Unit
33-
pokeSTArray = pokeSTArrayImpl
33+
poke = pokeImpl
3434

35-
foreign import pokeSTArrayImpl
35+
foreign import pokeImpl
3636
:: forall h a
37-
. STArray h a
38-
-> Int
37+
. Int
3938
-> a
39+
-> STArray h a
4040
-> ST h Unit

0 commit comments

Comments
 (0)