Commits

FlorianHartwig  committed 6a850eb

Add two convenience functions, remove some unnecessary parentheses

  • Participants
  • Parent commits c223acf

Comments (0)

Files changed (2)

File src/Data/AttoBencode.hs

     , ToBencode(..)
     -- * Convencience Functions
     , (.:)
+    , dict
+    , (.=)
     ) where
 
 import Data.AttoBencode.Types

File src/Data/AttoBencode/Types.hs

     , FromBencode(..)
     , ToBencode(..)
     , (.:)
+    , dict
+    , (.=)
     ) where
 
 import qualified Data.Map as M
 instance ToBencode Int where
     toBencode = BInt . fromIntegral
 
-instance (ToBencode a) => ToBencode [a] where
+instance ToBencode a => ToBencode [a] where
     toBencode = BList . map toBencode
 
-instance (ToBencode a) => ToBencode (M.Map ByteString a) where
+instance ToBencode a => ToBencode (M.Map ByteString a) where
     toBencode = BDict . M.map toBencode
 
-instance (ToBencode a) => ToBencode [(ByteString, a)] where
+instance ToBencode a => ToBencode [(ByteString, a)] where
     toBencode = BDict . M.fromList . map (\(k, v) -> (k, toBencode v))
 
 instance ToBencode BValue where
 -- | Look up the value corresponding to a (ByteString) key from a dictionary.
 --   Returns 'Nothing' if the key is not in the dictionary or if the 'BValue'
 --   cannot be converted to the expected type.
-(.:) :: (FromBencode a) => Dict -> ByteString -> Maybe a
+(.:) :: FromBencode a => Dict -> ByteString -> Maybe a
 d .: s = M.lookup s d >>= fromBencode
+
+-- | Make a BDict from a list of (key, value) tuples.
+dict :: [(ByteString, BValue)] -> BValue
+dict = BDict . M.fromList
+
+-- | Create a (key, value) tuple from a ByteString key and some bencode-able
+--   value. Can be used with the 'dict' function as a convenient way to create
+--   'BDict's.
+(.=) :: ToBencode a => ByteString -> a -> (ByteString, BValue)
+key .= value = (key, toBencode value)