@@ -38,21 +38,25 @@ onMap = onMaps {n=1}
3838empty : Ord k => SortedBag k
3939empty = BagWrapper empty
4040
41+||| The type of `insert` and the common core of most functions that update
42+||| the contests of a Bag.
43+UpdateBag : Type -> Type
44+UpdateBag k = k -> SortedBag k -> SortedBag k
45+
4146-- Keep in mind:
4247-- Idris> the Nat $ sum Nothing
4348-- 0 : Nat
4449-- Idris> the Nat $ sum $ Just 10
4550-- 10 : Nat
4651
47−||| The type of `insert` and the common core of most functions that update
48−||| the contests of a Bag.
49−UpdateBag : Type -> Type
50−UpdateBag k = k -> SortedBag k -> SortedBag k
52+||| Perform a count on the underlying `SortedMap`
53+countMap : k -> SortedMap k PosNat -> Nat
54+countMap k m = sum $ fst <$> lookup k m
5155
5256||| Apply a function `(Nat -> Nat)` to a given argument
5357alterCounts : (Nat -> Nat) -> UpdateBag k
5458alterCounts f k = onMap $ \m => let
55− n = sum $ fst <$> lookup k m
59+ n = countMap k m
5660 n' = f n
5761 in m |> if n == n'
5862 then id -- No change before and after.
@@ -62,11 +66,11 @@ alterCounts f k = onMap $ \m => let
6266
6367||| Count the number of occurances
6468count : k -> SortedBag k -> Nat
65−count k (BagWrapper m) = sum $ fst <$> lookup k m
69+count k (BagWrapper m) = countMap k m
6670
6771||| Is a count non-zero?
6872contains : k -> SortedBag k -> Bool
69−contains = isZero ... count
73+contains = isSucc ... count
7074
7175||| insert a given number of items
7276insertN : Nat -> UpdateBag k