on ce42607 help sign in
44 lines 1.4 kB history raw
1{-# LANGUAGE FlexibleInstances #-}
4 -- Module : Gen.Types.Map
5-- Copyright : (c) 2013-2015 Brendan Hay
6-- License : This Source Code Form is subject to the terms of
7-- the Mozilla Public License, v. 2.0.
8-- A copy of the MPL can be found in the LICENSE file or
9-- you can obtain it at http://mozilla.org/MPL/2.0/.
10-- Maintainer : Brendan Hay <brendan.g.hay@gmail.com>
11-- Stability : provisional
12-- Portability : non-portable (GHC extensions)
14module Gen.Types.Map where
16import Control.Lens
17import Data.Aeson
18import Data.Bifunctor
19import Data.Hashable
20import qualified Data.HashMap.Strict as Map
21import Data.Maybe
22import Data.Text (Text)
24type Map = Map.HashMap
26vMapMaybe :: (Eq k, Hashable k)
27 => (a -> Maybe b)
28 -> Map k a
29 -> Map k b
30vMapMaybe f = runIdentity . kvTraverseMaybe (const (pure . f))
32kvTraverseMaybe :: (Applicative f, Eq k, Hashable k)
33 => (k -> a -> f (Maybe b))
34 -> Map k a
35 -> f (Map k b)
36kvTraverseMaybe f = fmap (Map.map fromJust . Map.filter isJust)
37 . Map.traverseWithKey f
39class (Hashable a, Eq a) => TextKey a where
40 toKey :: Text -> a
42instance (TextKey k, FromJSON v) => FromJSON (Map k v) where
43 parseJSON = fmap (Map.fromList . map (first toKey) . Map.toList)
44 . parseJSON