我是scalaz的新手,我从验证开始.
我有一些表格的验证功能:
def validateXyz(...): ValidationNEL[String, String] = ...
Run Code Online (Sandbox Code Playgroud)
然后我使用applicative样式组合多个验证,然后调用另一个也返回验证的函数:
(validateXyz(...) |@| validateAbc(...)) { (first, second) =>
otherFunction(first, second)
}
Run Code Online (Sandbox Code Playgroud)
哪里,
def otherFunction(first: String, second: String): ValidationNEL[String, String] = ...
Run Code Online (Sandbox Code Playgroud)
但是,在调用上面的结果类型时:
val result: ValidationNEL[String, ValidationNEL[String, String]] = ...
Run Code Online (Sandbox Code Playgroud)
我可以通过使用两个函数调用结果上的fold来解压缩这个函数,第一个将NEL传播为失败,第二个传播其参数:
def propagateF(result: NonEmptyList[String]): ValidationNEL[String, String] = result.fail
def propagateV(result: ValidationNEL[String, String]) = result
result.fold(propagateF, propagateV)
// result type: ValidationNEL[String, String]
Run Code Online (Sandbox Code Playgroud)
这适用于并返回正确的类型和结果.然而,它感觉不是正确的解决方案,所以我必须遗漏一些东西.我最近需要做些什么来避免这种可怕的褶皱呢?
我通过将现有的类转换为使用Monoid特征来进行scalaz的第一次尝试.我想要实现的是在我的类类型参数上设置一个视图绑定,以确保它只能用于可以隐式转换为Monoid的类型.因此,我的(简化)类定义是:
import scalaz._
import Scalaz._
case class Foo[T <% Monoid[T]](v: T)
new Foo(42)
Run Code Online (Sandbox Code Playgroud)
编译这个简单的例子给出了编译器错误:
error: No implicit view available from Int => scalaz.Monoid[Int].
Run Code Online (Sandbox Code Playgroud)
以前这个视图绑定是根据我自己的自定义特征定义的,具有从T到特征的隐式转换,这很好.
我现在将这个转换为scalaz,我错过了什么?
谢谢,克里斯