Scalaz(20)-Monad: Validation-Applicative版本的Either

  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(3).getOrElse(0)                           //> 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立即退出功能:

 1 for {
 2   a <- Success(3)
 3   b <- Success(2)
 4 } yield a + b                                     //> res5: scalaz.Validation[Nothing,Int] = Success(5)
 5 
 6 val valid= for {
 7   a <- Success(3)
 8   c <- Failure("oh, error!"): Validation[String,Int]
 9   d <- Failure("oh, error again!"): Validation[String,Int]
10   b <- Success(2)
11 } yield a + b                                     //> valid  : scalaz.Validation[String,Int] = Failure(oh, error!)
12 if (valid.isFailure) valid.swap.getOrElse("no error")
13                                                   //> 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)
}

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

 1 for {
 2   a <- 3.success
 3   b <- 2.success
 4 } yield a + b                                     //> res7: scalaz.Validation[Nothing,Int] = Success(5)
 5 
 6 val pv = for {
 7   a <-  3.success
 8   c <- "oh, error!".failure[String]
 9   d <- "oh, error again!".failure[String]
10   b <-  2.success
11 } yield a + b                                     //> pv  : scalaz.Validation[String,Int] = Failure(oh, error!)
12 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算法并返回所有异常信息:

((3.success : Validation[String,Int]) |@|
 ("oh, error1! ".failure : Validation[String,Int]) |@|
 (2.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函数。我们试着用用:

((3.successNel : ValidationNel[String,Int]) |@|
 ("oh, error1! ".failureNel : ValidationNel[String,Int]) |@|
 (2.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:

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

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

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏张善友的专栏

Silverlight 2 Beta 1学习资源

Silverlight 2 Beta 1发布了,大家都介绍了怎么去下载安装了,晚上整理了一些资料放上来和大家共享: 1、Silverlight 2 Beta 1...

1697
来自专栏我和未来有约会

44 Amazing Silverlight 2.0 Screencasts

Silverlight - Hello World Silverlight - Anatomy of an Application Silverlight - ...

2019
来自专栏walterlv - 吕毅的博客

The undefined behaviors of WPF Grid (the so-called bugs)

发布于 2018-05-05 09:07 更新于 2018-08...

571
来自专栏DeveWork

小谈中文环境下中文排版的font-family 字体选择

在开发Mindia 主题的时候,为了弄出相对好看的中文排版,特意补习了下中文排版的font-family 字体选择的相关资料。以下就从自己的角度说下自己的不算得...

21810
来自专栏河湾欢儿的专栏

文档宽高及窗口事件

onscroll:当滚动条滚动的时候触发 onresize:当窗口大小发生改变的时候触发

782
来自专栏静晴轩

TOP 100 大前端超棒精选列表

正值如今这信息爆炸的年代,如何能从中汲取精华,于有限时间内,成为更高效的学习者,从而在激烈的竞争中更具优势,是当下每个人或企业都该思虑的问题;先前创立的 Web...

1593
来自专栏林德熙的博客

WPF 使用封装的 SharpDx 控件

上一篇告诉大家如何在 WPF 使用 SharpDx ,看起来代码比较复杂,所以本文告诉大家如何使用我封装的控件。

1352
来自专栏calvin

SignalR主动通知订阅者示例

关键点:GlobalHost.ConnectionManager.GetHubContext

1093
来自专栏我和未来有约会

Silverlight网络寻奇 at 090428

都是Silverlight做的游戏 http://www.silverarcade.com/Games Introducing Html Utilities f...

2984
来自专栏雪胖纸的玩蛇日常

canvas的api小结

2246

扫码关注云+社区