8 Functional data structures
This chapter covers
- Understanding the omnipresence of linked lists in FP languages
- Data sharing in functional data structures
- Working with trie structures
- Comparing a standard vector to its immutable counterpart
So far, I’ve talked mostly about higher-level functional programming concepts, and we spent quite a while examining the benefits of programming without mutable state. The problem is that programs tend to have many moving parts. While discussing purity in chapter 5, I said one of the options is to have only the main component with mutable state. All other components are pure and calculate a series of changes that should be performed on the main component, but without actually changing anything. Then the main component can perform those changes on itself.
This approach creates a clear separation between the pure parts of the program and those that deal with the mutable state. The problem is that it’s often not easy to design software like this, because you need to pay attention to the order in which the calculated changes to the state should be applied. If you don’t do that properly, you may encounter data races similar to those you have when working with mutable state in a concurrent environment.