博客
关于我
Scalaz(19)- Monad: \/ - Monad 版本的 Either
阅读量:459 次
发布时间:2019-03-06

本文共 2537 字,大约阅读时间需要 8 分钟。

Scala标准库提供了一个Either类型,它是一个对Option的升级版。与Option类似,Either也有两种状态:Left和Right,分别对应Option的None和Some。但是Left可以返回一个值,这通常用于表示异常信息。

scalaz提供了自己的Either实现,并使用/来区分Left和Right。/不仅仅是一个类型,它还是一个Monad,具备了函数组合的能力。这使得它能够方便地整合到函数式编程中。

我们先来看/的定义:

sealed abstract class \/[+A, +B] extends Product with Serializable {  def isLeft: Boolean = this match {    case -\/(_) => true    case \/-(_) => false  }  def isRight: Boolean = this match {    case -\/(_) => false    case \/-(_) => true  }  def getOrElse[BB >: B](x: => BB): BB = this match {    case -\/(_) => x    case \/-(b) => b  }  def |[BB >: B](x: => BB): BB = getOrElse(x)  def valueOr[BB >: B](x: A => BB): BB = this match {    case -\/(a) => x(a)    case \/--(b) => b  }  def orElse[AA >: A, BB >: B](x: => AA \/ BB): AA \/ BB = this match {    case -\/(_) => x    case \/--(b) => this  }  def |||[AA >: A, BB >: B](x: => AA \/ BB): AA \/ BB = orElse(x)}

与Option相同,/也提供了获取运算值的方法,如getOrElse。要获取异常信息,可以使用swap后再用getOrElse:

def swap: (B \/ A) = this match {  case -\/(a) => \/-(a)  case \/--(b) => -\/(b)}

与Option一样,/也有两种状态:

final case class -\/[+A](a: A) extends (A \/ Nothing)final case class \/-[+B](b: B) extends (Nothing \/ B)

/实现了map和flatMap:

def map[D](g: B => D): (A \/ D) = this match {  case \/-(a) => \/-(g(a))  case b @ -\/(_) => b}def flatMap[AA >: A, D](g: B => (AA \/ D)): (AA \/ D) = this match {  case a @ -\/(_) => a  case \/-(b) => g(b)}

注意到flatMap:如果状态为/- 则执行g(b),如果状态为-/ 则立即停止运算返回-/状态。这与Option的功能相当。

我们可以用for-comprehension来证明:

1 val epok = for {  2     a <- \/-(3)  3     b <- \/-(2)  4 } yield a + b// 结果:epok : scalaz.\/[Nothing,Int] = \/-(5)5 val epno = for {  6     a <- \/-(3)  7     c <- "breaking out...".left[Int]  8     b <- \/-(2)  9 } yield a + b// 结果:epno : scalaz.\/[String,Int] = -\/(breaking out...)10 if (epno.isLeft) (~epno).getOrElse("no error")// 结果:res5: Any = breaking out...

这样表述是不是清晰直白多了。

与Option一样,/也有两种状态。/type class为任何类型提供了注入方法left和right:

final def left[B]: (A \/ B) = -\/(self)final def right[B]: (B \/ A) = \/-(self)trait ToEitherOps {  implicit def ToEitherOps[A](a: A) = new EitherOps(a)}

现在这个for-comprehension可以这样写:

1 val epok1 = for {  2     a <- 3.right  3     b <- 2.right  4 } yield a + b// 结果:epok1 : scalaz.\/[Nothing,Int] = \/-(5)5 val epno1 = for {  6     a <- 3.right  7     c <- "breaking out...".left[Int]  8     b <- 2.right  9 } yield a + b// 结果:epno1 : scalaz.\/[String,Int] = -\/(breaking out...)10 if (epno1.isLeft) (~epno1).getOrElse("no error")// 结果:res6: Any = breaking out!

这样表述是不是清晰直白多了。

是不是觉得这样写起来更直观呢?通过这些例子可以看出,Either在处理错误信息时非常有优势。

转载地址:http://gyefz.baihongyu.com/

你可能感兴趣的文章
python+pytest接口自动化 —— 参数关联
查看>>
Python+pytest接口自动化 —— 接口测试基础
查看>>
python+pytest接口自动化 —— 自动化用例编写思路 (使用pytest编写一个测试脚本)
查看>>
Python+pytest接口自动化之cookie绕过登录(保持登录状态)
查看>>
python+pytest接口自动化:接口测试
查看>>
Python+requests+unittest执行接口自动化测试详情
查看>>
Python+requests+unittest执行接口自动化测试详情
查看>>
python+requests+unittest执行自动化接口测试!
查看>>
Python+Requests编码识别Bug
查看>>
python函数编写_[零基础学python]传说中的函数编写条规
查看>>
Python+selenium —— UI自动化之鼠标操作
查看>>
python函数注释,参数后面加冒号:,函数后面的箭头→是什么?
查看>>
Python+Selenium+Threading进行兼容性测试
查看>>
Python+selenium+unittest的GUI自动化框架实现
查看>>
python函数拟合不规则曲线_Python计算&绘图——曲线拟合问题(转)
查看>>
python函数定义的基本格式_零基础学python-2.19 定义函数、调用函数与默认参数
查看>>
Python+Selenium之数据驱动测试的实现
查看>>
python函数定义与使用+返回值简解
查看>>
Python+Selenium登录
查看>>
Python日期时间模块
查看>>