Dashboard > Mantra > ... > Types > Collections
  Mantra Log In | Sign Up   View a printable version of the current page.  
  Collections
Added by Terence Parr, last edited by Terence Parr on Nov 03, 2007  (view change)
Labels: 
(None)

Mantra has built-in list and dict collections and associated literals:

list filenames = ["t.html", "u.html"];
list states = [2,9,1];
dict phones = ["Ter"="x5707", "Mark"="x5135"];

Note that "=" is overloaded (means assignment also). It is unambiguous, however.

The array index operator has been overloaded for index lists and dicts:

n = names[i];
p = phones[name];

Indexes start at 0 for lists.

For dict, of course, a[x] invokes the a.get(x) method.

Lists and dicts grow to include new members. If index 42 does not exist in a list, the list grows to be large enough so that the following does not cause a runtime exception:

names[42] = "Ter";

The same is true for dicts:

phones["Ter"] = "x5707";

Sample:

dict m = dict();
m["Ter"] = 1;    
m["Tom"] = 3;
m["Able"] = 9;
m.keys():{n | print(m[n]); print(" "); println(n);};
list keyList = m.keys().toList();
println(keyList);

Naturally, lists and dicts and sets can all be nested, which also highlights that lists are heterogenous.

a = [1,2,["A","B"],[1=2,2="Tom"]];

OrderedDict is a dict where the order of addition is remember.

The range operator ".." yields an interval that can be used to select a contiguous subset of a list yielding a stream object:

range first5 = 0..4;
first5names = names[first5];

without copying the values. As a special case, sets may be used as list or dict indices to indicate a noncontiguous set of elements:

list goodones = [1,99,20];
subset = names[goodones]; // yields an InputStream

Tree example:

tree t = ^("hi" 34 Y(10) ^(99 32)); // where Y is a class
println(t.toStringTree());
Site powered by a free Open Source Project / Non-profit License (more) of Confluence - the Enterprise wiki.
Learn more or evaluate Confluence for your organisation.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.1 Build:#806 May 06, 2007) - Bug/feature request - Contact Administrators