Table of Contents

map

A map is an ordered key/value object.

Create a map with `map(…)`.

lookup = map();
lookup = map('x', 10, 'y', 20);

Keys are converted to strings.

Constructor

map()

Creates an empty map.

Item Description
Syntax `map()`
Arguments none
Returns map object

Example:

lookup = map();
lookup.empty();       // true

map(key, value, ...)

Creates a map initialized with key/value pairs.

Argument Type Description
`key` any Key converted to string.
`value` any Value stored for the key.
`…` any Additional key/value pairs.

Returns: map object.

The number of arguments must be even.

Example:

lookup = map('x', 10, 'y', 20);
lookup.get('x');      // 10

Properties

Map keys can be read as properties when the key is a valid property name.

lookup = map('x', 10);
lookup.x;             // 10

Property access is read-only. Use `set(key, value)` to change a map entry.

lookup.set('x', 20);

Common Object Methods

to_string()

Returns a display string for the object.

Item Description
Syntax `value.to_string()`
Arguments none
Returns string

Example:

text = value.to_string();

clone()

Returns a new map object with the same key/value pairs.

Item Description
Syntax `map.clone()`
Arguments none
Returns map object

Example:

lookup = map('x', 10);
copy = lookup.clone();

Methods

size()

Returns the number of key/value pairs in the map.

Item Description
Syntax `map.size()`
Arguments none
Returns number

Example:

map('x', 10, 'y', 20).size();  // 2

count()

Alias for `size()`.

Item Description
Syntax `map.count()`
Arguments none
Returns number

Example:

map('x', 10).count();          // 1

empty()

Returns true when the map has no entries.

Item Description
Syntax `map.empty()`
Arguments none
Returns boolean-style number

Example:

lookup = map();
lookup.empty();                // true

clear()

Removes all entries from the map.

Item Description
Syntax `map.clear()`
Arguments none
Returns boolean-style number

Example:

lookup = map('x', 10);
lookup.clear();
lookup.size();                 // 0

has(key)

Checks whether a key exists.

Argument Type Description
`key` any Key converted to string.

Returns: boolean-style number.

Example:

lookup = map('x', 10);
lookup.has('x');               // true
lookup.has('z');               // false

get(key)

Returns the value for a key.

Argument Type Description
`key` any Key converted to string.

Returns: stored value, or `none()` when the key does not exist.

Example:

lookup = map('x', 10);
lookup.get('x');               // 10
lookup.get('z').is_none();     // true

set(key, value)

Stores a value for a key.

Argument Type Description
`key` any Key converted to string.
`value` any Value to store.

Returns: boolean-style number.

Example:

lookup = map();
lookup.set('x', 10);
lookup.get('x');               // 10

remove(key)

Removes a key/value pair.

Argument Type Description
`key` any Key converted to string.

Returns: true if a value was removed, false when the key did not exist.

Example:

lookup = map('x', 10);
lookup.remove('x');            // true
lookup.remove('x');            // false

keys()

Returns an array of keys in insertion order.

Item Description
Syntax `map.keys()`
Arguments none
Returns array object

Example:

lookup = map('x', 10, 'y', 20);
lookup.keys().to_string();     // ["x", "y"]

values()

Returns an array of values in insertion order.

Item Description
Syntax `map.values()`
Arguments none
Returns array object

Example:

lookup = map('x', 10, 'y', 20);
lookup.values().to_string();   // [10, 20]

Previous: array

Next: bytes