I have 19 strings that need to be validated into various types. When all validate successfully, I would like to instantiate a class that represents a row of a spreadsheet (where the columns do not all have the same type).
When one or more of the strings fails to validate, I would like to have the errors accumulated in a NonEmptyList.
If there were 12 or fewer items, I could use |@| or apply12. If I use a for expression, it fails fast and no accumulation happens.
I could sequence the failures when the for expression fails, but that means I'm looping twice. Is there a way to use scalaz to pull each validation success into a variable (as would happen if i used a for expression to instantiate the class) at the same time as accumulating all of the failures?
Suppose we have a case class (which could have more than twelve members):
And that we're representing errors as strings and have defined a type alias for convenience:
We also have some validation results:
Now we can write:
Which gives us what we want:
In Haskell this would be much prettier—you'd just write:
Scala's weaker type inference requires us to write the arguments in the wrong order, unfortunately.