Data modelling with Clojure (Clojure Book Chapter Draft)
Coming from Java, I was finally free from OOP, classes, interfaces and inheritance. It was like a miracle to realize that I only needed a vector, a map and a set to model everything.
When I started my Clojure journey I was impressed by many things, one of which was the ease of expressing my thoughts regarding data modelling.
Coming from Java, I was finally free from OOP, classes, interfaces and inheritance. It was like a miracle to realize that I only needed a vector, a map and a set to model everything.
Lists are everywhere in Clojure, it’s basically a building block of the LISP program. They could be used as a data structure as well, under the hood it’s just a linked list, but usually, a vector is a better choice for data modelling.
Vector []
In simple terms, you can think about a vector as an array. Same properties: fast access by index, but requires shifting elements if inserted in the middle. Technically all Clojure data structures are trees (to implement persistent immutable data structures), so instead of O(1) for index access it is O(log32n) but realistically could be neglected.
Here we have a simple vector with elements of the same type:
[1 2 3]
["a" "b" "c"]
It doesn’t have to be the case all the time, here is a vector with mixed types and also a nested vector:
[1 "a" [2 3 nil]]
Keep reading with a 7-day free trial
Subscribe to Andrey Fadeev to keep reading this post and get 7 days of free access to the full post archives.