我想使用Scalaz进行验证,并希望能够在不同的上下文中重用验证函数.我对Scalaz btw完全不熟悉.
假设我有这些简单的检查:
def checkDefined(xs: Option[String]): Validation[String, String] =
xs.map(_.success).getOrElse("empty".fail)
def nonEmpty(str: String): Validation[String, String] =
if (str.nonEmpty) str.success else "empty".fail
def int(str: String): Validation[String, Int] = ...
Run Code Online (Sandbox Code Playgroud)
我希望能够组合验证,其中一个的输出被馈送到另一个.我可以很容易地用flatMap或通过理解,但感觉必须有一个更好的方式.
for {
v1 <- checkDefined(map.get("foo"))
v2 <- nonEmpty(v1)
v3 <- int(v2)
v4 <- ...
} yield SomeCaseClass(v3, v4)
Run Code Online (Sandbox Code Playgroud)
要么
val x1 = checkDefined(map get "foo").flatMap(nonEmpty).flatMap(int)
val x2 = check(...)
// How to combine x1 and x2?
Run Code Online (Sandbox Code Playgroud)
那里的Scalaz专家有什么想法吗?
我想有一个密封的特征,它有一个声明的方法,返回扩展特征的实际类.我应该使用抽象类型,参数类型还是有任何其他好方法来解决这个问题?
sealed trait Foo {
type T
def doit(other: T): T
}
Run Code Online (Sandbox Code Playgroud)
要么
sealed trait Foo[T] {
def doit(other: T): T
}
Run Code Online (Sandbox Code Playgroud)
请注意,在此示例中T必须是子类型Foo.如果我这样做,类型信息会重复:
case class Bar(name: String) extends Foo[Bar] {
def doit(other: Bar): Bar = ...
}
Run Code Online (Sandbox Code Playgroud) 假设我有这些课程:
case class A()
case class B()
case class C(a: A, b: B)
Run Code Online (Sandbox Code Playgroud)
和这些变量:
val a = A()
val b = B()
Run Code Online (Sandbox Code Playgroud)
有没有办法C隐式地获取一个实例而没有制作a和b隐式的val?即如果我有一个方法期望C:
def foo(c: C)
Run Code Online (Sandbox Code Playgroud)