博客
关于我
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 Seborn绘制空白直方图
查看>>
Python Selenium - 获取href值
查看>>
Python Selenium实现自动化测试及Chrome驱动使用!
查看>>
Python Selenium搭建UI自动化测试框架
查看>>
Python Selenium模块详解
查看>>
Python selenium爬取影评生成词云图
查看>>
python selenium自动化测试报告
查看>>
Python selenium自动化测试框架实战 —— 登录测试案例
查看>>
Python Selenium设计模式 —— POM
查看>>
Python Serial:如何使用 read 或 readline 函数一次读取多个字符
查看>>
Python set([]) 如何检查两个对象是否相等?一个对象需要定义哪些方法来自定义它?
查看>>
Python setup.py:数据文件无法复制目录:不存在或不是常规文件
查看>>
Python setuptools sdist:仅安装版本化文件
查看>>
Python Shell下使用matplotlib
查看>>
Python Slice How-to,我知道Python Slice,但我怎么才能使用内置的Slice对象呢?
查看>>
python socket分包发送数据
查看>>
python socket模块_Python socket模块实现TCP服务端客户端
查看>>
Python SOCKS5代理客户端HTTPS
查看>>
Python Soc网络分析:通过使用函数迭代列表来计算机会网络
查看>>
Python SQL和NoSQL数据库操作实战
查看>>