Tutorial: Containers

Storing Information in memory

Using Containers in Quorum

Like many programming languages, Quorum has a variety of support classes for storing information in memory. In Quorum, these are generally called containers. In other tutorials, we have learned how to use the Array class. In this tutorial, we will discuss one more container, the List. This tutorial is intended as a high level overview of the most basic operations. The full documentation can be found at the List page. Besides arrays and lists, Quorum has a variety of other data structures, including a Hashtable, Queue, Stack, and Table. In this tutorial, while we discuss lists, the other data structures are comparable.

Adding to a List

We can add to a list using code like the following:

//like with arrays, this code represents a template, a list that is filled only with integers
use Libraries.Containers.List
List<integer> list

//put a value in the second slot in the array, which is named 1
list:Add(5)
output "We added an item to the list."

Quorum does not allow us to add types to a templated value unless it matches exactly the type specified in the generic. So, for example, the following would be a compiler error:

use Libraries.Containers.List
List<integer> list

//since we have declared this list to use integers, we cannot put in a number type (with a decimal point)
list:Add(5.0)

Getting Elements From a List

Once we have added values to a list, we can obtain them individually using the Get action, like so:

use Libraries.Containers.List
List<integer> myList

//add two values to the list, 12 and 13
myList:Add(12)
myList:Add(13)

//this line returns the first integer, 12
integer value = myList:Get(0)
output value

Removing Items From a List

Similarly to getting, we can remove items from a list. If we want to remove just the first version of a value (values are not guaranteed to be unique), then we can call Remove. If we want to remove all instances of an item, we can call RemoveAll. If an item is found, we are returned a boolean saying that the removal was successful.

use Libraries.Containers.List
List<integer> myList
myList:Add(43)
myList:Add(13)
myList:Add(43)

//RemoveAll(43) would remove all 43s
boolean removed = myList:Remove(43)
output removed

Iterating Over a List

Finally, we often want to traverse over all elements in a list. To do this, we can use the Iterator class and ask it for each element, one at a time. The following example shows us how we might do this:

use Libraries.Containers.List
use Libraries.Containers.Iterator
class Main
   action Main
       List<integer> list
        //add 3 items: 5, 10, and 15
       list:Add(5)
       list:Add(10)
       list:Add(15)

       //create an iterator to traverse our list
       Iterator<integer> it = list:GetIterator()
       //Keep iterating if more elements exist
       repeat while it:HasNext()
           //get the next item.
           integer value = it:Next()
           output value
       end
   end
end

Additional Resources for Quorum Containers

Next Tutorial

In the next tutorial, we will discuss errors, which describes how to handle errors that occur at runtime.