map

type map<K: -immutable, V> : iterable<(K, V)>

A mutable map from keys of type K to values of type V, where K is immutable, and V may be either mutable or immutable. map<K,V> is a subtype of iterable<(K,V)>`. It is implemented as a hash-map, with iteration order determined by the order in which the entries were added.

Since

0.6.0

See also

Constructors

Link copied to clipboard
pure constructor()

Construct a new empty map.

pure constructor(entries: iterable<-map_entry<K,V>>)

Construct a new map by copying the entries from another iterable. The provided entries are an iterable with element type map_entry<K,V>; a supertype of the two-element tuple (K,V).

Functions

Link copied to clipboard
function clear()

Clear this map; i.e. remove all its entries. Immediately after this method returns, this map is empty.

Link copied to clipboard
pure function contains(key: K): boolean

Check if this map contains the given key.

Link copied to clipboard
pure function empty(): boolean

Check if this map is empty.

Link copied to clipboard
pure function get(key: K): V

Get the value associated with the given key in this map.

Link copied to clipboard
pure function <R: +V> get_or_default(key: K, default: R): R

Get the value associated with the given key in this map.

Link copied to clipboard
pure function get_or_null(key: K): V?

Get the value associated with the given key in this map.

Link copied to clipboard
pure function join_to_text([separator: text], [prefix: text], [postfix: text], [limit: integer?], [truncated: text], [transform: ((K, V)) -> text]): text

Generate a textual representation of this iterable.

An optional separator, prefix and postfix can be provided. One can also provide a limit: integer?. If there are more elements in the result than limit, the elements whose indices exceed limit are omitted, and the passed truncated: text is included instead.

Examples:

  • [1, 2, 3].join_to_text() returns '1, 2, 3'.

  • [1, 2, 3].join_to_text('_') returns '1_2_3'.

  • [1, 2, 3].join_to_text('*', '(', ')') returns '(1*2*3)'.

  • list<T>().join_to_text('!', '(', ')') returns '()' (where T is a valid type).

  • range(10).join_to_text('', '', '', 5) returns '01234...'.

  • range(10).join_to_text('', '', '', 5, 'more') returns '01234more'.

Where the function even is defined:

function even(x: integer): text {
return if (x % 2 == 0) 'EVEN' else 'ODD';
}

Then:

  • range(10).join_to_text('->', '{', '}', 5, '...', even(*)) returns {EVEN->ODD->EVEN->ODD->EVEN->...}.

Link copied to clipboard
pure function keys(): set<K>

Returns a set containing the keys of this map.

Examples:

  • [1: 'a', 2: 'b', 3: 'c'].keys() returns set([1, 2, 3]).

  • map<K,V>().keys() returns set() (where K and V are valid types).

Link copied to clipboard
(alias) pure function len(): integer

Get the size (number of entries) of this map.

Alias
Link copied to clipboard
function put(key: K, value: V)

Associate the specified value with the specified key in this map.

Link copied to clipboard
function put_all(map: map<-K, -V>)

Update this map with the entries from another map. Where a key is found in both this and the passed map, the corresponding value in the passed map overwrites the value in this map. In other words, this is a right-biased operation.

Link copied to clipboard
(alias) function putAll(map: map<-K, -V>)

Update this map with the entries from another map. Where a key is found in both this and the passed map, the corresponding value in the passed map overwrites the value in this map. In other words, this is a right-biased operation.

Alias
Link copied to clipboard
function remove(key: K): V

Remove an entry from this map.

Link copied to clipboard
function remove_or_null(key: K): V?

Remove an entry from this map.

Link copied to clipboard
pure function size(): integer

Get the size (number of entries) of this map.

Link copied to clipboard
(alias) pure function str(): text

Returns a textual representation of this map.

Alias
Link copied to clipboard
pure function to_text(): text

Returns a textual representation of this map.

Link copied to clipboard
pure function values(): list<V>

Returns a list containing the values of this map.

Examples:

  • [1: 'a', 2: 'b', 3: 'c'].values() returns ['a', 'b', 'c'].

  • map<K,V>().values() returns [] (where K and V are valid types).