This is a follow-up to my old question.
Suppose I need to validate an XML:
<a><a1/><a2/><a3/></a>
I need to make sure that the root a has children a1, a2, and a3 (in this order).
I'd like to use a List (instead of scalaz.Validation) and Writer monad to collect validation errors. So I define a function to validate a sequence of XML elements like this:
type Validate = List[Elem] => Writer[List[String], List[Elem]]
Now I can add a function to validate the label of an XML element:
val label: String => Validate = ...
If I redefine Validate as Kleisli I'll be able to compose the Validate functions with andThen :
val children: Validate = label("a1") andThen label("a2") andThen label("a3")
Doest it make sense ? How would you correct/simplify it ?
来源:https://stackoverflow.com/questions/34973056/validating-a-sequence-of-xml-elements-with-writer-monad