Clojures persistent vectors in Nim27th July 2017

One of the things about functional languages which makes them so easy to work with are their immutable data structures. Immutable means that you can't change them, which is why many people prefer the term persistent as it refers to how each pointer to the data structure would always yield the same view of the data. But instead of simply copying large amounts of data each time you want to do an operation most functional languages use some smarter methods to achieve persistence. I recently found this blog post series about how Clojures persistent vectors are implemented. I knew about the underlying concept before (as made clear by my talk on functional programming released here on this site), but I've never seen the entire thing with optimizations explained so clearly before. So I decided it would be fun to recreate the data-structure in Nim. Imperative languages like Nim typically don't use these kinds of data structures, and many programmers of such languages would shy away from using them out of performance reasons. But for certain tasks, especially things like asynchronous jobs or historic views of data, they offer some real benefits. Historic views of data here refers to things like infinite undos, which can be implemented much easier with proper persistant data structures. So if you want to play around with persistent data structures in Nim, have a look at my implementation here.