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

Commit 892378a

Browse files
authored
Merge pull request #73 from purescript/bump
Prepare for 2.0 release
2 parents 77f5362 + 103122a commit 892378a

File tree

5 files changed

+57
-63
lines changed

5 files changed

+57
-63
lines changed

bower.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@
2121
"package.json"
2222
],
2323
"dependencies": {
24-
"purescript-arrays": "^1.0.0",
25-
"purescript-functions": "^1.0.0",
26-
"purescript-lists": "^1.0.0",
27-
"purescript-st": "^1.0.0"
24+
"purescript-arrays": "^3.0.0",
25+
"purescript-functions": "^2.0.0",
26+
"purescript-lists": "^3.0.0",
27+
"purescript-st": "^2.0.0"
2828
},
2929
"devDependencies": {
30-
"purescript-quickcheck": "^1.0.0"
30+
"purescript-quickcheck": "^3.0.0"
3131
}
3232
}

src/Data/Map.purs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ module Data.Map
1919
, fromFoldable
2020
, fromFoldableWith
2121
, toList
22-
, fromList
23-
, fromListWith
22+
, toUnfoldable
2423
, delete
2524
, pop
2625
, member
@@ -38,11 +37,12 @@ module Data.Map
3837
import Prelude
3938

4039
import Data.Foldable (foldl, foldMap, foldr, class Foldable)
41-
import Data.List (List(..), length, nub)
40+
import Data.List (List(..), (:), length, nub)
4241
import Data.Maybe (Maybe(..), maybe, isJust, fromMaybe)
4342
import Data.Monoid (class Monoid)
4443
import Data.Traversable (traverse, class Traversable)
4544
import Data.Tuple (Tuple(..), uncurry, snd)
45+
import Data.Unfoldable (class Unfoldable, unfoldr)
4646

4747
import Partial.Unsafe (unsafePartial)
4848

@@ -55,16 +55,16 @@ data Map k v
5555
instance eqMap :: (Eq k, Eq v) => Eq (Map k v) where
5656
eq m1 m2 = toList m1 == toList m2
5757

58-
instance showMap :: (Show k, Show v) => Show (Map k v) where
59-
show m = "(fromList " <> show (toList m) <> ")"
60-
6158
instance ordMap :: (Ord k, Ord v) => Ord (Map k v) where
6259
compare m1 m2 = compare (toList m1) (toList m2)
6360

64-
instance semigroupMap :: (Ord k) => Semigroup (Map k v) where
61+
instance showMap :: (Show k, Show v) => Show (Map k v) where
62+
show m = "(fromList " <> show (toList m) <> ")"
63+
64+
instance semigroupMap :: Ord k => Semigroup (Map k v) where
6565
append = union
6666

67-
instance monoidMap :: (Ord k) => Monoid (Map k v) where
67+
instance monoidMap :: Ord k => Monoid (Map k v) where
6868
mempty = empty
6969

7070
instance functorMap :: Functor (Map k) where
@@ -77,7 +77,7 @@ instance foldableMap :: Foldable (Map k) where
7777
foldr f z m = foldr f z (values m)
7878
foldMap f m = foldMap f (values m)
7979

80-
instance traversableMap :: (Ord k) => Traversable (Map k) where
80+
instance traversableMap :: Ord k => Traversable (Map k) where
8181
traverse f ms = foldr (\x acc -> union <$> x <*> acc) (pure empty) ((map (uncurry singleton)) <$> (traverse f <$> toList ms))
8282
sequence = traverse id
8383

@@ -368,17 +368,17 @@ fromFoldableWith f = foldl (\m (Tuple k v) -> alter (combine v) k m) empty where
368368
-- | Convert a map to a list of key/value pairs
369369
toList :: forall k v. Map k v -> List (Tuple k v)
370370
toList Leaf = Nil
371-
toList (Two left k v right) = toList left <> pure (Tuple k v) <> toList right
372-
toList (Three left k1 v1 mid k2 v2 right) = toList left <> pure (Tuple k1 v1) <> toList mid <> pure (Tuple k2 v2) <> toList right
373-
374-
-- | Create a map from a list of key/value pairs
375-
fromList :: forall k v. Ord k => List (Tuple k v) -> Map k v
376-
fromList = fromFoldable
371+
toList (Two left k v right) = toList left <> Tuple k v : toList right
372+
toList (Three left k1 v1 mid k2 v2 right) = toList left <> Tuple k1 v1 : toList mid <> Tuple k2 v2 : toList right
377373

378-
-- | Create a map from a list of key/value pairs, using the specified function
379-
-- | to combine values for duplicate keys.
380-
fromListWith :: forall k v. Ord k => (v -> v -> v) -> List (Tuple k v) -> Map k v
381-
fromListWith = fromFoldableWith
374+
-- | Convert a map to an unfoldable structure of key/value pairs
375+
toUnfoldable :: forall f k v. (Ord k, Unfoldable f) => Map k v -> f (Tuple k v)
376+
toUnfoldable = unfoldr go
377+
where
378+
go :: Map k v -> Maybe (Tuple (Tuple k v) (Map k v))
379+
go Leaf = Nothing
380+
go (Two left k v right) = Just $ Tuple (Tuple k v) (left <> right)
381+
go (Three left k1 v1 mid k2 v2 right) = Just $ Tuple (Tuple k1 v1) (Two left k2 v2 right)
382382

383383
-- | Get a list of the keys contained in a map
384384
keys :: forall k v. Map k v -> List k

src/Data/StrMap.purs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ module Data.StrMap
1313
, insert
1414
, lookup
1515
, toList
16+
, toUnfoldable
1617
, fromFoldable
1718
, fromFoldableWith
18-
, fromList
19-
, fromListWith
2019
, delete
2120
, pop
2221
, member
@@ -51,6 +50,7 @@ import Data.Monoid (class Monoid, mempty)
5150
import Data.StrMap.ST as SM
5251
import Data.Traversable (class Traversable, traverse)
5352
import Data.Tuple (Tuple(..), uncurry)
53+
import Data.Unfoldable (class Unfoldable)
5454

5555
-- | `StrMap a` represents a map from `String`s to values of type `a`.
5656
foreign import data StrMap :: * -> *
@@ -191,8 +191,7 @@ update :: forall a. (a -> Maybe a) -> String -> StrMap a -> StrMap a
191191
update f k m = alter (maybe Nothing f) k m
192192

193193
-- | Create a map from a foldable collection of key/value pairs
194-
fromFoldable :: forall f a. (Foldable f) =>
195-
f (Tuple String a) -> StrMap a
194+
fromFoldable :: forall f a. Foldable f => f (Tuple String a) -> StrMap a
196195
fromFoldable l = pureST (do
197196
s <- SM.new
198197
for_ l (\(Tuple k v) -> SM.poke s k v)
@@ -202,28 +201,21 @@ foreign import _lookupST :: forall a h r z. Fn4 z (a -> z) String (SM.STStrMap h
202201

203202
-- | Create a map from a foldable collection of key/value pairs, using the
204203
-- | specified function to combine values for duplicate keys.
205-
fromFoldableWith :: forall f a. (Foldable f) =>
206-
(a -> a -> a) -> f (Tuple String a) -> StrMap a
204+
fromFoldableWith :: forall f a. Foldable f => (a -> a -> a) -> f (Tuple String a) -> StrMap a
207205
fromFoldableWith f l = pureST (do
208206
s <- SM.new
209207
for_ l (\(Tuple k v) -> runFn4 _lookupST v (f v) k s >>= SM.poke s k)
210208
pure s)
211209

212-
-- | Create a map from a list of key/value pairs
213-
fromList :: forall a. L.List (Tuple String a) -> StrMap a
214-
fromList = fromFoldable
215-
216-
-- | Create a map from a list of key/value pairs, using the specified function
217-
-- | to combine values for duplicate keys.
218-
fromListWith :: forall a. (a -> a -> a) -> L.List (Tuple String a) -> StrMap a
219-
fromListWith = fromFoldableWith
220-
221210
foreign import _collect :: forall a b . (String -> a -> b) -> StrMap a -> Array b
222211

223212
-- | Convert a map into a list of key/value pairs
224213
toList :: forall a. StrMap a -> L.List (Tuple String a)
225214
toList = L.fromFoldable <<< _collect Tuple
226215

216+
toUnfoldable :: forall f a. Unfoldable f => StrMap a -> f (Tuple String a)
217+
toUnfoldable = L.toUnfoldable <<< toList
218+
227219
-- | Get an array of the keys in a map
228220
foreign import keys :: forall a. StrMap a -> Array String
229221

test/Test/Data/Map.purs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,20 @@ import Control.Monad.Eff.Random (RANDOM)
1111
import Data.Foldable (foldl, for_, all)
1212
import Data.Function (on)
1313
import Data.List (List(..), groupBy, length, nubBy, sortBy, singleton)
14+
import Data.List.NonEmpty as NEL
1415
import Data.Map as M
1516
import Data.Maybe (Maybe(..), fromMaybe)
1617
import Data.Tuple (Tuple(..), fst)
1718

1819
import Partial.Unsafe (unsafePartial)
1920

20-
import Test.QuickCheck ((<?>), quickCheck, quickCheck')
21+
import Test.QuickCheck ((<?>), (===), quickCheck, quickCheck')
2122
import Test.QuickCheck.Arbitrary (class Arbitrary, arbitrary)
2223

2324
newtype TestMap k v = TestMap (M.Map k v)
2425

2526
instance arbTestMap :: (Eq k, Ord k, Arbitrary k, Arbitrary v) => Arbitrary (TestMap k v) where
26-
arbitrary = TestMap <<< M.fromList <$> arbitrary
27+
arbitrary = TestMap <<< (M.fromFoldable :: List (Tuple k v) -> M.Map k v) <$> arbitrary
2728

2829
data SmallKey = A | B | C | D | E | F | G | H | I | J
2930

@@ -193,25 +194,25 @@ mapTests = do
193194
quickCheck (M.lookup 1 nums == Just 2 <?> "invalid lookup - 1")
194195
quickCheck (M.lookup 2 nums == Nothing <?> "invalid lookup - 2")
195196

196-
log "toList . fromList = id"
197-
quickCheck $ \arr -> let f x = M.toList (M.fromList x)
197+
log "toList . fromFoldable = id"
198+
quickCheck $ \arr -> let f x = M.toList (M.fromFoldable x)
198199
in f (f arr) == f (arr :: List (Tuple SmallKey Int)) <?> show arr
199200

200-
log "fromList . toList = id"
201-
quickCheck $ \(TestMap m) -> let f m' = M.fromList (M.toList m') in
201+
log "fromFoldable . toList = id"
202+
quickCheck $ \(TestMap m) -> let f m' = M.fromFoldable (M.toList m') in
202203
M.toList (f m) == M.toList (m :: M.Map SmallKey Int) <?> show m
203204

204-
log "fromListWith const = fromList"
205-
quickCheck $ \arr -> M.fromListWith const arr ==
206-
M.fromList (arr :: List (Tuple SmallKey Int)) <?> show arr
205+
log "fromFoldableWith const = fromFoldable"
206+
quickCheck $ \arr -> M.fromFoldableWith const arr ==
207+
M.fromFoldable (arr :: List (Tuple SmallKey Int)) <?> show arr
207208

208-
log "fromListWith (<>) = fromList . collapse with (<>) . group on fst"
209+
log "fromFoldableWith (<>) = fromFoldable . collapse with (<>) . group on fst"
209210
quickCheck $ \arr ->
210211
let combine (Tuple s a) (Tuple t b) = (Tuple s $ b <> a)
211212
foldl1 g = unsafePartial \(Cons x xs) -> foldl g x xs
212-
f = M.fromList <<< map (foldl1 combine) <<<
213+
f = M.fromFoldable <<< map (foldl1 combine <<< NEL.toList) <<<
213214
groupBy ((==) `on` fst) <<< sortBy (compare `on` fst) in
214-
M.fromListWith (<>) arr == f (arr :: List (Tuple String String)) <?> show arr
215+
M.fromFoldableWith (<>) arr === f (arr :: List (Tuple String String))
215216

216217
log "Lookup from union"
217218
quickCheck $ \(TestMap m1) (TestMap m2) k ->
@@ -249,7 +250,7 @@ mapTests = do
249250
log "size"
250251
quickCheck $ \xs ->
251252
let xs' = nubBy ((==) `on` fst) xs
252-
in M.size (M.fromList xs') == length (xs' :: List (Tuple SmallKey Int))
253+
in M.size (M.fromFoldable xs') == length (xs' :: List (Tuple SmallKey Int))
253254

254255
log "lookupLE result is correct"
255256
quickCheck $ \k (TestMap m) -> case M.lookupLE k (smallKeyToNumberMap m) of

test/Test/Data/StrMap.purs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Control.Monad.Eff.Random (RANDOM)
1010
import Data.Foldable (foldl)
1111
import Data.Function (on)
1212
import Data.List (List(..), groupBy, sortBy, singleton, fromFoldable, zipWith)
13+
import Data.List.NonEmpty as NEL
1314
import Data.Maybe (Maybe(..))
1415
import Data.StrMap as M
1516
import Data.Tuple (Tuple(..), fst)
@@ -22,7 +23,7 @@ import Test.QuickCheck.Arbitrary (class Arbitrary, arbitrary)
2223
newtype TestStrMap v = TestStrMap (M.StrMap v)
2324

2425
instance arbTestStrMap :: (Arbitrary v) => Arbitrary (TestStrMap v) where
25-
arbitrary = TestStrMap <<< M.fromList <$> arbitrary
26+
arbitrary = TestStrMap <<< (M.fromFoldable :: List (Tuple String v) -> M.StrMap v) <$> arbitrary
2627

2728
data Instruction k v = Insert k v | Delete k
2829

@@ -118,26 +119,26 @@ strMapTests = do
118119
quickCheck (M.lookup "1" nums == Just 2 <?> "invalid lookup - 1")
119120
quickCheck (M.lookup "2" nums == Nothing <?> "invalid lookup - 2")
120121

121-
log "fromFoldable . fromList = id"
122-
quickCheck $ \arr -> let f x = M.toList (M.fromList x)
122+
log "toList . fromFoldable = id"
123+
quickCheck $ \arr -> let f x = M.toList (M.fromFoldable x)
123124
in f (f arr) == f (arr :: List (Tuple String Int)) <?> show arr
124125

125-
log "fromList . fromFoldable = id"
126+
log "fromFoldable . toList = id"
126127
quickCheck $ \(TestStrMap m) ->
127-
let f m1 = M.fromList (M.toList m1) in
128+
let f m1 = M.fromFoldable (M.toList m1) in
128129
M.toList (f m) == M.toList (m :: M.StrMap Int) <?> show m
129130

130-
log "fromListWith const = fromList"
131-
quickCheck $ \arr -> M.fromListWith const arr ==
132-
M.fromList (arr :: List (Tuple String Int)) <?> show arr
131+
log "fromFoldableWith const = fromFoldable"
132+
quickCheck $ \arr -> M.fromFoldableWith const arr ==
133+
M.fromFoldable (arr :: List (Tuple String Int)) <?> show arr
133134

134-
log "fromListWith (<>) = fromList . collapse with (<>) . group on fst"
135+
log "fromFoldableWith (<>) = fromFoldable . collapse with (<>) . group on fst"
135136
quickCheck $ \arr ->
136137
let combine (Tuple s a) (Tuple t b) = (Tuple s $ b <> a)
137138
foldl1 g = unsafePartial \(Cons x xs) -> foldl g x xs
138-
f = M.fromList <<< map (foldl1 combine) <<<
139+
f = M.fromFoldable <<< map (foldl1 combine <<< NEL.toList) <<<
139140
groupBy ((==) `on` fst) <<< sortBy (compare `on` fst) in
140-
M.fromListWith (<>) arr == f (arr :: List (Tuple String String)) <?> show arr
141+
M.fromFoldableWith (<>) arr == f (arr :: List (Tuple String String)) <?> show arr
141142

142143
log "Lookup from union"
143144
quickCheck $ \(TestStrMap m1) (TestStrMap m2) k ->

0 commit comments

Comments
 (0)