on ae@dev-script help sign in
reflex/src/Data/AppendMap.hs
84 lines 2.8 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 (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
35import qualified Data.Map.Monoidal as MonoidalMap
38{-# DEPRECATED AppendMap "Use 'MonoidalMap' instead" #-}
39-- | AppendMap is a synonym for 'Data.Map.Monoidal.MonoidalMap'
40type AppendMap = MonoidalMap
42{-# DEPRECATED _unAppendMap "Use 'getMonoidalMap' instead" #-}
43-- | A synonym for 'getMonoidalMap'
44_unAppendMap :: MonoidalMap k v -> Map k v
45_unAppendMap = getMonoidalMap
47-- | Pattern synonym for 'MonoidalMap'
48pattern AppendMap :: Map k v -> MonoidalMap k v
49pattern AppendMap m = MonoidalMap m
51#if !MIN_VERSION_witherable(0,3,2)
52instance W.Filterable (MonoidalMap k) where
53 mapMaybe = MonoidalMap.mapMaybe
54#endif
56-- | Deletes a key, returning 'Nothing' if the result is empty.
57nonEmptyDelete :: Ord k => k -> MonoidalMap k a -> Maybe (MonoidalMap k a)
58nonEmptyDelete k vs =
59 let deleted = delete k vs
60 in if null deleted
61 then Nothing
62 else Just deleted
64-- | Like 'mapMaybe' but indicates whether the resulting container is empty
65mapMaybeNoNull :: (a -> Maybe b)
66 -> MonoidalMap token a
67 -> Maybe (MonoidalMap token b)
68mapMaybeNoNull f as =
69 let bs = mapMaybe f as
70 in if null bs
71 then Nothing
72 else Just bs
74-- TODO: Move instances to `Data.Patch`
75-- | Displays a 'MonoidalMap' as a tree. See 'Data.Map.Lazy.showTree' for details.
76showTree :: forall k a. (Show k, Show a) => MonoidalMap k a -> String
77showTree = coerce (Map.showTree :: Map k a -> String)
79-- | Displays a 'MonoidalMap' as a tree, using the supplied function to convert nodes to string.
80showTreeWith :: forall k a. (k -> a -> String) -> Bool -> Bool -> MonoidalMap k a -> String
81showTreeWith = coerce (Map.showTreeWith :: (k -> a -> String) -> Bool -> Bool -> Map k a -> String)
83instance Default (MonoidalMap k a) where
84 def = empty