Maplets

Maps are made of Maplets, so it makes sense to discuss Maplets first.

A Maplet is a Pair of information. We refer to this as a key-value pair, e.g. the key is associated with the value.

A Maplet is shown surrounded with parenthesis () containing the key-value pair separated by the Maplet operator ==>.

You can construct a Maplet in MillScript using the newMaplet function or ==> operator, e.g.

:-) newMaplet( "key", "value" );
There is 1 result
( key ==> value )
:-)
:-) "hello" ==> "there";
There is 1 result
( hello ==> there )
      

We can access the key and value of the Maplet by indexing it like a list of two items. Because MillScript uses 1 indexed lists, we can do the following:

:-) var m = newMaplet( "key", "value" );
There are 0 results
:-) m[1];
There is 1 result
"key"
:-) m[2];
There is 1 result;
"value"
      

Maps

The smallest component of a Map is the Maplet, a key that is associated with a value. The Map represents a collection of Maplets, where the collection is indexed by the Maplet keys.

New maps are constructed in similar ways, using the various newMap* functions. The new map functions can only accept maps, lists or maplets as their arguments and are treated as follows:

Map
All the mappings in the supplied map are copied into the new map
List
Each pair of elements in the list are used to initialize a mapping in the new map. e.g. the first item in the list becomes a key and it's associated value is the second item in the list.
Maplet
The maplets mapping is copied into the new map.

When a map is shown by MillScript, it is represented by a pair of curly brackets {}. Each key-value pair in the map is displayed as key=value separated by a comma ",".

The following example shows the different ways a map could be constructed and how they are displayed:

:-) newMap();
There is 1 result
{}
:-)
:-) newMap( "hello" ==> "there" );
There is 1 result
{hello=there}
:-)
:-) newMap( { "key", "value", "hello", "there" } );
There is 1 result
{key=value, hello=there}
:-)
:-) newMap( "key" ==> "value", "hello" ==> "there" );
There is 1 result
{key=value, hello=there}
:-)
:-) var a = newMap( "key" ==> "value" );
There are 0 results
:-) newMap( a, { "hello", "there" }, "a" ==> "b" );
There is 1 result
{key=value, hello=there, a=b}
      

The above examples show keys and values as Strings. The keys and values in a map can be any valid MillScript object, e.g. other Maps, Lists, XML, strings, numbers, ...

:-) newMap( "hello" ==> 1, "xml" ==> <xml><child/></xml> );
There is 1 result
{hello=1, xml=<xml><child/></xml>}