Dynamic collections are composite types. These collections are dynamic because we can add or remove elements very quick with minimum impact on memory relocation. Vectors and Matrices are simple and fast to access using index, but they have fixed capacity. When we resize a vector or a matrix we have to move all data into a new location. Dynamic collections can grow and shrink capacity very fast but searching elements in dynamic collections is slower.

Define

We define for Level these dynamic collections: {List, Map, Set }. For each of these collections we use a special literal inspired from mathematics notation. We have avoid to use {…} as code block to be able to use this notation for Map and Set collections.

List

A List is a consecutive sequence of elements having a dynamic capacity. The elements of a List must have the same type. For a list with n elements each element can be identified using an integer index in range (0..n-1). Unlike Julia where the range is (1..n). We have chosen this convention for compatibility in thinking with Java, Python and C++. Software developers count from 0 to 9.

Declaration syntax:

variable <list_name>: List of <type_name>;

Different Lists:

type
  Person: Record of (name: String, age: Integer);
variables
  n_list: List of Integer;
  s_list: List of String;
  p_list: List of Person;

List literals:

A list is similar to a mathematical set, except that it can contain duplicate elements. Also the elements of a list are ordered by a numeric subscript index. Initialization of a list can be done using define symbol “=” and List literals. String literals are comma separated enumerations of enclosed in square brackets: [,,,].

variable
  c_list = ['a','b','c','c','c'];
  n_list = [1,2,3,1,2,3];

Concatenation

List concatenation operator “&” can be used to concatenate two lists. Can be used to combine a string with a list. This will automatically convert the list elements  into coma separated string.

print ([1,2,3]  & [4,5]);   --will print:[1,2,3,4,5]
print ([1,2,3]  & "4,5");   --will print:"1,2,3,4,5"
print ("1,2,3," & [4,5]);   --will print:"1,2,3,4,5"

Joining

The join function receive a list and convert elements into a string separated be specified character. join(lst list, sep separator) result string;

let
  str:String;
  lst=[1,2,3]:List;
do
  str := list.join(lst,"-");
  print(str); --> "1-2-3"
end do;

Empty list

To create an empty list using type inference we can use empty list symbol [].
Elements of the list can be a created later using list operators.

c_list=[]:List of String;
n_list=[]:List of Integer;

List operations

We can add elements to a List or remove elements using methods.

  • append() – can append an element and return the index of the element
  • clear() – can delete all elements or a specific index element.
  • insert() – can insert an element at specified index

Other methods:

  • count() – retrieve the number of elements
  • first() – retrieve the first element
  • last() – retrieve the last element
  • next() – retrieve the next element
  • current() – retrieve the current element

List comprehension

You can define elements of a sub-list from a list using the following construction:

<sub_list> = [ <var> | <var> in <list_name> and <filter_expression>]

Symbol “|” is the filter operator and is derived from mathematics. We can use <var> to create the <filter_expression>.

Example of a list comprehension operation:

let
  my_list = [0,1,2,3,4,5,6,7,8,9]: List of Integer;
job
  sub_list= [ x | x in my_list and x%2 = 0];
  print(sub_list); 
end job;

=> [0,2,4,6,8]


You need to login to view the rest of the content. Please . Not a Member? Join Us