expiring-containers 0.2.1 → 0.2.2
raw patch · 5 files changed
+33/−5 lines, 5 filessetup-changed
Files
- Setup.hs +0/−2
- expiring-containers.cabal +1/−1
- library/ExpiringContainers/ExpiringMap.hs +16/−0
- library/ExpiringContainers/ExpiringSet.hs +2/−2
- test/Main.hs +14/−0
− Setup.hs
@@ -1,2 +0,0 @@-import Distribution.Simple-main = defaultMain
expiring-containers.cabal view
@@ -1,7 +1,7 @@ name: expiring-containers version:- 0.2.1+ 0.2.2 synopsis: Expiring containers category:
library/ExpiringContainers/ExpiringMap.hs view
@@ -15,6 +15,7 @@ -- * Transformations map, mapWithKey,+ traverseWithKey, -- * Basic interface null,@@ -22,6 +23,7 @@ member, insert, delete,+ lookupWithTime, ) where @@ -44,6 +46,12 @@ (HashMap.HashMap key value) deriving (Eq, Foldable, Show) +instance Functor (ExpiringMap key) where+ fmap = map++instance Traversable (ExpiringMap key) where+ traverse f = traverseWithKey (const f)+ {-------------------------------------------------------------------- Transformations --------------------------------------------------------------------}@@ -55,6 +63,10 @@ mapWithKey f (ExpiringMap expiringSet hashMap) = ExpiringMap expiringSet $ HashMap.mapWithKey f hashMap +traverseWithKey :: Applicative f => (k -> v1 -> f v2) -> ExpiringMap k v1+ -> f (ExpiringMap k v2)+traverseWithKey f (ExpiringMap expiringSet hashMap) = ExpiringMap expiringSet <$> HashMap.traverseWithKey f hashMap+ {-------------------------------------------------------------------- Lists --------------------------------------------------------------------}@@ -116,3 +128,7 @@ where (keys, newExpSet) = ExpiringSet.clean time expSet newHashMap = List.foldl' (flip HashMap.delete) hashMap keys++lookupWithTime :: (Eq k, Hashable k) => k -> ExpiringMap k v -> Maybe (v, UTCTime)+lookupWithTime key (ExpiringMap expSet hashMap) =+ HashMap.lookup key hashMap >>= (\v -> fmap ((,) v) $ ExpiringSet.lookup key expSet)
library/ExpiringContainers/ExpiringSet.hs view
@@ -50,7 +50,8 @@ (HashMap.HashMap element Int) deriving(Eq, Show, Generic) -+instance Foldable.Foldable ExpiringSet where+ foldr f b (ExpiringSet intMultimap _) = foldr f b $ IntMap.elems intMultimap {-------------------------------------------------------------------- Transformations@@ -74,7 +75,6 @@ mapKeys :: (Eq k2, Hashable k2) => (k1 -> k2) -> HashMap.HashMap k1 a -> HashMap.HashMap k2 a mapKeys f hashMap = HashMap.fromList $ fmap (first f) $ (HashMap.toList hashMap)- {-------------------------------------------------------------------- Lists
test/Main.hs view
@@ -94,6 +94,12 @@ , testProperty "lookup found" $ \ (list :: [(UTCTime, Int)], key :: UTCTime, value :: Int) -> (A.lookup value $ A.insertForce key value $ A.fromList list) === (Just $ timestampUtcTime $ utcTimeTimestamp key)+ ,+ testProperty "foldr" $ \ (list :: [(UTCTime, Int)], seed :: Int) ->+ (foldr (\a b -> a + b) seed $ A.fromList list) === (foldr (\(t, a) b -> a + b) seed $ A.toList $ A.fromList list)+ ,+ testProperty "foldMap" $ \ (list :: [(UTCTime, Int)]) ->+ (foldMap (show) $ A.fromList list) === (foldMap (\(t, a) -> show a) $ A.toList $ A.fromList list) ] expiringMap =@@ -176,6 +182,14 @@ -- , testProperty "member" $ \ (list :: [(UTCTime, Int, Int)], time :: UTCTime, key :: Int, value :: Int) -> (B.member key $ B.delete key $ B.insert time key value $ B.fromList list) === False+ ,+ testProperty "lookupWithTime" $ \ (list :: [(UTCTime, Int, Int)], key :: Int) ->+ let map' = B.fromList list+ in (B.lookupWithTime key map') === (foldr (\(t, k, v) m -> if k == key then (Just (v, t)) else m ) Nothing $ B.toList $ B.fromList list)+ ,+ testProperty "traverse" $ \ (list :: [(UTCTime, Int, Int)]) ->+ let map' = B.fromList list + in (traverse (\a -> [a + 1]) map') === [fmap (\a -> a + 1) map'] -- , -- testProperty "insert and delete" $ \ (list :: [(UTCTime, Int, Int)], time :: UTCTime, key :: Int, value :: Int) -> -- let list2 = C.nubBy (\(t1,k1,v1) (t2,k2,v2) -> k1 == k2) list