on 7ae1e23 help sign in
patch/src/Data/AppendMap.hs
82 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 (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
49#if !MIN_VERSION_witherable(0,3,2)
50instance W.Filterable (MonoidalMap k) where
51 mapMaybe = mapMaybe
52#endif
54-- | Deletes a key, returning 'Nothing' if the result is empty.
55nonEmptyDelete :: Ord k => k -> MonoidalMap k a -> Maybe (MonoidalMap k a)
56nonEmptyDelete k vs =
57 let deleted = delete k vs
58 in if null deleted
59 then Nothing
60 else Just deleted
62-- | Like 'mapMaybe' but indicates whether the resulting container is empty
63mapMaybeNoNull :: (a -> Maybe b)
64 -> MonoidalMap token a
65 -> Maybe (MonoidalMap token b)
66mapMaybeNoNull f as =
67 let bs = mapMaybe f as
68 in if null bs
69 then Nothing
70 else Just bs
72-- TODO: Move instances to `Reflex.Patch`
73-- | Displays a 'MonoidalMap' as a tree. See 'Data.Map.Lazy.showTree' for details.
74showTree :: forall k a. (Show k, Show a) => MonoidalMap k a -> String
75showTree = coerce (Map.showTree :: Map k a -> String)
77-- | Displays a 'MonoidalMap' as a tree, using the supplied function to convert nodes to string.
78showTreeWith :: forall k a. (k -> a -> String) -> Bool -> Bool -> MonoidalMap k a -> String
79showTreeWith = coerce (Map.showTreeWith :: (k -> a -> String) -> Bool -> Bool -> Map k a -> String)
81instance Default (MonoidalMap k a) where
82 def = empty