博客
关于我
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两组变量的相关关系散点图_变量是什么?变量间的两种关系——函数关系和相关关系(小学版)...
查看>>
Python中 pass语句的作用
查看>>
python中 urllib, urllib2, httplib, httplib2 几个库的区别
查看>>
python中 yaml库 & json库 & xmltodict库使用
查看>>
python中 将你的名字转化成为二进制并输出
查看>>
Python中*args 和**kwargs的用法
查看>>
Python中*args 和**kwargs的用法
查看>>
Python中*args和**kwargs的使用
查看>>
python中0、1、True、False、if条件
查看>>
python中argparse模块关于 parse_args() 函数详解(全)
查看>>
python中base64编码与解码
查看>>
python中break怎么用_详解Python中break语句的用法
查看>>
python中del函数的垃圾回收
查看>>
Python中DrissionPage的详细解析与实战
查看>>
python中enumerate、变量类型转换
查看>>
python中execute是啥_python – 为什么“c.execute(…)”打破循环?
查看>>
python中exit和break区别_Python中exit()和sys.exit()之间的区别
查看>>
python中h5的用法
查看>>
python中http协议编程_HTTP编程
查看>>
python中if的效率_python中关于if-else使用性能的一点感悟
查看>>