```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)
...```

```  /** 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和flatMap：

```  /** 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)
}```

``` 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...")
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...```

\/在for-comprehension里的运算行为与Option一致。不过这个\/写法比较别扭。\/type class为任何类型提供了注入方法left和right: scalaz.syntax/EitherOps.scala

```  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)
}```

``` 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...```

0 条评论

## 相关文章

2709

3507

### SDP（9）：MongoDB-Scala - data access and modeling

MongoDB是一种文件型数据库，对数据格式没有硬性要求，所以可以实现灵活多变的数据存储和读取。MongoDB又是一种分布式数据库，与传统关系数据库不同...

3504

2498

672

3244

804

8067

### BZOJ 2748: [HAOI2012]音量调节【二维dp，枚举】

2748: [HAOI2012]音量调节 Time Limit: 3 Sec  Memory Limit: 128 MB Submit: 2010  Solve...

2563

752