Skip to content

Commit

Permalink
add Aeson instances to MonoidalMap (issue #21)
Browse files Browse the repository at this point in the history
  • Loading branch information
danbornside committed Aug 30, 2018
1 parent f8a7913 commit 0909c4d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Data/Map/Monoidal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE StandaloneDeriving #-}

-- | This module provides a 'Data.Map' variant which uses the value's
-- 'Monoid' instance to accumulate conflicting entries when merging
Expand Down Expand Up @@ -144,13 +145,24 @@ import Control.DeepSeq
import qualified Data.Map as M
import Control.Lens
import Control.Newtype
import Data.Aeson(FromJSON, ToJSON, FromJSON1, ToJSON1)
#if MIN_VERSION_containers(0,5,9)
import Data.Functor.Classes
#endif

-- | A 'Map' with monoidal accumulation
newtype MonoidalMap k a = MonoidalMap { getMonoidalMap :: M.Map k a }
deriving (Show, Read, Functor, Eq, Ord, NFData,
Foldable, Traversable,
FromJSON, ToJSON, FromJSON1, ToJSON1,
Data, Typeable)

#if MIN_VERSION_containers(0,5,9)
deriving instance (Ord k) => Eq1 (MonoidalMap k)
deriving instance (Ord k) => Ord1 (MonoidalMap k)
deriving instance (Show k) => Show1 (MonoidalMap k)
#endif

type instance Index (MonoidalMap k a) = k
type instance IxValue (MonoidalMap k a) = a
instance Ord k => Ixed (MonoidalMap k a) where
Expand Down
12 changes: 12 additions & 0 deletions src/Data/Map/Monoidal/Strict.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}

-- | This module provides a 'Data.Map' variant which uses the value's
-- 'Monoid' instance to accumulate conflicting entries when merging
Expand Down Expand Up @@ -144,13 +145,24 @@ import Control.DeepSeq
import qualified Data.Map.Strict as M
import Control.Lens
import Control.Newtype
import Data.Aeson(FromJSON, ToJSON, FromJSON1, ToJSON1)
#if MIN_VERSION_containers(0,5,9)
import Data.Functor.Classes
#endif

-- | A 'Map' with monoidal accumulation
newtype MonoidalMap k a = MonoidalMap { getMonoidalMap :: M.Map k a }
deriving (Show, Read, Functor, Eq, Ord, NFData,
Foldable, Traversable,
FromJSON, ToJSON, FromJSON1, ToJSON1,
Data, Typeable)

#if MIN_VERSION_containers(0,5,9)
deriving instance (Ord k) => Eq1 (MonoidalMap k)
deriving instance (Ord k) => Ord1 (MonoidalMap k)
deriving instance (Show k) => Show1 (MonoidalMap k)
#endif

type instance Index (MonoidalMap k a) = k
type instance IxValue (MonoidalMap k a) = a
instance Ord k => Ixed (MonoidalMap k a) where
Expand Down

0 comments on commit 0909c4d

Please sign in to comment.