1{-# LANGUAGE AllowAmbiguousTypes #-}
2{-# LANGUAGE Rank2Types #-}
5'TMap' is a heterogeneous data structure similar in its essence to
6'Data.Map.Map' with types as keys, where each value has the type of its key.
8Here is an example of a 'TMap' with a comparison to 'Data.Map.Map':
11 'Data.Map.Map' 'Prelude.String' 'Prelude.String' 'TMap'
12-------------------- -----------------
13 \"Int\" -> \"5\" 'Prelude.Int' -> 5
14 \"Bool\" -> \"True\" 'Prelude.Bool' -> 'Prelude.True'
15 \"Char\" -> \"\'x\'\" 'Prelude.Char' -> \'x\'
18The runtime representation of 'TMap' is an array, not a tree. This makes
19'lookup' significantly more efficient.
46import Prelude hiding (lookup, map)
48import Data.Functor.Identity (Identity (..))
49import Data.Typeable (Typeable)
50import GHC.Exts (coerce)
51import Type.Reflection (SomeTypeRep)
53import qualified Data.TypeRepMap as F
55-- | 'TMap' is a special case of 'F.TypeRepMap' when the interpretation is
57type TMap = F.TypeRepMap Identity
61A 'TMap' with no values stored in it.
64prop> member @a empty == False
73Construct a 'TMap' with a single element.
75prop> size (one x) == 1
76prop> member @a (one (x :: a)) == True
79one :: forall a . Typeable a => a -> TMap
80one x = coerce (F.one @a @Identity $ coerce x)
85Insert a value into a 'TMap'.
87prop> size (insert v tm) >= size tm
88prop> member @a (insert (x :: a) tm) == True
91insert :: forall a . Typeable a => a -> TMap -> TMap
92insert x = coerce (F.insert @a @Identity $ coerce x)
95{- | Delete a value from a 'TMap'.
97prop> size (delete @a tm) <= size tm
98prop> member @a (delete @a tm) == False
100>>> tm = delete @Bool $ insert True $ one 'a'
108delete :: forall a . Typeable a => TMap -> TMap
109delete = F.delete @a @Identity
112-- | The union of two 'TMap's using a combining function.
113unionWith :: (forall x. x -> x -> x) -> TMap -> TMap -> TMap
114unionWith f = F.unionWith fId
116 fId :: forall y . Identity y -> Identity y -> Identity y
117 fId y1 y2 = Identity $ f (coerce y1) (coerce y2)
118{-# INLINE unionWith #-}
120-- | The (left-biased) union of two 'TMap's. It prefers the first map when
121-- duplicate keys are encountered, i.e. @'union' == 'unionWith' const@.
122union :: TMap -> TMap -> TMap
126{- | Lookup a value of the given type in a 'TMap'.
128>>> x = lookup $ insert (11 :: Int) empty
134lookup :: forall a. Typeable a => TMap -> Maybe a
135lookup = coerce (F.lookup @a @Identity)
138{- | Check if a value of the given type is present in a 'TMap'.
140>>> member @Char $ one 'a'
142>>> member @Bool $ one 'a'
145member :: forall a . Typeable a => TMap -> Bool
146member = F.member @a @Identity
149-- | Get the amount of elements in a 'TMap'.
154-- | Returns the list of 'SomeTypeRep's from keys.
155keys :: TMap -> [SomeTypeRep]
159-- | Map a function over the values.
160map :: (forall a. Typeable a => a -> a) -> TMap -> TMap
161map f = F.hoistWithKey (liftToIdentity f)
164-- | Update a value with the result of the provided function.
165adjust :: Typeable a => (a -> a) -> TMap -> TMap
166adjust f = F.adjust (liftToIdentity f)
169liftToIdentity :: forall a. Typeable a => (a -> a) -> Identity a -> Identity a
170liftToIdentity = coerce
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list