Skip to content
This repository was archived by the owner on Oct 4, 2020. It is now read-only.

Commit a51ece2

Browse files
committed
Merge pull request #30 from purescript/updates
Updates
2 parents 6a7f05f + 366a5c7 commit a51ece2

File tree

6 files changed

+107
-106
lines changed

6 files changed

+107
-106
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ Apply a function to the values in a map
238238
#### `size`
239239

240240
``` purescript
241-
size :: forall k v. Map k v -> Number
241+
size :: forall k v. Map k v -> Int
242242
```
243243

244244
Calculate the number of key/value pairs in a map
@@ -247,7 +247,7 @@ Calculate the number of key/value pairs in a map
247247
## Module Data.StrMap
248248

249249

250-
This module defines a type of native Javascript maps which
250+
This module defines a type of native Javascript maps which
251251
require the keys to be strings.
252252

253253
To maximize performance, Javascript objects are not wrapped,
@@ -283,7 +283,7 @@ Convert a mutable map into an immutable map
283283
runST :: forall a r. (forall h. Eff (st :: ST.ST h | r) (SM.STStrMap h a)) -> Eff r (StrMap a)
284284
```
285285

286-
Freeze a mutable map, creating an immutable map. Use this function as you would use
286+
Freeze a mutable map, creating an immutable map. Use this function as you would use
287287
`Prelude.runST` to freeze a mutable reference.
288288

289289
The rank-2 type prevents the map from escaping the scope of `runST`.
@@ -503,7 +503,7 @@ Get an array of the values in a map
503503
union :: forall a. StrMap a -> StrMap a -> StrMap a
504504
```
505505

506-
Compute the union of two maps, preferring the first map in the case of
506+
Compute the union of two maps, preferring the first map in the case of
507507
duplicate keys.
508508

509509
#### `unions`

bower.json

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,18 @@
2222
"package.json"
2323
],
2424
"devDependencies": {
25-
"purescript-quickcheck": "~0.5.0"
25+
"purescript-quickcheck": "~0.6.0"
2626
},
2727
"dependencies": {
28-
"purescript-arrays": "~0.3.0",
29-
"purescript-foldable-traversable": "~0.3.0",
30-
"purescript-strings": "~0.4.2",
31-
"purescript-math": "~0.1.0",
32-
"purescript-maybe": "~0.2.1",
33-
"purescript-tuples": "~0.3.0"
28+
"purescript-arrays": "~0.4.0",
29+
"purescript-foldable-traversable": "~0.4.0",
30+
"purescript-strings": "~0.5.0",
31+
"purescript-math": "~0.1.1",
32+
"purescript-maybe": "~0.3.0",
33+
"purescript-tuples": "~0.4.0",
34+
"purescript-integers": "~0.2.0",
35+
"purescript-prelude": "~0.1.0",
36+
"purescript-eff": "~0.1.0",
37+
"purescript-st": "~0.1.0"
3438
}
3539
}

src/Data/Map.purs

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,37 @@
22
-- | <http://www.cs.princeton.edu/~dpw/courses/cos326-12/ass/2-3-trees.pdf>
33

44
module Data.Map
5-
( Map(),
6-
showTree,
7-
empty,
8-
isEmpty,
9-
singleton,
10-
checkValid,
11-
insert,
12-
lookup,
13-
toList,
14-
fromList,
15-
fromListWith,
16-
delete,
17-
member,
18-
alter,
19-
update,
20-
keys,
21-
values,
22-
union,
23-
unionWith,
24-
unions,
25-
map,
26-
size
5+
( Map()
6+
, showTree
7+
, empty
8+
, isEmpty
9+
, singleton
10+
, checkValid
11+
, insert
12+
, lookup
13+
, toList
14+
, fromList
15+
, fromListWith
16+
, delete
17+
, member
18+
, alter
19+
, update
20+
, keys
21+
, values
22+
, union
23+
, unionWith
24+
, unions
25+
, map
26+
, size
2727
) where
2828

29-
import qualified Data.Array as A
30-
import Data.Maybe
31-
import Data.Tuple
32-
import Data.Monoid (Monoid)
3329
import Data.Foldable (foldl, foldMap, foldr, Foldable)
30+
import Data.Int (Int())
31+
import Data.Maybe (Maybe(..), maybe, isJust)
32+
import Data.Monoid (Monoid)
3433
import Data.Traversable (traverse, Traversable)
34+
import Data.Tuple (Tuple(..), uncurry)
35+
import qualified Data.Array as A
3536

3637
-- | `Map k v` represents maps from keys of type `k` to values of type `v`.
3738
data Map k v
@@ -103,12 +104,12 @@ singleton k v = Two Leaf k v Leaf
103104
-- |
104105
-- | This function is provided for internal use.
105106
checkValid :: forall k v. Map k v -> Boolean
106-
checkValid tree = A.length (A.nub (allHeights tree)) == 1
107+
checkValid tree = A.length (A.nub (allHeights tree)) == one
107108
where
108-
allHeights :: forall k v. Map k v -> [Number]
109-
allHeights Leaf = [0]
110-
allHeights (Two left _ _ right) = A.map (\n -> n + 1) (allHeights left ++ allHeights right)
111-
allHeights (Three left _ _ mid _ _ right) = A.map (\n -> n + 1) (allHeights left ++ allHeights mid ++ allHeights right)
109+
allHeights :: forall k v. Map k v -> [Int]
110+
allHeights Leaf = [zero]
111+
allHeights (Two left _ _ right) = A.map (\n -> n + one) (allHeights left ++ allHeights right)
112+
allHeights (Three left _ _ mid _ _ right) = A.map (\n -> n + one) (allHeights left ++ allHeights mid ++ allHeights right)
112113

113114
-- | Lookup a value for the specified key
114115
lookup :: forall k v. (Ord k) => k -> Map k v -> Maybe v
@@ -172,17 +173,17 @@ delete = down []
172173
where
173174
down :: forall k v. (Ord k) => [TreeContext k v] -> k -> Map k v -> Map k v
174175
down ctx _ Leaf = fromZipper ctx Leaf
175-
down ctx k (Two Leaf k1 _ Leaf)
176+
down ctx k (Two Leaf k1 _ Leaf)
176177
| k == k1 = up ctx Leaf
177-
down ctx k (Two left k1 v1 right)
178+
down ctx k (Two left k1 v1 right)
178179
| k == k1 = let max = maxNode left
179180
in removeMaxNode (TwoLeft max.key max.value right : ctx) left
180181
| k < k1 = down (TwoLeft k1 v1 right : ctx) k left
181182
| otherwise = down (TwoRight left k1 v1 : ctx) k right
182-
down ctx k (Three Leaf k1 v1 Leaf k2 v2 Leaf)
183+
down ctx k (Three Leaf k1 v1 Leaf k2 v2 Leaf)
183184
| k == k1 = fromZipper ctx (Two Leaf k2 v2 Leaf)
184185
| k == k2 = fromZipper ctx (Two Leaf k1 v1 Leaf)
185-
down ctx k (Three left k1 v1 mid k2 v2 right)
186+
down ctx k (Three left k1 v1 mid k2 v2 right)
186187
| k == k1 = let max = maxNode left
187188
in removeMaxNode (ThreeLeft max.key max.value mid k2 v2 right : ctx) left
188189
| k == k2 = let max = maxNode mid
@@ -283,5 +284,5 @@ map :: forall k a b. (a -> b) -> Map k a -> Map k b
283284
map = (<$>)
284285

285286
-- | Calculate the number of key/value pairs in a map
286-
size :: forall k v. Map k v -> Number
287+
size :: forall k v. Map k v -> Int
287288
size = A.length <<< values

src/Data/StrMap.purs

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,47 @@
1-
-- | This module defines a type of native Javascript maps which
1+
-- | This module defines a type of native Javascript maps which
22
-- | require the keys to be strings.
3-
-- |
3+
-- |
44
-- | To maximize performance, Javascript objects are not wrapped,
55
-- | and some native code is used even when it's not necessary.
66

77
module Data.StrMap
8-
( StrMap(),
9-
empty,
10-
isEmpty,
11-
size,
12-
singleton,
13-
insert,
14-
lookup,
15-
toList,
16-
fromList,
17-
fromListWith,
18-
delete,
19-
member,
20-
alter,
21-
update,
22-
keys,
23-
values,
24-
union,
25-
unions,
26-
map,
27-
isSubmap,
28-
fold,
29-
foldMap,
30-
foldM,
31-
foldMaybe,
32-
all,
33-
34-
thawST,
35-
freezeST,
36-
runST
8+
( StrMap()
9+
, empty
10+
, isEmpty
11+
, size
12+
, singleton
13+
, insert
14+
, lookup
15+
, toList
16+
, fromList
17+
, fromListWith
18+
, delete
19+
, member
20+
, alter
21+
, update
22+
, keys
23+
, values
24+
, union
25+
, unions
26+
, map
27+
, isSubmap
28+
, fold
29+
, foldMap
30+
, foldM
31+
, foldMaybe
32+
, all
33+
, thawST
34+
, freezeST
35+
, runST
3736
) where
3837

3938
import Control.Monad.Eff (Eff(), runPure)
4039
import Data.Foldable (Foldable, foldl, foldr, for_)
41-
import Data.Function
42-
import Data.Maybe
43-
import Data.Monoid
44-
import Data.Monoid.All
45-
import Data.Tuple
40+
import Data.Function (Fn2(), runFn2, Fn4(), runFn4)
41+
import Data.Maybe (Maybe(..), maybe, fromMaybe)
42+
import Data.Monoid (Monoid, mempty)
4643
import Data.Traversable (Traversable, traverse)
44+
import Data.Tuple (Tuple(..), uncurry)
4745
import qualified Control.Monad.ST as ST
4846
import qualified Data.Array as A
4947
import qualified Data.StrMap.ST as SM
@@ -83,9 +81,9 @@ thawST = _copyEff
8381
freezeST :: forall a h r. SM.STStrMap h a -> Eff (st :: ST.ST h | r) (StrMap a)
8482
freezeST = _copyEff
8583

86-
-- | Freeze a mutable map, creating an immutable map. Use this function as you would use
84+
-- | Freeze a mutable map, creating an immutable map. Use this function as you would use
8785
-- | `Prelude.runST` to freeze a mutable reference.
88-
-- |
86+
-- |
8987
-- | The rank-2 type prevents the map from escaping the scope of `runST`.
9088
foreign import runST
9189
"""
@@ -329,7 +327,7 @@ foreign import keys
329327
values :: forall a. StrMap a -> [a]
330328
values = _collect (\_ v -> v)
331329

332-
-- | Compute the union of two maps, preferring the first map in the case of
330+
-- | Compute the union of two maps, preferring the first map in the case of
333331
-- | duplicate keys.
334332
union :: forall a. StrMap a -> StrMap a -> StrMap a
335333
union m = mutate (\s -> foldM SM.poke s m)

tests/Data/Map.purs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
module Tests.Data.Map where
22

3-
import Debug.Trace
4-
53
import Control.Alt ((<|>))
6-
import Data.Maybe
7-
import Data.Tuple
84
import Data.Array (groupBy, map, length, nubBy, sortBy)
9-
import Data.Function (on)
105
import Data.Foldable (foldl, for_)
11-
12-
import Test.QuickCheck
13-
6+
import Data.Function (on)
7+
import Data.Maybe (Maybe(..), fromMaybe)
8+
import Data.Int (fromNumber)
9+
import Data.Tuple (Tuple(..), fst)
10+
import Debug.Trace
11+
import Test.QuickCheck ((<?>), quickCheck, quickCheck')
12+
import Test.QuickCheck.Arbitrary (Arbitrary, arbitrary)
1413
import qualified Data.Map as M
1514

1615
instance arbMap :: (Eq k, Ord k, Arbitrary k, Arbitrary v) => Arbitrary (M.Map k v) where
@@ -129,7 +128,7 @@ mapTests = do
129128
<?> ("k1: " ++ show k1 ++ ", v1: " ++ show v1 ++ ", k2: " ++ show k2 ++ ", v2: " ++ show v2)
130129

131130
trace "Check balance property"
132-
quickCheck' 5000 $ \instrs ->
131+
quickCheck' (fromNumber 5000) $ \instrs ->
133132
let
134133
tree :: M.Map SmallKey Number
135134
tree = runInstructions instrs M.empty
@@ -142,7 +141,7 @@ mapTests = do
142141
quickCheck $ \k v -> M.lookup (k :: SmallKey) (M.singleton k (v :: Number)) == Just v
143142

144143
trace "Random lookup"
145-
quickCheck' 5000 $ \instrs k v ->
144+
quickCheck' (fromNumber 5000) $ \instrs k v ->
146145
let
147146
tree :: M.Map SmallKey Number
148147
tree = M.insert k v (runInstructions instrs M.empty)

tests/Data/StrMap.purs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
module Tests.Data.StrMap where
22

3-
import Debug.Trace
4-
5-
import Data.Maybe
6-
import Data.Tuple
7-
import qualified Data.String as S
83
import Data.Array (groupBy, map, sortBy)
9-
import Data.Function (on)
104
import Data.Foldable (foldl)
11-
12-
import Test.QuickCheck
13-
5+
import Data.Function (on)
6+
import Data.Maybe (Maybe(..))
7+
import Data.Int (fromNumber)
8+
import Data.Tuple (Tuple(..), fst, zip)
9+
import Debug.Trace
10+
import Test.QuickCheck ((<?>), quickCheck, quickCheck')
11+
import Test.QuickCheck.Arbitrary (Arbitrary, arbitrary)
12+
import qualified Data.String as S
1413
import qualified Data.StrMap as M
1514

1615
instance arbStrMap :: (Arbitrary v) => Arbitrary (M.StrMap v) where
@@ -70,7 +69,7 @@ strMapTests = do
7069
quickCheck $ \k v -> M.lookup k (M.singleton k (v :: Number)) == Just v
7170

7271
trace "Random lookup"
73-
quickCheck' 5000 $ \instrs k v ->
72+
quickCheck' (fromNumber 5000) $ \instrs k v ->
7473
let
7574
tree :: M.StrMap Number
7675
tree = M.insert k v (runInstructions instrs M.empty)

0 commit comments

Comments
 (0)