Skip to content

Commit 01a9175

Browse files
committed
Merge branch 'master' of github.com:purescript/purescript-arrays into compiler/0.12
2 parents 56f6e23 + 8058d49 commit 01a9175

File tree

5 files changed

+72
-6
lines changed

5 files changed

+72
-6
lines changed

src/Data/Array/NonEmpty.purs

+2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ module Data.Array.NonEmpty
6666

6767
, nub
6868
, nubBy
69+
, nubEq
70+
, nubByEq
6971
, union
7072
, union'
7173
, unionBy

src/Data/Array/ST.js

+10
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ exports.copyImpl = function (xs) {
5454
};
5555
};
5656

57+
exports.sortByImpl = function (comp) {
58+
return function (xs) {
59+
return function () {
60+
return xs.sort(function (x, y) {
61+
return comp(x)(y);
62+
});
63+
};
64+
};
65+
};
66+
5767
exports.toAssocArray = function (xs) {
5868
return function () {
5969
var n = xs.length;

src/Data/Array/ST.purs

+35
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ module Data.Array.ST
1313
, modify
1414
, pushAll
1515
, splice
16+
, sort
17+
, sortBy
18+
, sortWith
1619
, freeze
1720
, thaw
1821
, unsafeFreeze
@@ -67,6 +70,38 @@ foreign import empty :: forall h a. ST h (STArray h a)
6770
thaw :: forall h a. Array a -> ST h (STArray h a)
6871
thaw = copyImpl
6972

73+
-- | Sort a mutable array in place.
74+
sort :: forall a h. Ord a => STArray h a -> ST h (STArray h a)
75+
sort = sortBy compare
76+
77+
-- | Sort a mutable array in place using a comparison function.
78+
sortBy
79+
:: forall a h
80+
. (a -> a -> Ordering)
81+
-> STArray h a
82+
-> ST h (STArray h a)
83+
sortBy comp = sortByImpl comp'
84+
where
85+
comp' x y = case comp x y of
86+
GT -> 1
87+
EQ -> 0
88+
LT -> -1
89+
90+
foreign import sortByImpl
91+
:: forall a h
92+
. (a -> a -> Int)
93+
-> STArray h a
94+
-> ST h (STArray h a)
95+
96+
-- | Sort a mutable array in place based on a projection.
97+
sortWith
98+
:: forall a b h
99+
. Ord b
100+
=> (a -> b)
101+
-> STArray h a
102+
-> ST h (STArray h a)
103+
sortWith f = sortBy (comparing f)
104+
70105
-- | Create an immutable copy of a mutable array.
71106
freeze :: forall h a. STArray h a -> ST h (Array a)
72107
freeze = copyImpl

test/Test/Data/Array/NonEmpty.purs

+7-4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ module Test.Data.Array.NonEmpty (testNonEmptyArray) where
22

33
import Prelude
44

5-
import Effect (Effect)
6-
import Effect.Console (log)
75
import Data.Array.NonEmpty as NEA
86
import Data.Const (Const(..))
97
import Data.Foldable (for_, sum, traverse_)
@@ -13,6 +11,8 @@ import Data.Monoid.Additive (Additive(..))
1311
import Data.Semigroup.Foldable (foldMap1)
1412
import Data.Semigroup.Traversable (traverse1)
1513
import Data.Tuple (Tuple(..))
14+
import Effect (Effect)
15+
import Effect.Console (log, logShow)
1616
import Partial.Unsafe (unsafePartial)
1717
import Test.Assert (assert)
1818

@@ -234,9 +234,12 @@ testNonEmptyArray = do
234234
log "nub should remove duplicate elements from the list, keeping the first occurence"
235235
assert $ NEA.nub (fromArray [1, 2, 2, 3, 4, 1]) == fromArray [1, 2, 3, 4]
236236

237-
log "nubBy should remove duplicate items from the list using a supplied predicate"
237+
log "nubEq should remove duplicate elements from the list, keeping the first occurence"
238+
assert $ NEA.nubEq (fromArray [1, 2, 2, 3, 4, 1]) == fromArray [1, 2, 3, 4]
239+
240+
log "nubByEq should remove duplicate items from the list using a supplied predicate"
238241
let nubPred = \x y -> if odd x then false else x == y
239-
assert $ NEA.nubBy nubPred (fromArray [1, 2, 2, 3, 3, 4, 4, 1]) == fromArray [1, 2, 3, 3, 4, 1]
242+
assert $ NEA.nubByEq nubPred (fromArray [1, 2, 2, 3, 3, 4, 4, 1]) == fromArray [1, 2, 3, 3, 4, 1]
240243

241244
log "union should produce the union of two arrays"
242245
assert $ NEA.union (fromArray [1, 2, 3]) (fromArray [2, 3, 4]) == fromArray [1, 2, 3, 4]

test/Test/Data/Array/ST.purs

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
module Test.Data.Array.ST (testArrayST) where
22

33
import Prelude
4-
import Effect (Effect)
5-
import Effect.Console (log)
4+
65
import Control.Monad.ST (ST)
76
import Control.Monad.ST as ST
87
import Data.Array.ST (STArray)
98
import Data.Array.ST as STA
109
import Data.Foldable (all)
1110
import Data.Maybe (Maybe(..), isNothing)
11+
import Effect (Effect)
12+
import Effect.Console (log)
1213
import Test.Assert (assert)
1314

1415
run :: forall a. (forall r. ST r (STArray r a)) -> Array a
@@ -134,6 +135,21 @@ testArrayST = do
134135
void $ STA.poke 1 2 arr
135136
pure arr) == [1]
136137

138+
log "sort should reorder a list into ascending order based on the result of compare"
139+
assert $ run (
140+
STA.sort =<< STA.unsafeThaw [1, 3, 2, 5, 6, 4]
141+
) == [1, 2, 3, 4, 5, 6]
142+
143+
log "sortBy should reorder a list into ascending order based on the result of a comparison function"
144+
assert $ run (
145+
STA.sortBy (flip compare) =<< STA.unsafeThaw [1, 3, 2, 5, 6, 4]
146+
) == [6, 5, 4, 3, 2, 1]
147+
148+
log "sortWith should reorder a list into ascending order based on the result of compare over a projection"
149+
assert $ run (
150+
STA.sortWith identity =<< STA.unsafeThaw [1, 3, 2, 5, 6, 4]
151+
) == [1, 2, 3, 4, 5, 6]
152+
137153
log "splice should be able to delete multiple items at a specified index"
138154

139155
assert $ run (do

0 commit comments

Comments
 (0)