Module DA.Map¶
Note: This is only supported in DAML-LF 1.11 or later.
This module exports the generic map type Map k v and associated
functions. This module should be imported qualified, for example:
import DA.Map (Map)
import DA.Map qualified as M
This will give access to the Map type, and the various operations
as M.lookup, M.insert, M.fromList, etc.
Map k v internally uses the built-in order for the type k.
This means that keys that contain functions are not comparable
and will result in runtime errors. To prevent this, the Ord k
instance is required for most map operations. It is recommended to
only use Map k v for key types that have an Ord k instance
that is derived automatically using deriving:
data K = ...
deriving (Eq, Ord)
This includes all built-in types that aren’t function types, such as
Int, Text, Bool, (a, b) assuming a and b have default
Ord instances, Optional t and [t] assuming t has a
default Ord instance, Map k v assuming k and v have
default Ord instances, and Set k assuming k has a
default Ord instance.
Functions¶
- fromListWith
: Ord k => (v -> v -> v) -> [(k, v)] -> Map k v
Create a map from a list of key/value pairs with a combining function. Examples:
>>> fromListWith (++) [("A", [1]), ("A", [2]), ("B", [2]), ("B", [1]), ("A", [3])] fromList [("A", [1, 2, 3]), ("B", [2, 1])] >>> fromListWith (++) [] == (empty : Map Text [Int]) True
- keys
: Map k v -> [k]
Get the list of keys in the map. Keys are sorted according to the built-in order for the type
k, which matches theOrd kinstance when usingderiving Ord.>>> keys (fromList [("A", 1), ("C", 3), ("B", 2)]) ["A", "B", "C"]
- values
: Map k v -> [v]
Get the list of values in the map. These will be in the same order as their respective keys from
M.keys.>>> values (fromList [("A", 1), ("B", 2)]) [1, 2]
- toList
: Map k v -> [(k, v)]
Convert the map to a list of key/value pairs. These will be ordered by key, as in
M.keys.
- filter
: Ord k => (v -> Bool) -> Map k v -> Map k v
Filter the
Mapusing a predicate: keep only the entries where the value satisfies the predicate.
- filterWithKey
: Ord k => (k -> v -> Bool) -> Map k v -> Map k v
Filter the
Mapusing a predicate: keep only the entries which satisfy the predicate.
- delete
: Ord k => k -> Map k v -> Map k v
Delete a key and its value from the map. When the key is not a member of the map, the original map is returned.
- insert
: Ord k => k -> v -> Map k v -> Map k v
Insert a new key/value pair in the map. If the key is already present in the map, the associated value is replaced with the supplied value.
- alter
: Ord k => (Optional v -> Optional v) -> k -> Map k v -> Map k v
Update the value in
matkwithf, inserting or deleting as required.fwill be called with either the value atk, orNoneif absent;fcan returnSomewith a new value to be inserted inm(replacing the old value if there was one), orNoneto remove anykassociationmmay have.Some implications of this behavior:
alter identity k = identity alter g k . alter f k = alter (g . f) k alter (_ -> Some v) k = insert k v alter (_ -> None) = delete