#MonthOfJulia Day 5: Collections
Julia caters for various collection types including tuples and arrays, dictionaries, sets, dequeues, priority queues and heaps. There’s a lot of functionality distributed across these different structures, so we’ll only skim the surface and pick out a few interesting bits and pieces.
Array is really the most important workhorse collection (IMHO). Julia can handle arrays of arbitrary dimension, but we’ll only have a look at the most commonly used, which are 1D and 2D.
Type promotion is applied to an array with mixed content (like the second example above, which contains an integer, a string and a character), elevating the element type to a common ancestor, which in the example is
The usual indexing operations apply, noting that in Julia indices are 1-based.
Array can be treated like a stack or queue, where additional items can be popped from or pushed onto the “end” of the collection. Functions
unshift!() do analogous operations to the “front” of the collection.
What about a 2D array (or matrix)? Not too many surprises here. With reference to the examples above we can see that a 1D array is effectively a column vector.
Collections are copied by reference. A shallow copy can be created with
copy(). If you want a truly distinct collection of objects you need to use
And now a taste of the other collection types, starting with the tuple.
A dictionary is just a collection of key-value pairs.
Sets are unordered collections which are not indexed and do not allow duplicates.
We’ll see more about collections when we look at Julia’s functional programming capabilities, which will be in the next but one installment. In the meantime you can find the full code for today’s flirtation with Julia on github.