Ior

cats.data.Ior
See theIor companion object
sealed abstract class Ior[+A, +B] extends Product, Serializable

Represents a right-biased disjunction that is either an A, or a B, or both an A and a B.

An instance of A Ior B is one of: - Left[A] - Right[B] - Both[A, B]

A Ior B is similar to scala.util.Either[A, B], except that it can represent the simultaneous presence of an A and a B. It is right-biased so methods such as map and flatMap operate on the B value. Some methods, like flatMap, handle the presence of two Both values using a Semigroup[A], while other methods, like toEither, ignore the A value in a Both.

A Ior B is isomorphic to Either[Either[A, B], (A, B)], but provides methods biased toward B values, regardless of whether the B values appear in a Right or a Both. The isomorphic scala.util.Either form can be accessed via the unwrap method.

Attributes

Companion
object
Source
Ior.scala
Graph
Supertypes
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
Show all
Known subtypes
class Both[A, B]
class Left[A]
class Right[B]

Members list

Value members

Concrete methods

final def ===[AA >: A, BB >: B](that: Ior[AA, BB])(implicit AA: Eq[AA], BB: Eq[BB]): Boolean

Attributes

Source
Ior.scala
final def addLeft[AA >: A](left: AA)(implicit AA: Semigroup[AA]): Ior[AA, B]

When a Left value is present in the Ior, combine it with the value specified.

When a Left value is present in the Ior, combine it with the value specified.

When the Left value is absent, set it to the value specified.

Example:

scala> import cats.data.Ior
scala> import cats.syntax.all._

scala> val ior1 = "abc".leftIor[Int]
scala> ior1.addLeft("def")
res0: Ior[String, Int] = Left(abcdef)

scala> val ior2 = 123.rightIor[String]
scala> ior2.addLeft("abc")
res1: cats.data.Ior[String,Int] = Both(abc,123)

scala> val ior3 = Ior.Both("abc",123)
scala> ior3.addLeft("def")
res2: Ior[String, Int] = Both(abcdef,123)

Attributes

Source
Ior.scala
final def addRight[BB >: B](right: BB)(implicit BB: Semigroup[BB]): Ior[A, BB]

When a Right value is present in the Ior, combine it with the value specified.

When a Right value is present in the Ior, combine it with the value specified.

When the Right value is absent, set it to the value specified.

Example:

scala> import cats.data.Ior
scala> import cats.syntax.all._

scala> val ior1 = "abc".leftIor[Int]
scala> ior1.addRight(123)
res0: Ior[String, Int] = Both(abc,123)

scala> val ior2 = 123.rightIor[String]
scala> ior2.addRight(123)
res1: Ior[String, Int] = Right(246)

scala> val ior3 = Ior.Both("abc",123)
scala> ior3.addRight(123)
res2: Ior[String, Int] = Both(abc,246)

Attributes

Source
Ior.scala
final def bimap[C, D](fa: A => C, fb: B => D): Ior[C, D]

Example:

Example:

scala> import cats.data.Ior
scala> import cats.syntax.all._

scala> "abc".leftIor[Int].bimap(_.length, identity)
res0: Ior[Int, Int] = Left(3)

scala> 123.rightIor[String].bimap(_.length, identity)
res1: Ior[Int, Int] = Right(123)

scala> Ior.Both("abc", 123).bimap(_.length, identity)
res2: Ior[Int, Int] = Both(3,123)

Attributes

Source
Ior.scala
final def combine[AA >: A, BB >: B](that: Ior[AA, BB])(implicit AA: Semigroup[AA], BB: Semigroup[BB]): Ior[AA, BB]

Example:

Example:

scala> import cats.data.Ior
scala> import cats.syntax.all._

scala> "abc".leftIor[Int].combine("other".leftIor[Int])
res0: Ior[String, Int] = Left(abcother)

scala> "abc".leftIor[Int].combine(456.rightIor[String])
res1: Ior[String, Int] = Both(abc,456)

scala> 123.rightIor[String].combine("other".leftIor[Int])
res2: Ior[String, Int] = Both(other,123)

scala> Ior.Both("abc", 123).combine(Ior.Both("other",456))
res3: Ior[String, Int] = Both(abcother,579)

scala> Ior.Both("abc", 123).combine("other".leftIor[Int])
res3: Ior[String, Int] = Both(abcother,123)

scala> Ior.Both("abc", 123).combine(456.rightIor[String])
res3: Ior[String, Int] = Both(abc,579)

Attributes

Source
Ior.scala
final def compare[AA >: A, BB >: B](that: Ior[AA, BB])(implicit AA: Order[AA], BB: Order[BB]): Int

Attributes

Source
Ior.scala
final def exists(p: B => Boolean): Boolean

Example:

Example:

scala> import cats.data.Ior
scala> import cats.syntax.all._

scala> "abc".leftIor[Int].exists(_ > 100)
res0: Boolean = false

scala> 123.rightIor[String].exists(_ > 100)
res1: Boolean = true

scala> Ior.Both("abc", 123).exists(_ > 100)
res2: Boolean = true

Attributes

Source
Ior.scala
final def flatMap[AA >: A, D](f: B => Ior[AA, D])(implicit AA: Semigroup[AA]): Ior[AA, D]

Example:

Example:

scala> import cats.data.Ior
scala> import cats.syntax.all._

scala> "abc".leftIor[Int].flatMap(i => 456.rightIor[String])
res0: Ior[String, Int] = Left(abc)

scala> 123.rightIor[String].flatMap(i => (i * 2).rightIor[String])
res1: Ior[String, Int] = Right(246)

scala> 123.rightIor[String].flatMap(_ => "error".leftIor[Int])
res2: Ior[String, Int] = Left(error)

scala> Ior.Both("abc", 123).flatMap(_ => 456.rightIor[String])
res3: Ior[String, Int] = Both(abc,456)

scala> Ior.Both("abc", 123).flatMap(_ => "error".leftIor[Int])
res4: Ior[String, Int] = Left(abcerror)

scala> Ior.Both("abc", 123).flatMap(_ => Ior.Both("error",456))
res5: Ior[String, Int] = Both(abcerror,456)

Attributes

Source
Ior.scala
final def fold[C](fa: A => C, fb: B => C, fab: (A, B) => C): C

Example:

Example:

scala> import cats.data.Ior
scala> import cats.syntax.all._

scala> val rightF: Int => String = _.show
scala> val bothF: (String,Int) => String = (a,b) => a.combine(b.show)

scala> val ior1 = "abc".leftIor[Int]
scala> ior1.fold(identity, rightF, bothF)
res0: String = abc

scala> val ior2 = 123.rightIor[String]
scala> ior2.fold(identity, rightF, bothF)
res1: String = 123

scala> val ior3 = Ior.Both("abc", 123)
scala> ior3.fold(identity, rightF, bothF)
res2: String = abc123

Attributes

Source
Ior.scala
final def foldLeft[C](c: C)(f: (C, B) => C): C

Example:

Example:

scala> import cats.data.Ior
scala> import cats.syntax.all._

scala> "abc".leftIor[Int].foldLeft(List(456))((c,b) => b :: c)
res0: List[Int] = List(456)

scala> 123.rightIor[String].foldLeft(List(456))((c,b) => b :: c)
res1: List[Int] = List(123, 456)

scala> Ior.Both("abc", 123).foldLeft(List(456))((c,b) => b :: c)
res2: List[Int] = List(123, 456)

Attributes

Source
Ior.scala
final def foldRight[C](lc: Eval[C])(f: (B, Eval[C]) => Eval[C]): Eval[C]

Attributes

Source
Ior.scala
final def forall(p: B => Boolean): Boolean

Example:

Example:

scala> import cats.data.Ior
scala> import cats.syntax.all._

scala> "abc".leftIor[Int].forall(_ > 100)
res0: Boolean = true

scala> 123.rightIor[String].forall(_ > 150)
res1: Boolean = false

scala> Ior.Both("abc", 123).forall(_ > 100)
res2: Boolean = true

Attributes

Source
Ior.scala
final def foreach(f: B => Unit): Unit

Example:

Example:

scala> import cats.data.Ior
scala> import cats.syntax.all._

// Nothing to show
scala> "abc".leftIor[Int].foreach(println)
res0: Unit = ()

// 123 to be shown
scala> 123.rightIor[String].foreach(println)
res1: Unit = ()

123 to be shown
scala> Ior.both("abc", 123).foreach(println)
res2: Unit = ()

Attributes

Source
Ior.scala
final def getOrElse[BB >: B](bb: => BB): BB

Example:

Example:

scala> import cats.data.Ior
scala> import cats.syntax.all._

scala> "abc".leftIor[Int].getOrElse(456)
res0: Int = 456

scala> 123.rightIor[String].getOrElse(456)
res1: Int = 123

scala> Ior.Both("abc", 123).getOrElse(456)
res2: Int = 123

Attributes

Source
Ior.scala
final def isBoth: Boolean

Attributes

Source
Ior.scala
final def isLeft: Boolean

Example:

Example:

scala> import cats.data.Ior
scala> import cats.syntax.all._

scala> "abc".leftIor[Int].isLeft
res0: Boolean = true

scala> 123.rightIor[String].isLeft
res1: Boolean = false

scala> Ior.Both("abc", 123).isLeft
res2: Boolean = false

Attributes

Source
Ior.scala
final def isRight: Boolean

Attributes

Source
Ior.scala
final def left: Option[A]

Example:

Example:

scala> import cats.data.Ior
scala> import cats.syntax.all._

scala> "abc".leftIor[Int].left
res0: Option[String] = Some(abc)

scala> 123.rightIor[String].left
res1: Option[String] = None

scala> Ior.Both("abc", 123).left
res2: Option[String] = Some(abc)

Attributes

Source
Ior.scala
final def leftMap[C](f: A => C): Ior[C, B]

Example:

Example:

scala> import cats.data.Ior
scala> import cats.syntax.all._

scala> "abc".leftIor[Int].leftMap(_.length)
res0: Ior[Int, Int] = Left(3)

scala> 123.rightIor[String].leftMap(_.length)
res1: Ior[Int, Int] = Right(123)

scala> Ior.Both("abc", 123).leftMap(_.length)
res2: Ior[Int, Int] = Both(3,123)

Attributes

Source
Ior.scala
final def map[D](f: B => D): Ior[A, D]

Example:

Example:

scala> import cats.data.Ior
scala> import cats.syntax.all._

scala> "abc".leftIor[Int].map(_ * 2)
res0: Ior[String, Int] = Left(abc)

scala> 123.rightIor[String].map(_ * 2)
res1: Ior[String, Int] = Right(246)

scala> Ior.Both("abc", 123).map(_ * 2)
res2: Ior[String, Int] = Both(abc,246)

Attributes

Source
Ior.scala
final def merge[AA >: A](implicit ev: B <:< AA, AA: Semigroup[AA]): AA

Attributes

Source
Ior.scala
final def mergeLeft[AA >: A](implicit ev: B <:< AA): AA

Attributes

Source
Ior.scala
final def mergeRight[AA >: A](implicit ev: B <:< AA): AA

Attributes

Source
Ior.scala
final def mergeWith[AA >: A](f: (A, B) => AA)(implicit ev: B <:< AA): AA

Attributes

Source
Ior.scala
final def onlyBoth: Option[(A, B)]

Example:

Example:

scala> import cats.data.Ior
scala> import cats.syntax.all._

scala> "abc".leftIor[Int].onlyBoth
res0: Option[(String, Int)] = None

scala> 123.rightIor[String].onlyBoth
res1: Option[(String, Int)] = None

scala> Ior.Both("abc", 123).onlyBoth
res2: Option[(String, Int)] = Some((abc,123))

Attributes

Source
Ior.scala
final def onlyLeft: Option[A]

Example:

Example:

scala> import cats.data.Ior
scala> import cats.syntax.all._

scala> "abc".leftIor[Int].onlyLeft
res0: Option[String] = Some(abc)

scala> 123.rightIor[String].onlyLeft
res1: Option[String] = None

scala> Ior.Both("abc", 123).onlyLeft
res2: Option[String] = None

Attributes

Source
Ior.scala
final def onlyLeftOrRight: Option[Either[A, B]]

Example:

Example:

scala> import cats.data.Ior
scala> import cats.syntax.all._

scala> "abc".leftIor[Int].onlyLeftOrRight
res0: Option[Either[String, Int]] = Some(Left(abc))

scala> 123.rightIor[String].onlyLeftOrRight
res1: Option[Either[String, Int]] = Some(Right(123))

scala> Ior.Both("abc", 123).onlyLeftOrRight
res2: Option[Either[String, Int]] = None

Attributes

Source
Ior.scala
final def onlyRight: Option[B]

Example:

Example:

scala> import cats.data.Ior
scala> import cats.syntax.all._

scala> "abc".leftIor[Int].onlyRight
res0: Option[Int] = None

scala> 123.rightIor[String].onlyRight
res1: Option[Int] = Some(123)

scala> Ior.Both("abc", 123).onlyRight
res2: Option[Int] = None

Attributes

Source
Ior.scala
final def pad: (Option[A], Option[B])

Example:

Example:

scala> import cats.data.Ior
scala> import cats.syntax.all._

scala> "abc".leftIor[Int].pad
res0: (Option[String], Option[Int]) = (Some(abc),None)

scala> 123.rightIor[String].pad
res1: (Option[String], Option[Int]) = (None,Some(123))

scala> Ior.Both("abc", 123).pad
res2: (Option[String], Option[Int]) = (Some(abc),Some(123))

Attributes

Source
Ior.scala
final def putLeft[C](left: C): Ior[C, B]

Example:

Example:

scala> import cats.data.Ior
scala> import cats.syntax.all._

scala> val ior1 = "abc".leftIor[Int]
scala> ior1.putLeft(true)
res0: Ior[Boolean, Int] = Left(true)

scala> val ior2 = 123.rightIor[String]
scala> ior2.putLeft(false)
res1: Ior[Boolean, Int] = Both(false,123)

scala> val ior3 = Ior.Both("abc",123)
scala> ior3.putLeft(true)
res2: Ior[Boolean, Int] = Both(true,123)

Attributes

Source
Ior.scala
final def putRight[C](right: C): Ior[A, C]

Example:

Example:

scala> import cats.data.Ior
scala> import cats.syntax.all._

scala> val ior1 = "abc".leftIor[Int]
scala> ior1.putRight(123L)
res0: Ior[String, Long] = Both(abc,123)

scala> val ior2 = 123.rightIor[String]
scala> ior2.putRight(123L)
res1: Ior[String, Long] = Right(123)

scala> val ior3 = Ior.Both("abc",123)
scala> ior3.putRight(123L)
res2: Ior[String, Long] = Both(abc,123)

Attributes

Source
Ior.scala
final def right: Option[B]

Example:

Example:

scala> import cats.data.Ior
scala> import cats.syntax.all._

scala> "abc".leftIor[Int].right
res0: Option[Int] = None

scala> 123.rightIor[String].right
res1: Option[Int] = Some(123)

scala> Ior.Both("abc", 123).right
res2: Option[Int] = Some(123)

Attributes

Source
Ior.scala
final def swap: Ior[B, A]

Example:

Example:

scala> import cats.data.Ior
scala> import cats.syntax.all._

scala> "abc".leftIor[Int].swap
res0: Ior[Int, String] = Right(abc)

scala> 123.rightIor[String].swap
res1: Ior[Int, String] = Left(123)

scala> Ior.Both("abc", 123).swap
res2: Ior[Int, String] = Both(123,abc)

Attributes

Source
Ior.scala
final def to[F[_], BB >: B](implicit F: Alternative[F]): F[BB]

Example:

Example:

scala> import cats.data.Ior
scala> import cats.syntax.all._

scala> "abc".leftIor[Int].to[List, Int]
res0: List[Int] = List()

scala> 123.rightIor[String].to[List, Int]
res1: List[Int]= List(123)

scala> Ior.Both("abc", 123).to[List, Int]
res2: List[Int] = List(123)

Attributes

Source
Ior.scala
final def toEither: Either[A, B]

Example:

Example:

scala> import cats.data.Ior
scala> import cats.syntax.all._

scala> "abc".leftIor[Int].toEither
res0: Either[String, Int] = Left(abc)

scala> 123.rightIor[String].toEither
res1: Either[String, Int]= Right(123)

scala> Ior.Both("abc", 123).toEither
res2: Either[String, Int] = Right(123)

Attributes

Source
Ior.scala
final def toIorNec[AA >: A]: IorNec[AA, B]

Attributes

Source
Ior.scala
final def toIorNel[AA >: A]: IorNel[AA, B]

Example:

Example:

scala> import cats.data.Ior
scala> import cats.syntax.all._

scala> "abc".leftIor[Int].toIorNel
res0: IorNel[String, Int] = Left(NonEmptyList(abc))

scala> 123.rightIor[String].toIorNel
res1: IorNel[String, Int]= Right(123)

scala> Ior.Both("abc", 123).toIorNel
res2: IorNel[String, Int] = Both(NonEmptyList(abc),123)

Attributes

Source
Ior.scala
final def toIorNes[AA >: A](implicit O: Order[AA]): IorNes[AA, B]

Example:

Example:

scala> import cats.data.Ior
scala> import cats.syntax.all._

scala> "abc".leftIor[Int].toIorNes
res0: IorNes[String, Int] = Left(TreeSet(abc))

scala> 123.rightIor[String].toIorNes
res1: IorNes[String, Int]= Right(123)

scala> Ior.Both("abc", 123).toIorNes
res2: IorNes[String, Int] = Both(TreeSet(abc),123)

Attributes

Source
Ior.scala
final def toList: List[B]

Example:

Example:

scala> import cats.data.Ior
scala> import cats.syntax.all._

scala> "abc".leftIor[Int].toList
res0: List[Int] = List()

scala> 123.rightIor[String].toList
res1: List[Int]= List(123)

scala> Ior.Both("abc", 123).toList
res2: List[Int] = List(123)

Attributes

Source
Ior.scala
final def toOption: Option[B]

Example:

Example:

scala> import cats.data.Ior
scala> import cats.syntax.all._

scala> "abc".leftIor[Int].toOption
res0: Option[Int] = None

scala> 123.rightIor[String].toOption
res1: Option[Int]= Some(123)

scala> Ior.Both("abc", 123).toOption
res2: Option[Int] = Some(123)

Attributes

Source
Ior.scala
final def toValidated: Validated[A, B]

Example:

Example:

scala> import cats.data.Ior
scala> import cats.syntax.all._

scala> "abc".leftIor[Int].toValidated
res0: Validated[String, Int] = Invalid(abc)

scala> 123.rightIor[String].toValidated
res1: Validated[String, Int]= Valid(123)

scala> Ior.Both("abc", 123).toValidated
res2: Validated[String, Int] = Valid(123)

Attributes

Source
Ior.scala
final def traverse[F[_], AA >: A, D](g: B => F[D])(implicit F: Applicative[F]): F[Ior[AA, D]]

Example

Example

scala> import cats.data.Ior
scala> import cats.syntax.all._

scala> "abc".leftIor[Int].traverse(i => List(i, i * 2))
res0: List[Ior[String,Int]] = List(Left(abc))

scala> 123.rightIor[String].traverse(i => List(i, i * 2))
res1: List[Ior[String,Int]] = List(Right(123), Right(246))

scala> Ior.both("abc", 123).traverse(i => List(i, i * 2))
res2: List[Ior[String,Int]] = List(Both(abc,123), Both(abc,246))

Attributes

Source
Ior.scala
final def unwrap: Either[Either[A, B], (A, B)]

Example:

Example:

scala> import cats.data.Ior
scala> import cats.syntax.all._

scala> "abc".leftIor[Int].unwrap
res0: Either[Either[String, Int], (String, Int)] = Left(Left(abc))

scala> 123.rightIor[String].unwrap
res1: Either[Either[String, Int], (String, Int)] = Left(Right(123))

scala> Ior.Both("abc", 123).unwrap
res2: Either[Either[String, Int], (String, Int)] = Right((abc,123))

Attributes

Source
Ior.scala
final def valueOr[BB >: B](f: A => BB)(implicit BB: Semigroup[BB]): BB

Example:

Example:

scala> import cats.data.Ior
scala> import cats.syntax.all._

scala> "abc".leftIor[Int].valueOr(_.length)
res0: Int = 3

scala> 123.rightIor[String].valueOr(_.length)
res1: Int = 123

scala> Ior.Both("abc", 123).valueOr(_.length)
res2: Int = 126

Attributes

Source
Ior.scala

Inherited methods

def canEqual(that: Any): Boolean

Attributes

Inherited from:
Equals

Attributes

Inherited from:
Product
def productElement(n: Int): Any

Attributes

Inherited from:
Product

Attributes

Inherited from:
Product

Attributes

Inherited from:
Product

Attributes

Inherited from:
Product

Attributes

Inherited from:
Product