on 089d4e1 help sign in
patch/src/Data/AppendMap.hs
80 lines 2.7 kB history raw
1{-# LANGUAGE CPP #-}
2{-# LANGUAGE FlexibleContexts #-}
3{-# LANGUAGE FlexibleInstances #-}
4{-# LANGUAGE MultiParamTypeClasses #-}
5{-# LANGUAGE PatternSynonyms #-}
6{-# LANGUAGE RoleAnnotations #-}
7{-# LANGUAGE ScopedTypeVariables #-}
8{-# LANGUAGE TypeFamilies #-}
9{-# OPTIONS_GHC -fno-warn-orphans #-}
10-- |
11-- Module:
12-- Data.AppendMap
13-- Description:
14-- Instances and convenience functions for 'Data.Map.Monoidal'. We use
15-- monoidal-containers to take advantage of its better monoid instance.
16-- 'Data.Map' has @mappend = union@, which is left-biased. 'MonoidalMap'
17-- has @mappend = unionWith mappend@ instead.
18module Data.AppendMap
19 ( module Data.AppendMap
20 , module Data.Map.Monoidal
21 ) where
23import Prelude hiding (map, null)
25import Data.Coerce
26import Data.Default
27import Data.Map (Map)
28#if MIN_VERSION_containers(0,5,11)
29import qualified Data.Map.Internal.Debug as Map (showTree, showTreeWith)
30#else
31import qualified Data.Map as Map (showTree, showTreeWith)
32#endif
33import qualified Data.Witherable as W
34import Data.Map.Monoidal
36{-# DEPRECATED AppendMap "Use 'MonoidalMap' instead" #-}
37-- | AppendMap is a synonym for 'Data.Map.Monoidal.MonoidalMap'
38type AppendMap = MonoidalMap
40{-# DEPRECATED _unAppendMap "Use 'getMonoidalMap' instead" #-}
41-- | A synonym for 'getMonoidalMap'
42_unAppendMap :: MonoidalMap k v -> Map k v
43_unAppendMap = getMonoidalMap
45-- | Pattern synonym for 'MonoidalMap'
46pattern AppendMap :: Map k v -> MonoidalMap k v
47pattern AppendMap m = MonoidalMap m
49instance W.Filterable (MonoidalMap k) where
50 mapMaybe = mapMaybe
52-- | Deletes a key, returning 'Nothing' if the result is empty.
53nonEmptyDelete :: Ord k => k -> MonoidalMap k a -> Maybe (MonoidalMap k a)
54nonEmptyDelete k vs =
55 let deleted = delete k vs
56 in if null deleted
57 then Nothing
58 else Just deleted
60-- | Like 'mapMaybe' but indicates whether the resulting container is empty
61mapMaybeNoNull :: (a -> Maybe b)
62 -> MonoidalMap token a
63 -> Maybe (MonoidalMap token b)
64mapMaybeNoNull f as =
65 let bs = mapMaybe f as
66 in if null bs
67 then Nothing
68 else Just bs
70-- TODO: Move instances to `Reflex.Patch`
71-- | Displays a 'MonoidalMap' as a tree. See 'Data.Map.Lazy.showTree' for details.
72showTree :: forall k a. (Show k, Show a) => MonoidalMap k a -> String
73showTree = coerce (Map.showTree :: Map k a -> String)
75-- | Displays a 'MonoidalMap' as a tree, using the supplied function to convert nodes to string.
76showTreeWith :: forall k a. (k -> a -> String) -> Bool -> Bool -> MonoidalMap k a -> String
77showTreeWith = coerce (Map.showTreeWith :: (k -> a -> String) -> Bool -> Bool -> Map k a -> String)
79instance Default (MonoidalMap k a) where
80 def = empty