Scalaz(19)- Monad: \/ - Monad 版本的 Either
来源:互联网 发布:php 7.0 phpmyadmin 编辑:程序博客网 时间:2024/04/29 14:32
scala标准库提供了一个Either类型,它可以说是Option的升级版。与Option相同,Either也有两种状态:Left和Right,分别对应Option的None和Some,不同的是Left可以返回一个值。我们通常用这个值来表述异常信息。scalaz也提供了自己版本的Either,并用\/来分辨表示,以及两种状态-\/和\/-。我想scalaz特别提供\/是有原因的:\/不单是一种类型,它是一种type class。更重要的是\/是一种Monad,具备了函数组合能力(composibility)。如此能够方便把Either功能整合到FP编程中去。我们先看看\/的定义:scalaz/Either.scala
sealed abstract class \/[+A, +B] extends Product with Serializable {
... def isLeft: Boolean = this match { case -\/(_) => true case \/-(_) => false } /** Return `true` if this disjunction is right. */ def isRight: Boolean = this match { case -\/(_) => false case \/-(_) => true }... /** Return the right value of this disjunction or the given default if left. Alias for `|` */ def getOrElse[BB >: B](x: => BB): BB = this match { case -\/(_) => x case \/-(b) => b } /** Return the right value of this disjunction or the given default if left. Alias for `getOrElse` */ def |[BB >: B](x: => BB): BB = getOrElse(x) /** Return the right value of this disjunction or run the given function on the left. */ def valueOr[BB >: B](x: A => BB): BB = this match { case -\/(a) => x(a) case \/-(b) => b } /** Return this if it is a right, otherwise, return the given value. Alias for `|||` */ def orElse[AA >: A, BB >: B](x: => AA \/ BB): AA \/ BB = this match { case -\/(_) => x case \/-(_) => this } /** Return this if it is a right, otherwise, return the given value. Alias for `orElse` */ def |||[AA >: A, BB >: B](x: => AA \/ BB): AA \/ BB = orElse(x)...
与Option相同:\/也提供了函数来获取运算值(Right[A]),如getOrElse。那么如何获取异常信息呢?可以用swap后再用getOrElse:
/** Flip the left/right values in this disjunction. Alias for `unary_~` */ def swap: (B \/ A) = this match { case -\/(a) => \/-(a) case \/-(b) => -\/(b) } /** Flip the left/right values in this disjunction. Alias for `swap` */ def unary_~ : (B \/ A) = swap"ah, error!".left[Int].getOrElse("no error") //> res2: Any = no error"ah, error!".left[Int].swap.getOrElse("no error") //> res3: String = ah, error!(~"ah, error!".left[Int]).getOrElse("no error") //> res4: String = ah, error!
/** A left disjunction * * Often used to represent the failure case of a result */final case class -\/[+A](a: A) extends (A \/ Nothing)/** A right disjunction * * Often used to represent the success case of a result */final case class \/-[+B](b: B) extends (Nothing \/ B)
/** Map on the right of this disjunction. */ def map[D](g: B => D): (A \/ D) = this match { case \/-(a) => \/-(g(a)) case b @ -\/(_) => b } /** Bind through the right of this disjunction. */ def flatMap[AA >: A, D](g: B => (AA \/ D)): (AA \/ D) = this match { case a @ -\/(_) => a case \/-(b) => g(b) }
val epok = for {a <- \/-(3)b <- \/-(2)} yield a + b //> epok : scalaz.\/[Nothing,Int] = \/-(5)val epno = for {a <- \/-(3)c <- -\/("breaking out...")b <- \/-(2)} yield a + b //> epno : scalaz.\/[String,Int] = -\/(breaking out...)if (epno.isLeft) (~epno).getOrElse("no error") //> res5: Any = breaking out...
final class EitherOps[A](self: A) {
\/在for-comprehension里的运算行为与Option一致。不过这个\/写法比较别扭。\/type class为任何类型提供了注入方法left和right: scalaz.syntax/EitherOps.scala
<p style="font-family: Arial, Helvetica, sans-serif; white-space: normal;"><span style="font-family:SimSun;font-size:14px;"></span></p> final def left[B]: (A \/ B) = -\/(self) final def right[B]: (B \/ A) = \/-(self)}trait ToEitherOps { //可以为任何类型A注入方法 implicit def ToEitherOps[A](a: A) = new EitherOps(a)}
现在这个for-comprehension可以这样写:
val epok1 = for {a <- 3.rightb <- 2.right} yield a + b //> epok1 : scalaz.\/[Nothing,Int] = \/-(5)val epno1 = for {a <- 3.rightc <- "breaking out...".left[Int]b <- 2.right} yield a + b //> epno1 : scalaz.\/[String,Int] = -\/(breaking out...)if (epno1.isLeft) (~epno1).getOrElse("no error") //> res6: Any = breaking out...
0 0
- Scalaz(19)- Monad: \/ - Monad 版本的 Either
- Scalaz(20)-Monad: Validation-Applicative版本的Either
- Scalaz(25)- Monad: Monad Transformer-叠加Monad效果
- Scalaz(41)- Free :IO Monad-Free特定版本的FP语法
- Scalaz(11)- Monad:你存在的意义
- Scalaz(16)- Monad:依赖注入-Dependency Injection By Reader Monad
- Scalaz(17)- Monad:泛函状态类型-State Monad
- Scalaz(12)- Monad:再述述flatMap,顺便了解MonadPlus
- Scalaz(13)- Monad:Writer - some kind of logger
- Scalaz(28)- ST Monad :FP方式适用变量
- Scalaz(32)- Free :lift - Monad生产线
- Scalaz(32)- Free :lift - Monad生产线 企
- Scalaz(18)- Monad: ReaderWriterState-可以是一种简单的编程语言
- Monad
- Monad
- Scalaz(10)- Monad:就是一种函数式编程模式-a design pattern
- Scalaz(14)- Monad:函数组合-Kleisli to Reader
- Scalaz(15)- Monad:依赖注入-Reader besides Cake
- Jenkins
- Tomcat heap 配置案例
- C++11: inherited constructors
- mysql 性能优化方案
- Block
- Scalaz(19)- Monad: \/ - Monad 版本的 Either
- AlertDialog居中问题
- liunx CentOS 6.5 匿名用户 安装FTP
- Xcode快捷键
- Linux下文件的权限
- poj 2449 Remmarguts' Date(A*+Dijsktra 求第K短路)
- Android 获取日期并格式化
- error: implicit declaration of function 'posix_memalign' [-Werror=implicit-function-declaration]
- java 操作mongodb的增删查改 crud