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

Commit 4c5c3f8

Browse files
committed
Merge pull request #22 from hdgarrood/master
add 'size' to Data.Map
2 parents 27991cb + 757a077 commit 4c5c3f8

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848

4949
singleton :: forall k v. k -> v -> Map k v
5050

51+
size :: forall k v. Map k v -> Number
52+
5153
toList :: forall k v. Map k v -> [Tuple k v]
5254

5355
union :: forall k v. (P.Ord k) => Map k v -> Map k v -> Map k v

src/Data/Map.purs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ module Data.Map
2323
values,
2424
union,
2525
unions,
26-
map
26+
map,
27+
size
2728
) where
2829

2930
import qualified Prelude as P
@@ -237,3 +238,6 @@ unions = foldl union empty
237238

238239
map :: forall k a b. (a -> b) -> Map k a -> Map k b
239240
map = P.(<$>)
241+
242+
size :: forall k v. Map k v -> Number
243+
size = A.length P.<<< values

tests/Data/Map.purs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Debug.Trace
44

55
import Data.Maybe
66
import Data.Tuple
7-
import Data.Array (map)
7+
import Data.Array (map, length, nubBy)
88
import Data.Function (on)
99
import Data.Foldable (foldl)
1010

@@ -165,3 +165,8 @@ mapTests = do
165165

166166
trace "Union is idempotent"
167167
quickCheck $ \m1 m2 -> (m1 `M.union` m2) == ((m1 `M.union` m2) `M.union` (m2 :: M.Map SmallKey Number))
168+
169+
trace "size"
170+
quickCheck $ \xs ->
171+
let xs' = nubBy ((==) `on` fst) xs
172+
in M.size (M.fromList xs') == length (xs' :: [Tuple SmallKey Number])

0 commit comments

Comments
 (0)