Functional solution
Functional programming uses a completely different mindset in approaching the problem. The fundamental elements can be categorized into immutable data structures and pure functions.
Immutable data structures
An immutable data structure cannot be changed once it has been created. If a new value is needed to capture a change, new data structure instances are created and usually transformed from the existing ones. This approach makes data reliable and thread-safe.
Kotlin provides the data class construct that comes with standard functions such as toString
, hashcode
, and equals
for free. Combined with the use of the val
keyword and exclusive refereces to other immutable data, we can easily create an immutable data structure.
Here are the equivalent data structures for the example:
data class Plumbing( val startedAt: Instant? = null, val completedAt: Instant? = null, val confirmedAt...