From edc81ffee27e489453cbd599f9ec95a3071ddb5a Mon Sep 17 00:00:00 2001 From: Sam Truzjan Date: Mon, 30 Sep 2013 06:46:33 +0400 Subject: Add a couple of functions to BDict --- src/Data/BEncode/BDict.hs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/Data/BEncode/BDict.hs b/src/Data/BEncode/BDict.hs index 4ff5caa..d1d5215 100644 --- a/src/Data/BEncode/BDict.hs +++ b/src/Data/BEncode/BDict.hs @@ -17,6 +17,8 @@ module Data.BEncode.BDict , Data.BEncode.BDict.singleton -- * Query + , Data.BEncode.BDict.null + , Data.BEncode.BDict.member , Data.BEncode.BDict.lookup -- * Combine @@ -81,6 +83,21 @@ singleton :: BKey -> a -> BDictMap a singleton k v = Cons k v Nil {-# INLINE singleton #-} +-- | /O(1)/. Is the dictionary empty? +null :: BDictMap a -> Bool +null Nil = True +null _ = False +{-# INLINE null #-} + +-- | /O(n)/. Is the key a member of the dictionary? +member :: BKey -> BDictMap a -> Bool +member key = go + where + go Nil = False + go (Cons k _ xs) + | k == key = True + | otherwise = go xs + -- | /O(n)/. Lookup the value at a key in the dictionary. lookup :: BKey -> BDictMap a -> Maybe a lookup x = go -- cgit v1.2.3