Validating a sequence of XML elements with Writer monad

不打扰是莪最后的温柔 提交于 2019-12-10 11:18:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!