StaticArrays
provides an implementation of immutable vectors. We'll explore this package in the context of high performance, as it greatly speeds up computations that involve small vectors. <function>
or <operator>
).
This is just notation, and the symbols <
and >
should not be misconstrued as Julia's syntax.
Action | Keyboard Shortcut |
---|---|
Previous Section | Ctrl + 🠘 |
Next Section | Ctrl + 🠚 |
List of Sections | Ctrl + z |
List of Subsections | Ctrl + x |
Close Any Popped Up Window (like this one) | Esc |
Open All Codes and Outputs in a Post | Alt + 🠛 |
Close All Codes and Outputs in a Post | Alt + 🠙 |
Unit | Acronym | Measure in Seconds |
---|---|---|
Seconds | s | 1 |
Milliseconds | ms | 10-3 |
Microseconds | μs | 10-6 |
Nanoseconds | ns | 10-9 |
Objects in programming can be broadly classified into two categories: mutable and immutable. Mutable objects allow their elements to be modified, appended, or removed at will. They're designed for flexibility, with vectors constituting a prime example.
In contrast, immutable objects are inherently unchangeable: they prevent additions, removals, or modifications of their elements. A common example of immutable object is tuples. Immutability effectively locks variables into a read-only state, safeguarding against unintended changes. Moreover, it can result in potential performance gains, as we'll show in Part II of this website.
This section will be relatively brief, focusing solely on the distinctions between mutable and immutable objects. Subsequent sections will expand on their uses and properties.
StaticArrays
provides an implementation of immutable vectors. We'll explore this package in the context of high performance, as it greatly speeds up computations that involve small vectors.
To illustrate the consequences of immutability, the following examples attempt to modify existing elements of a collection. The examples rely on vectors as an example of mutable object and tuples for immutable ones. Additionally, we present the case of strings as another example of immutable object. Recall that strings are essentially sequences of characters, usually employed to represent text.
x = [3,4,5]
x[1] = 0
x
x = (3,4,5)
x[1] = 0
x = "hello"
x[1]
x[1] = "a"
The key characteristic of mutable objects is their ability to modify existing elements. Moreover, mutability also commonly allows for the dynamic addition and removal of elements. In a subsequent section, we'll present various methods for implementing this functionality. For now, we simply demonstrate the concept by using the functions push!
and pop!
, which respectively add and remove an element at the end of a collection.
x = [3,4]
push!(x, 5) # add element 5 at the end
x
x = [3,4,5]
pop!(x) # delete last element
x
x = (3,4,5)
pop!(x) # error, just like push!(x, <some element>)