So far, we have explored the string and number type. These are great for exactly that, text and numerical values. What if we want to store a collection of strings and numbers? Meet your companion, the list.
obj x = [];
The list type is a static collection that can be grown, shrunk, indexed, combined, and more. It can store any type separated by commas:
obj x = ["Object in the list", 1, 2, 3, 4];
To access a value in a list, we can index it:
obj x = [1, 2, 3, 4];
# 'retrieve' will get the value at index 'i'
obj value_in_list = retrieve(x, 0); # in this case, index 0 is the first item in the list
To add a value to a list, we can push it:
obj x = [];
# 'push' will return a new list with the added value
obj x = push(x, "value");
To get rid of a value in a list, we can remove it:
obj x = [1, 2, 3, 4, 5];
# 'remove' will return a new list with the removed index
obj x = remove(x, 4); # list looks like: [1, 2, 3, 4]
To combine two lists, we can append them:
obj x = [1, 2];
obj y = [3, 4];
# 'append' will return the two lists combined with each other
obj combined = append(x, y);
And lastly, we can reverse the values in a list:
obj x = [3, 2, 1];
# 'reverse' will return a new list with reversed values
obj reversed = reverse(x);
With all these operations, you may have noticed we return a new list. This is because the list type is static, meaning we can't modify it in place; we have to create a new list for every modification.
In programming, this is referred to as "immutable" data. Why GLang does this