Skip to content

Commit 5524a9f

Browse files
committed
Add ST tests, simplify sortByImpl fix
1 parent da62e39 commit 5524a9f

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

src/Data/Array.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -290,17 +290,11 @@ exports.sortByImpl = (function () {
290290
while (i < mid && j < to) {
291291
x = xs2[i];
292292
y = xs2[j];
293-
c = fromOrdering(compare(x)(y)) || i - j;
293+
c = fromOrdering(compare(x)(y));
294294
if (c > 0) {
295295
xs1[k++] = y;
296296
++j;
297297
}
298-
else if (c === 0) {
299-
xs1[k++] = x;
300-
xs1[k++] = y;
301-
++i;
302-
++j;
303-
}
304298
else {
305299
xs1[k++] = x;
306300
++i;

src/Data/Array/ST.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,6 @@ exports.sortByImpl = (function () {
123123
xs1[k++] = y;
124124
++j;
125125
}
126-
else if (c === 0) {
127-
xs1[k++] = x;
128-
xs1[k++] = y;
129-
++i;
130-
++j;
131-
}
132126
else {
133127
xs1[k++] = x;
134128
++i;

test/Test/Data/Array/ST.purs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ module Test.Data.Array.ST (testArrayST) where
33
import Prelude
44

55
import Control.Monad.ST as ST
6+
import Data.Array (range)
67
import Data.Array.ST (withArray)
78
import Data.Array.ST as STA
89
import Data.Foldable (all)
910
import Data.Maybe (Maybe(..), isNothing)
11+
import Data.Tuple (Tuple(..), fst)
1012
import Effect (Effect)
1113
import Effect.Console (log)
1214
import Test.Assert (assert)
@@ -234,11 +236,23 @@ testArrayST = do
234236
STA.sortBy (flip compare) =<< STA.unsafeThaw [1, 3, 2, 5, 6, 4]
235237
) == [6, 5, 4, 3, 2, 1]
236238

239+
log "sortBy should not reorder elements that are equal according to a comparison function"
240+
let s1 = map (Tuple "a") (range 1 100)
241+
assert $ STA.run (
242+
STA.sortBy (comparing fst) =<< STA.unsafeThaw s1
243+
) == s1
244+
237245
log "sortWith should reorder a list into ascending order based on the result of compare over a projection"
238246
assert $ STA.run (
239247
STA.sortWith identity =<< STA.unsafeThaw [1, 3, 2, 5, 6, 4]
240248
) == [1, 2, 3, 4, 5, 6]
241249

250+
log "sortWith should not reorder elements that are equal according to a projection"
251+
let s2 = map (Tuple "a") (range 1 100)
252+
assert $ STA.run (
253+
STA.sortWith fst =<< STA.unsafeThaw s2
254+
) == s2
255+
242256
log "splice should be able to delete multiple items at a specified index"
243257

244258
assert $ STA.run (do

0 commit comments

Comments
 (0)