To round up our introduction to functional programming, we'll cover another common pattern, that of error accumulation. This is also sometimes simply referred to as validation.
The idea is that we have a series of functions that individually error check a value. They can return some kind of success value if the input is good, and some kind of error value if the input is bad. These individual functions are then combined, retaining all the errors (if any). Finally, we can interrogate the accumulation to get the errors.
Let's start by modeling the good and bad values that we can use. We'll call them Valid and Invalid, respectively. They will both extend from a superclass called Validation:
sealed class Validation object Valid : Validation() class Invalid(val errors: List<String>) : Validation()
Note that the Invalid...