Map

CSL has the built-in type Map k v which is a mapping between keys of type k and values of type v. For example, a map of type Map Int MySumType is a mapping between integers and some custom type MySumType.

Maps can only be constructed using the functions in the standard library – there are no constructors for Map. This also means that it is not possible to do pattern-matching on Map-values like it is with, e.g., List.

The simplest way to create a Map is by using the Map::fromList function from the standard library:

val myMap = Map::fromList [(1, "One"), (2, "Two"), (3, "Three")]
val oneString = Map::lookup 1 myMap
//            = Some "One"
val fourString = Map::lookup 4 myMap
//             = None

Maps are immutable: when you insert an element a new map is created and returned:

val myNewMap = Map::insert 4 "Four" myMap
val fourString2 = Map::lookup 4 myNewMap
//              = Some 4
val fourString3 = Map::lookup 4 myMap
//              = None (Unchanged from above)

Note

A map is unordered: when you iterate over the elements in a map using Map::fold you should not make any assumptions about the order the elements are returned. Elements will be returned in some order but this order should not be assigned any special significance.

Map::empty : Map k v

Returns a new empty map.

Map::insert : k -> v -> Map k v -> Map k v

Returns a new map that contains, in addition to the keys already present in the argument map, the given key/value.

Examples

val noElms = Map::empty
val oneElm = Map::insert 1 "One" noElms
val twoElms = Map::insert 2 "Two" oneElm

Map::remove : k -> Map k v -> Map k v

Returns a new map with the given key removed.

Examples

val myMap = Map::insert 1 "One" Map::empty
val myEmptyMap = Map::remove 1 myMap
//             = Map::empty
val myMap2 = Map::remove 2 myMap
//         = myMap

Map::lookup : k -> Map k v -> Maybe v

Returns the value at the given key, if the key is in the map. Otherwise it returns None.

Examples

val myMap = Map::insert 1 "One" Map::empty
val oneVal = Map::lookup 1 myMap
//         = Just "One"
val twoVal = Map::lookup 2 myMap
//         = None

Map::lookupOrDefault : v -> k -> Map k v -> Maybe v

Returns the value at the given key if the key is in the map. Otherwise returns the given default value..

Examples

val myMap = Map::insert 1 "One" Map::empty
val oneVal = Map::lookupOrDefault "Nothing" 1 myMap
//          = "One"
val twoVal = Map::lookupOrDefault "Nothing" 2 myMap
//         = "Nothing"

Map::fold : (k -> v -> a -> a) -> a -> Map k v -> a

Fold over the key/value pairs in a map. As mentioned above, maps are unordered.

Examples

val sumKeysAndVals = Map::fold (\k -> \v -> \counter -> k + v + counter) 0
val x = sumKeysAndVals (Map::fromList [(1, 10), (2, 20)])
//    = 1 + 10 + 2 + 20

Map::fromList : List (Tuple k v) -> Map k v

Constructs a map that contains the elements in the given list.

Examples

val prices = Map::fromList [("hammer", 10), ("saw", 12), ("axe", 15)]

Map::toList : Map k v -> List (Tuple k v)

The Map::toList function constructs a list that contains the pairs of (key, value) in the given map. As mentioned above, maps are unordered, so the order of elements of the original list is not guaranteed to be preserved.

Examples

val prices = Map::fromList [("hammer", 10), ("saw", 12), ("axe", 15)]
val priceList = Map::toList prices
//   = [("axe", 15), ("hammer", 10), ("saw", 12)]