Have you ever tried adding a default to a Scala Map and lamented that downstream references have no idea that the default exists? Or unknowingly clobbered values in your Map by adding a value whose key was already present? Or do you ever want to combine two or more Maps with some operation (e.g. add, subtract, multiply)?
While Scala Maps are powerful and fast, my fellow engineers and I kept running into some of their limitations, so I set out to improve them. My solution was a relatively lightweight Map collection that wraps a Scala Map, with the goals of protecting my fellow engineers from some of the unsafe properties of Maps while also enabling rich and expressive functionality on collections of key-value pairs. For example, PMaps enable:
- Adding a default that is represented in the type of the collection
- Convenient arithmetic operations on the collection level – e.g. adding or subtracting the values of two collections
- Explicitly handle errors when you add overlapping keys
- ‘Join’ functionality that allows us to write expressive and readable business logic combining multiple collections
In the talk, I hope to:
- Motivate some of the shortcomings with Scala’s built-in immutable Map collection and our decision to build our own simple Map collection.
- Describe the functional programming and Scala patterns and principles I relied on, and the tradeoffs I came across in building PMaps.
- Show off some of the functionality of PMaps and how they enable rich and clean logic.
This talk is meant to be accessible for all levels of Scala experience. Type classes and F-Bounded Polymorphism might be mentioned briefly, but most of the talk will be understandable for beginners and hopefully relevant even for experienced Scala users.