scalaz还提供了个type class叫Validation。乍看起来跟\/没什么分别。实际上这个Validation是在\/的基础上增加了Applicative功能,就是实现了ap函数。通过Applicative实例就可以同时运算多个Validation并返回多条异常信息。所以,\/与Validation核心分别就在于Validation可以返回多条异常信息。Validation也是由两种状态组成:Success和Failure,分别与\/的left和right相对应。Failure可以返回多个值。我们先来看看Validation在scalaz里的定义:scalaz/Validation.scala

sealed abstract class Validation[+E, +A] extends Product with Serializable {
...
def isSuccess: Boolean = this match {
case Success(_) => true
case Failure(_) => false
} /** Return `true` if this validation is failure. */
def isFailure: Boolean = !isSuccess
...
/** Return the success value of this validation or the given default if failure. Alias for `|` */
def getOrElse[AA >: A](x: => AA): AA =
this match {
case Failure(_) => x
case Success(a) => a
} /** Return the success value of this validation or the given default if failure. Alias for `getOrElse` */
def |[AA >: A](x: => AA): AA =
getOrElse(x) /** Return the success value of this validation or run the given function on the failure. */
def valueOr[AA >: A](x: E => AA): AA =
this match {
case Failure(a) => x(a)
case Success(b) => b
} /** Return this if it is a success, otherwise, return the given value. Alias for `|||` */
def orElse[EE >: E, AA >: A](x: => Validation[EE, AA]): Validation[EE, AA] =
this match {
case Failure(_) => x
case Success(_) => this
} /** Return this if it is a success, otherwise, return the given value. Alias for `orElse` */
def |||[EE >: E, AA >: A](x: => Validation[EE, AA]): Validation[EE, AA] =
orElse(x)
...

与\/非常相似,也是提供了getOrElse来获取Success[A]的A值。如果需要获取Failure[B]值则与\/一样先用swap再用getOrElse:

/** Flip the failure/success values in this validation. Alias for `unary_~` */
def swap: Validation[A, E] =
this match {
case Failure(a) => Success(a)
case Success(b) => Failure(b)
} Success().getOrElse() //> res5: Int = 3
Success("Three").getOrElse("Everything OK!") //> res6: String = Three
Failure("Something wrong!").swap.getOrElse("Everything OK!")
//> res7: String = Something wrong!
(~Failure("Something wrong!")).getOrElse("Everything OK!")
//> res8: String = Something wrong!

Validation的两个状态是这样定义的:

final case class Success[A](a: A) extends Validation[Nothing, A]
final case class Failure[E](e: E) extends Validation[E, Nothing]

Validation也是一个Monad,可以在for-comprehension中实现Failure立即退出功能:

 for {
a <- Success()
b <- Success()
} yield a + b //> res5: scalaz.Validation[Nothing,Int] = Success(5) val valid= for {
a <- Success()
c <- Failure("oh, error!"): Validation[String,Int]
d <- Failure("oh, error again!"): Validation[String,Int]
b <- Success()
} yield a + b //> valid : scalaz.Validation[String,Int] = Failure(oh, error!)
if (valid.isFailure) valid.swap.getOrElse("no error")
//> res6: Any = oh, error!

scalaz同样为所有类型值提供了注入方法:scalaz.syntax/ValidationOps.scala

final class ValidationOps[A](self: A) {
def success[X]: Validation[X, A] = Validation.success[X, A](self) def successNel[X]: ValidationNel[X, A] = success def failure[X]: Validation[A, X] = Validation.failure[A, X](self) @deprecated("use `failure` instead", "7.1")
def fail[X]: Validation[A, X] = failure[X] def failureNel[X]: ValidationNel[A, X] = Validation.failureNel[A, X](self) @deprecated("use `failureNel` instead", "7.1")
def failNel[X]: ValidationNel[A, X] = failureNel[X]
} trait ToValidationOps {
implicit def ToValidationOps[A](a: A) = new ValidationOps(a)
}

上面的例子也可以这样写:

 for {
a <- .success
b <- .success
} yield a + b //> res7: scalaz.Validation[Nothing,Int] = Success(5) val pv = for {
a <- .success
c <- "oh, error!".failure[String]
d <- "oh, error again!".failure[String]
b <- .success
} yield a + b //> pv : scalaz.Validation[String,Int] = Failure(oh, error!)
if (pv.isFailure) (~pv).getOrElse("no error") //> res8: Any = oh, error!

不过上面两条异常信息只返回了头一条,这与\/并没有什么两样,因为它们的flatMap都是一样的:

final class ValidationFlatMap[E, A] private[scalaz](val self: Validation[E, A]) {
/** Bind through the success of this validation. */
def flatMap[EE >: E, B](f: A => Validation[EE, B]): Validation[EE, B] =
self match {
case Success(a) => f(a)
case e @ Failure(_) => e
}
}

当前版本的scalaz已经放弃了flatMap用法:

  @deprecated("""flatMap does not accumulate errors, use `scalaz.\/` or `import scalaz.Validation.FlatMap._` instead""", "7.1")
@inline implicit def ValidationFlatMapDeprecated[E, A](d: Validation[E, A]): ValidationFlatMap[E, A] =
new ValidationFlatMap(d) /** Import this if you wish to use `flatMap` without a deprecation
* warning.
*/
object FlatMap {
@inline implicit def ValidationFlatMapRequested[E, A](d: Validation[E, A]): ValidationFlatMap[E, A] =
new ValidationFlatMap(d)
}

因为Validation又是个Applicative。它实现了ap函数:

  /** Apply a function in the environment of the success of this validation, accumulating errors. */
def ap[EE >: E, B](x: => Validation[EE, A => B])(implicit E: Semigroup[EE]): Validation[EE, B] = (this, x) match {
case (Success(a), Success(f)) => Success(f(a))
case (e @ Failure(_), Success(_)) => e
case (Success(_), e @ Failure(_)) => e
case (Failure(e1), Failure(e2)) => Failure(E.append(e2, e1))
}

我们可以同时运算几个Validation算法并返回所有异常信息:

((.success : Validation[String,Int]) |@|
("oh, error1! ".failure : Validation[String,Int]) |@|
(.success : Validation[String,Int]) |@|
("oh, error2 again!".failure : Validation[String,Int])){_ + _ + _ + _}
//> res13: scalaz.Validation[String,Int] = Failure(oh, error1! oh, error2 again!)

我们看到即使其中两项运算出现异常但还是完成了所有运算并且返回了两条异常信息。不过这两条信息合并在了String里,可能不方便后续处理。Validation注入方法提供了failureNel函数。我们试着用用:

((.successNel : ValidationNel[String,Int]) |@|
("oh, error1! ".failureNel : ValidationNel[String,Int]) |@|
(.successNel : ValidationNel[String,Int]) |@|
("oh, error2 again!".failureNel : ValidationNel[String,Int])){_ + _ + _ + _}
//> res14: scalaz.Validation[scalaz.NonEmptyList[String],Int] = Failure(NonEmptyList(oh, error1! , oh, error2 again!))

现在这两条信息被放进了NonEmptyList里。NonEmptyList就是一种List,不过没有Nil状态。看看它的定义:scalaz/NonEmptyList.scala

/** A singly-linked list that is guaranteed to be non-empty. */
final class NonEmptyList[+A] private[scalaz](val head: A, val tail: List[A]) {
...

至少这个List含有head元素。NonEmptyList的构建器在注入方法中:scalaz/NonEmptyListOps.scala

final class NelOps[A](self: A) {
final def wrapNel: NonEmptyList[A] =
NonEmptyList(self)
} trait ToNelOps {
implicit def ToNelOps[A](a: A) = new NelOps(a)
}

我们简单地试用这个NonEmptyList:

  val nel =  <::  <:: .wrapNel                  //> nel  : scalaz.NonEmptyList[Int] = NonEmptyList(2, 4, 3)
val snel = "one" <:: "two" <:: "three".wrapNel //> snel : scalaz.NonEmptyList[String] = NonEmptyList(one, two, three)
nel.list //> res17: List[Int] = List(2, 4, 3)
snel.list //> res18: List[String] = List(one, two, three)

我们可以直接把它转成List再进行处理操作。

Scalaz(20)-Monad: Validation-Applicative版本的Either的更多相关文章

  1. 泛函编程(25)-泛函数据类型-Monad-Applicative

    上两期我们讨论了Monad.我们说Monad是个最有概括性(抽象性)的泛函数据类型,它可以覆盖绝大多数数据类型.任何数据类型只要能实现flatMap+unit这组Monad最基本组件函数就可以变成Mo ...

  2. 泛函编程(26)-泛函数据类型-Monad-Applicative Functor Traversal

    前面我们讨论了Applicative.Applicative 就是某种Functor,因为我们可以用map2来实现map,所以Applicative可以map,就是Functor,叫做Applicat ...

  3. Monad / Functor / Applicative 浅析

    前言 Swift 其实比 Objective-C 复杂很多,相对于出生于上世纪 80 年代的 Objective-C 来说,Swift 融入了大量新特性.这也使得我们学习掌握这门语言变得相对来说更加困 ...

  4. Scalaz(41)- Free :IO Monad-Free特定版本的FP语法

    我们不断地重申FP强调代码无副作用,这样才能实现编程纯代码.像通过键盘显示器进行交流.读写文件.数据库等这些IO操作都会产生副作用.那么我们是不是为了实现纯代码而放弃IO操作呢?没有IO的程序就是一段 ...

  5. Scalaz(19)- Monad: \/ - Monad 版本的 Either

    scala标准库提供了一个Either类型,它可以说是Option的升级版.与Option相同,Either也有两种状态:Left和Right,分别对应Option的None和Some,不同的是Lef ...

  6. Scalaz(10)- Monad:就是一种函数式编程模式-a design pattern

    Monad typeclass不是一种类型,而是一种程序设计模式(design pattern),是泛函编程中最重要的编程概念,因而很多行内人把FP又称为Monadic Programming.这其中 ...

  7. 浅释Functor、Applicative与Monad

    引言 转入Scala一段时间以来,理解Functor.Applicative和Monad等概念,一直是我感到头疼的部分.虽然读过<Functors, Applicatives, And Mona ...

  8. 泛函编程(27)-泛函编程模式-Monad Transformer

    经过了一段时间的学习,我们了解了一系列泛函数据类型.我们知道,在所有编程语言中,数据类型是支持软件编程的基础.同样,泛函数据类型Foldable,Monoid,Functor,Applicative, ...

  9. Spring4新特性——集成Bean Validation 1.1(JSR-349)到SpringMVC 配置校验器

    Spring4新特性——泛型限定式依赖注入 Spring4新特性——核心容器的其他改进 Spring4新特性——Web开发的增强 Spring4新特性——集成Bean Validation 1.1(J ...

随机推荐

  1. 解析for循环

    循环的作用就是让一个程序.连续进行一遍又一遍的循环: for循环: 分为四大类: 初始状态:相当于他一开始的数值,或条件: 循环条件:满足进行循环,不满足则停止: 循环体:循环的东西,程序: 状态改变 ...

  2. chrome远程调试真机上的app

    chrome远程调试真机上的app 看来要上真机了...

  3. KnockoutJS 3.X API 第二章 数据监控(1)视图模型与监控

    数据监控 KO的三个内置核心功能: 监控(Observable)和依赖性跟踪(dependency tracking) 声明绑定(Declarative bindings) 模板(Templating ...

  4. vm中centos7配置静态ip访问外网

    我使用的是桥接方式,具体步骤如下   1.设置虚拟机网络: 编辑>虚拟网络编辑器   2.设置vm中操作系统的网络设置   3.进入centos7中后修改网络配置:     另附我的宿主机网络配 ...

  5. python的继承

    继承是面向对象的重要特征之一,继承是两个类或者多个类之间的父子关系,子进程继承了父进程的所有公有实例变量和方法.继承实现了代码的重用.重用已经存在的数据和行为,减少代码的重新编写,python在类名后 ...

  6. China Mobile 免流原理

    用户添加包头之类(SSR软件),最后连接上了Server. 这个过程因为修改了数据包(混淆)以及服务器使用移动IP. 服务器得到信息后通过计费系统漏洞137 138端口等再返回给用户. 整个过程最重要 ...

  7. dubbox

    github源码: https://github.com/dangdangdotcom/dubbox maven中央仓: 无 获取分支 git clone -b dubbox-2.8.4 https: ...

  8. 动画animation的三个应用(漂浮的白云、旋转的星球、正方体合成)

    × 目录 [1]漂浮的白云 [2]旋转的星球 [3]正方体合成 前面的话 前面介绍过动画animation的详细用法,本文主要介绍动画animation的三个效果 漂浮的白云 [效果演示] [简要介绍 ...

  9. Unity3d知识体系思维导图

    整理了一下U3D的技能树.

  10. 【特别推荐】Web 开发人员必备的经典 HTML5 教程

    对于我来说,Web 前端开发是最酷的职业之一,因为你可以用新的技术发挥,创造出一些惊人的东西.唯一的问题是,你需要跟上这个领域的发展脚步,因此,你必须不断的学习,不断的前进.本文将分享能够帮助您快速掌 ...