In our last post we dug in to lists (a.k.a first and rest). Lists seemed rather limiting/constrainging in functionality, wouldn’t you agree? The vector fixes all these issues.
The Vector Data Structure
Data within brackets constitutes a vector. So [1 2 3] is a vector.
There are other ways to make vectors as well:
So easy, right?
Vector Access
While rest and first are available here as well, vectors support getting a variable at an index N. Complexity for getting is log32N.
The last way to get a value by index is interesting– you can use a vector as a function.
Vector Modification
You can add to the end of a vector, but not the front (without paying an O(n) cost). This would be the exact opposite of lists if not for the fact that you can replace elements at set indexes via assoc.
Special Functions
I like the distinct? function.
One thing to note is the contains? function. You would think that this would tell you if a vector contains a specific value. Nope– it tells you if the vector has a value at index X. So just don’t use contains? for things other than maps/hash-based data structures.