Module Map

This module is undocumented. This is a list of its definitions.

containsKey :: Map.T a b -> a -> Boolean

Returns True if the map contains the given key.

containsValue :: Map.T a b -> b -> Boolean

Returns True if the map contains the given value.

entrySet :: Map.T a b -> Set.T (Entry a b)

The entries of the map as a set. The entry type is MMap.Entry, from another module: take an entry apart with MMap.keyOf and MMap.valueOf.

Example:

> import "Map" as Map
> m = Map.freeze (MMap.fromEntryList [("a", 1), ("b", 2)])
> es = Set.toList (Map.entrySet m)
> sort (map (\entry -> (MMap.keyOf entry, MMap.valueOf entry)) es)
[("a", 1), ("b", 2)]

The result is sorted because the iteration order of the set is not specified.

fold :: (a -> b -> c -> <d> a) -> a -> Map.T b c -> <d> a

Folds over all key-value pairs of the map starting with the given initial value. The folded function receives the accumulator first, then the key, then the value.

Because the iteration order of the map is not specified, the folded function should not depend on it.

Example:

> import "Map" as Map
> m = Map.freeze (MMap.fromEntryList [("a", 1), ("b", 2)])
> Map.fold (\accum k v -> accum + v) 0 m
3
freeze :: MMap.T a b -> <Proc> Map.T a b

Converts the mutable map into an immutable one without copying the underlying data. This only changes how the structure is treated by the SCL type system; it is an O(1) operation, not a copy. The original mutable map must not be used anymore after this.

This is the only way to construct a Map.T: the module has no fromList, put or remove. Fill an MMap first and freeze it. Note that the function lives in this module but takes an MMap.T, so it is called Map.freeze, not MMap.freeze, and it is the only <Proc> function here.

Example:

> import "Map" as Map
> mm = MMap.create () :: <Proc> MMap.T String Integer
> MMap.put mm "a" 1
Nothing
> MMap.put mm "b" 2
Nothing
> m = Map.freeze mm
> Map.size m
2
> Map.get m "a"
Just 1
get :: Map.T a b -> a -> Maybe b

The value associated with the given key, or Nothing if the map contains no such key.

isEmpty :: Map.T a b -> Boolean

Returns True if the map contains no entries.

iter :: (a -> b -> <c> ()) -> Map.T a b -> <c> ()

Calls the given function with all key-value pairs of the map.

iterB :: (a -> b -> <c> Boolean) -> Map.T a b -> <c> Boolean

Calls the given function with the key-value pairs of the map until it returns False. Returns False if the iteration was interrupted this way and True if the function accepted all entries. The returned boolean therefore answers "was the iteration allowed to finish", not "did any entry match".

See Set.iterB for a worked example of the return value.

keySet :: Map.T a b -> Set.T a

The keys of the map as a set.

size :: Map.T a b -> Integer

The number of entries in the map.

unsafeGet :: Map.T a b -> a -> b

Like get, but returns the value directly instead of wrapping it in a Maybe. If the map contains no such key, the result is a raw Java null.

Nothing in the type or in the effect signature warns about this: the null is returned silently and usually surfaces as a NullPointerException in unrelated code much later. Use get unless the key is known to be present.

Example:

> import "Map" as Map
> m = Map.freeze (MMap.fromEntryList [("a", 1)])
> Map.unsafeGet m "a"
1
> Map.get m "b"
Nothing

The last command deliberately uses get: Map.unsafeGet m "b" would return null instead of Nothing, and even printing that result fails with a NullPointerException.