I started reading this book called Thinking with Types. https://leanpub.com/thinking-with-types Right off he gave an example that I thought was a pretty cool about using the algebra of types to analyze and improve some code. Granted this is a simple example but I got a bit of a chub from it. What follows is most of the lead up to the example and the example itself. Mostly converted from Haskell to Scala.
As their name suggests, there is in fact an algebra behind algebraic data types. Being comfortable understanding and manipulating this algebra is a mighty superpower — it allows us to analyze types, find more convenient forms for them, and determine which operations