Learn the heart of the Clojure language
Clojure is based on collections, but how are they used? What are some patterns for making the most of them? This course introduces you to the workhorses of the Clojure programming language, the immutable collections.
This course has four lessons now, focused on hash maps and vectors. I'll add to this over time.
Want this course?
Love it or leave it guarantee
If you don't learn as much from this course as you thought, just ask for a refund within 30 days and I'll give you your money back.
A super common way to access items in Clojure collections is through the seq abstraction. The seq abstraction gives us the items in a collection one at a time. But what order do those come in? The order is determined by the collection itself. We look at the different options for order. which largely determine the choice of collection type we use.
Another common access pattern that largely determines the choice of collection is whether you want to remember duplicates. What happens when duplicates occur? Do you remember them? Do you replace the old one? Do you keep the old one?
Lookup by key
It is a very useful access pattern to look up a value given another key. We use HashMaps, Vectors, and Sets to implement this pattern. Each data type has its own semantics.
Associate key and value
We look at another common access pattern, which is to associate a key and a value into a collection. The two collections that this works with are HashMap and Vector. Combined with Lookup value by key, we can do a convenient update operation.
Dissociate a key and value
Sometimes we want to remove a key/value pair from a HashMap. We use the dissoc operator to do that.
Count the elements
A common access pattern that we often don't think about is to count the elements. We don't think about it because all of the collections implement it. But there is one big gotcha you should know about.
Clojure obeys the Java standard library's equality partitions. Namely, sequential, map, and set partitions for collections. These allow us to swap out different concrete implementations of our collections while still having equivalent logical collections.
Removing an item from a set
Clojure gives us an operation to remove an element from a set. We pass in the set and the element, and it's gone!
Splitting a sequence
Normally, splitting a sequence requires a take and a drop, which get slower as the number of elements gets bigger. However, Vectors provide a fast way to split off a sub vector.
We can quickly check if an item is contained in a set. We can also check quickly if a key is in a Map, or if an index will work for a Vector.
A useful access pattern is to add stuff on one and and take it off the other end. This gives you a way to take stuff off in the same order you put it in on, like a queue. Clojure provides a collection called Queue for just that purpose.
This lesson teaches the basics of creating and using hashmaps. It includes a 13 minute screencast.
This lesson teaches the basics of vectors. This lesson includes a video screencast. The video is 7 minutes long.
Do you know these two patterns (Record and Index) that are commonly used with hashmaps? This lesson explains why and when they are used and give you a method for determining which one is being used in someone else's code.