Ior

Ior represents an inclusive-or relationship between two data types. This makes it very similar to the Either data type, which represents an “exclusive-or” relationship. What this means, is that an Ior[A, B] (also written as A Ior B) can contain either an A, a B, or both an A and B. Another similarity to Either is that Ior is right-biased, which means that the map and flatMap functions will work on the right side of the Ior, in our case the B value. You can see this in the function signature of map:

def map[B, C](fa: A Ior B)(f: B => C): A Ior C

We can create Ior values using Ior.left, Ior.right and Ior.both:

scala> import cats.data._
import cats.data._

scala> val right = Ior.right[String, Int](3)
right: cats.data.Ior[String,Int] = Right(3)

scala> val left = Ior.left[String, Int]("Error")
left: cats.data.Ior[String,Int] = Left(Error)

scala> val both = Ior.both("Warning", 3)
both: cats.data.Ior[String,Int] = Both(Warning,3)

Cats also offers syntax enrichment for Ior. The leftIor and rightIor functions can be imported from cats.syntax.ior._:

scala> import cats.implicits._
import cats.implicits._

scala> val right = 3.rightIor
right: cats.data.Ior[Nothing,Int] = Right(3)

scala> val left = "Error".leftIor
left: cats.data.Ior[String,Nothing] = Left(Error)

When we look at the Monad or Applicative instances of Ior, we can see that they actually requires a Semigroup instance on the left side. This is because Ior will actually accumulate failures on the left side, very similar to how the Validated data type does. This means we can accumulate data on the left side while also being able to short-circuit upon the first left-side-only value. For example, sometimes, we might want to accumulate warnings together with a valid result and only halt the computation on a “hard error” Here’s an example of how we might be able to do that:

import cats.implicits._
import cats.data.{ NonEmptyChain => Nec }

type Failures = Nec[String]

case class Username(value: String) extends AnyVal
case class Password(value: String) extends AnyVal

case class User(name: Username, pw: Password)

def validateUsername(u: String): Failures Ior Username = {
  if (u.isEmpty)
    Nec.one("Can't be empty").leftIor
  else if (u.contains("."))
    Ior.both(Nec.one("Dot in name is deprecated"), Username(u))
  else
    Username(u).rightIor
}

def validatePassword(p: String): Failures Ior Password = {
  if (p.length < 8)
    Nec.one("Password too short").leftIor
  else if (p.length < 10)
    Ior.both(Nec.one("Password should be longer"), Password(p))
  else
    Password(p).rightIor
}

def validateUser(name: String, password: String): Failures Ior User =
  (validateUsername(name), validatePassword(password)).mapN(User)

Now we’re able to validate user data and also accumulate non-fatal warnings:

scala> validateUser("John", "password12")
res0: cats.data.Ior[Failures,User] = Right(User(Username(John),Password(password12)))

scala> validateUser("john.doe", "password")
res1: cats.data.Ior[Failures,User] = Both(Chain(Dot in name is deprecated, Password should be longer),User(Username(john.doe),Password(password)))

scala> validateUser("jane", "short")
res2: cats.data.Ior[Failures,User] = Left(Chain(Password too short))

To extract the values, we can use the fold method, which expects a function for each case the Ior can represent:

scala> validateUser("john.doe", "password").fold(
     |   errorNec => s"Error: ${errorNec.head}",
     |   user => s"Success: $user",
     |   (warnings, user) => s"Warning: ${user.name.value}; The following warnings occurred: ${warnings.show}"
     | )
res3: String = Warning: john.doe; The following warnings occurred: NonEmptyChain(Dot in name is deprecated, Password should be longer)

Similar to Validated, there is also a type alias for using a NonEmptyChain on the left side.

type IorNec[B, A] = Ior[NonEmptyChain[B], A]
scala> import cats.implicits._, cats.data.NonEmptyChain
import cats.implicits._
import cats.data.NonEmptyChain

scala> val left: IorNec[String, Int] = Ior.fromEither("Error".leftNec[Int])
left: IorNec[String,Int] = Left(Chain(Error))

We can also convert our Ior to Either, Validated or Option. All of these conversions will discard the left side value if both are available:

scala> Ior.both("Warning", 42).toEither
res4: Either[String,Int] = Right(42)