Skip to main content

F[Either[A, B]]

Extension Methods for F[Either[A, B]]

Change Value

innerMap

val foa: F[Either[A, B]] = ...
foa.innerMap(B => D) // F[Either[A, D]]

Example

import cats.syntax.all._
import cats.effect._

import extras.cats.syntax.all._
val foa = IO.pure(1.asRight[String])
// foa: IO[Either[String, Int]] = Pure(a = Right(value = 1))

foa.innerMap(_ + 999)
.unsafeRunSync()
// res1: Either[String, Int] = Right(value = 1000)

innerFlatMap

val feab: F[Either[A, B]] = ...
feab.innerFlatMap(B => Either[A, D]) // F[Either[A, D]]

Example

import cats.syntax.all._
import cats.effect._

import extras.cats.syntax.all._

val feab = IO.pure(1.asRight[String])
// feab: IO[Either[String, Int]] = Pure(a = Right(value = 1))

feab.innerFlatMap(b => (b + 999).asRight[String])
.unsafeRunSync()
// res3: Either[String, Int] = Right(value = 1000)

innerFlatMapF

val feab: F[Either[A, B]] = ...

feab.innerFlatMapF(B => F[Either[A, D]]) // F[Either[A, D]]

Example

import cats.syntax.all._
import cats.effect._

import extras.cats.syntax.all._

val feab = IO.pure(1.asRight[String])
// feab: IO[Either[String, Int]] = Pure(a = Right(value = 1))

feab.innerFlatMapF(b => IO.pure((b + 999).asRight))
.unsafeRunSync()
// res5: Either[String, Int] = Right(value = 1000)

innerLeftMap

val feab: F[Either[A, B]] = ...
feab.innerLeftMap(A => C) // F[Either[C, B]]

Example

import cats.syntax.all._
import cats.effect._

import extras.cats.syntax.all._

val feab = IO.pure("Error".asLeft[Int])
// feab: IO[Either[String, Int]] = Pure(a = Left(value = "Error"))

feab.innerLeftMap("Failed: " + _)
.unsafeRunSync()
// res7: Either[String, Int] = Left(value = "Failed: Error")

innerLeftFlatMap

val feab: F[Either[A, B]] = ...
feab.innerFlatMap(A => Either[C, B]) // F[Either[C, B]]

Example

import cats.syntax.all._
import cats.effect._

import extras.cats.syntax.all._

val feab = IO.pure("Error".asLeft[Int])
// feab: IO[Either[String, Int]] = Pure(a = Left(value = "Error"))

feab.innerLeftFlatMap(a => ("Failed: " + a).asLeft[Int])
.unsafeRunSync()
// res9: Either[String, Int] = Left(value = "Failed: Error")

innerLeftFlatMapF

val feab: F[Either[A, B]] = ...
feab.innerFlatMapF(A => F[Either[C, B]]) // F[Either[C, B]]

Example

import cats.syntax.all._
import cats.effect._

import extras.cats.syntax.all._

val feab = IO.pure("Error".asLeft[Int])
// feab: IO[Either[String, Int]] = Pure(a = Left(value = "Error"))

feab.innerLeftFlatMapF(a => IO.pure(("Failed: " + a).asLeft[Int]))
.unsafeRunSync()
// res11: Either[String, Int] = Left(value = "Failed: Error")

Get Value

innerGetOrElse

val feab: F[Either[A, B]] = ...
feab.innerGetOrElse[D >: B](=> D) // F[D]

Example

import cats.syntax.all._
import cats.effect._

import extras.cats.syntax.all._

val feab = IO.pure(999.asRight[String])
// feab: IO[Either[String, Int]] = Pure(a = Right(value = 999))

feab.innerGetOrElse(0)
.unsafeRunSync()
// res13: Int = 999

val feab2 = IO.pure("Error".asLeft[Int])
// feab2: IO[Either[String, Int]] = Pure(a = Left(value = "Error"))

feab2.innerGetOrElse(0)
.unsafeRunSync()
// res14: Int = 0

innerGetOrElseF

val feab: F[Either[A, B]] = ...
feab.innerGetOrElseF[D >: B](=> F[D]) // F[D]

Example

import cats.syntax.all._
import cats.effect._

import extras.cats.syntax.all._

val feab = IO.pure(999.asRight[String])
// feab: IO[Either[String, Int]] = Pure(a = Right(value = 999))

feab.innerGetOrElseF(IO.pure(0))
.unsafeRunSync()
// res16: Int = 999

val feab2 = IO.pure("Error".asLeft[Int])
// feab2: IO[Either[String, Int]] = Pure(a = Left(value = "Error"))

feab2.innerGetOrElseF(IO.pure(0))
.unsafeRunSync()
// res17: Int = 0

innerFold

val feab: F[Either[A, B]] = ...
feab.innerFold[D](=> D)(B => D) // F[D]

Example

import cats.syntax.all._
import cats.effect._

import extras.cats.syntax.all._

val feab = IO.pure(1.asRight[String])
// feab: IO[Either[String, Int]] = Pure(a = Right(value = 1))

feab.innerFold(_ => 0)(_ + 999)
.unsafeRunSync()
// res19: Int = 1000

val feab2 = IO.pure("Error".asLeft[Int])
// feab2: IO[Either[String, Int]] = Pure(a = Left(value = "Error"))

feab2.innerFold(_ => 0)(_ + 999)
.unsafeRunSync()
// res20: Int = 0

innerFoldF

val feab: F[Either[A, B]] = ...
feab.innerFoldF[D](=> F[D])(B => F[D]) // F[D]

Example

import cats.syntax.all._
import cats.effect._

import extras.cats.syntax.all._

val feab = IO.pure(1.asRight[String])
// feab: IO[Either[String, Int]] = Pure(a = Right(value = 1))

feab.innerFoldF(_ => IO.pure(0))(b => IO.pure(b + 999))
.unsafeRunSync()
// res22: Int = 1000

val feab2 = IO.pure("Error".asLeft[Int])
// feab2: IO[Either[String, Int]] = Pure(a = Left(value = "Error"))

feab2.innerFoldF(_ => IO.pure(0))(b => IO.pure(b + 999))
.unsafeRunSync()
// res23: Int = 0

innerOrElse

val feab: F[Either[A, B]] = ...
feab.innerOrElse[C >: A, D >: B](=> Either[C, D]): F[Either[C, D]]

Example

import cats.syntax.all._
import cats.effect._

import extras.cats.syntax.all._

val feab = IO(1.asRight[String])
// feab: IO[Either[String, Int]] = Delay(
// thunk = <function0>,
// trace = StackTrace(
// ...

feab.innerOrElse(0.asRight[String])
.unsafeRunSync()
// res25: Either[String, Int] = Right(value = 1)

val feab2 = IO("Error".asLeft[Int])
// feab2: IO[Either[String, Int]] = Delay(
// thunk = <function0>,
// trace = StackTrace(
// ...

feab2.innerOrElse(0.asRight[String])
.unsafeRunSync()
// res26: Either[String, Int] = Right(value = 0)

innerOrElseF

val feab: F[Either[A, B]] = ...
feab.innerOrElseF[C >: A, D >: B](=> F[Either[C, D]]): F[Either[C, D]]

Example

import cats.syntax.all._
import cats.effect._

import extras.cats.syntax.all._

val feab = IO(1.asRight[String])
// feab: IO[Either[String, Int]] = Delay(
// thunk = <function0>,
// trace = StackTrace(
// ...

feab.innerOrElseF(IO.pure(0.asRight[String]))
.unsafeRunSync()
// res28: Either[String, Int] = Right(value = 1)

val feab2 = IO("Error".asLeft[Int])
// feab2: IO[Either[String, Int]] = Delay(
// thunk = <function0>,
// trace = StackTrace(
// ...

feab2.innerOrElseF(IO.pure(0.asRight[String]))
.unsafeRunSync()
// res29: Either[String, Int] = Right(value = 0)

Do

innerForeach

val feab: F[Either[A, B]] = ...
feab.innerForeach(B => Unit) // F[Unit]

Example

import cats.syntax.all._
import cats.effect._

import extras.cats.syntax.all._

val feab = IO.pure(1.asRight[String])
// feab: IO[Either[String, Int]] = Pure(a = Right(value = 1))

feab.innerForeach(println)
.unsafeRunSync()
// 1

val feab2 = IO.pure("Error".asLeft[Int])
// feab2: IO[Either[String, Int]] = Pure(a = Left(value = "Error"))

feab2.innerForeach(println)
.unsafeRunSync()

innerForeachF

val feab: F[Either[A, B]] = ...
feab.innerForeachF(B => F[Unit]) // F[Unit]

Example

import cats.syntax.all._
import cats.effect._

import extras.cats.syntax.all._

val feab = IO.pure(1.asRight[String])
// feab: IO[Either[String, Int]] = Pure(a = Right(value = 1))

feab.innerForeachF(b => IO.delay(println(b)))
.unsafeRunSync()
// 1

val feab2 = IO.pure("Error".asLeft[Int])
// feab2: IO[Either[String, Int]] = Pure(a = Left(value = "Error"))

feab2.innerForeachF(b => IO.delay(println(b)))
.unsafeRunSync()

innerFind

val feab: F[Either[A, B]] = ...
feab.innerFind(B => Boolean): F[Option[B]]

Example

import cats.syntax.all._
import cats.effect._

import extras.cats.syntax.all._

val feab = IO(1.asRight[String])
// feab: IO[Either[String, Int]] = Delay(
// thunk = <function0>,
// trace = StackTrace(
// ...

feab.innerFind(_ > 0)
.unsafeRunSync()
// res37: Option[Int] = Some(value = 1)

feab.innerFind(_ > 1)
.unsafeRunSync()
// res38: Option[Int] = None

innerFilterOrElse

val feab: F[Either[A, B]] = ...
feab.innerFilterOrElse[C >: A](B => Boolean, => C): F[Either[C, B]]

Example

import cats.syntax.all._
import cats.effect._

import extras.cats.syntax.all._

val feab = IO(1.asRight[String])
// feab: IO[Either[String, Int]] = Delay(
// thunk = <function0>,
// trace = StackTrace(
// ...

feab.innerFilterOrElse(_ > 0, "Error")
.unsafeRunSync()
// res40: Either[String, Int] = Right(value = 1)

feab.innerFilterOrElse(_ > 1, "Error")
.unsafeRunSync()
// res41: Either[String, Int] = Left(value = "Error")

innerExists

val feab: F[Either[A, B]] = ...
feab.innerExists(B => Boolean): F[Boolean]

Example

import cats.syntax.all._
import cats.effect._

import extras.cats.syntax.all._

val feab = IO(1.asRight[String])
// feab: IO[Either[String, Int]] = Delay(
// thunk = <function0>,
// trace = StackTrace(
// ...

feab.innerExists(_ > 0)
.unsafeRunSync()
// res43: Boolean = true

feab.innerExists(_ > 1)
.unsafeRunSync()
// res44: Boolean = false

innerForall

val feab: F[Either[A, B]] = ...
feab.innerForall(B => Boolean): F[Boolean]

Example

import cats.syntax.all._
import cats.effect._

import extras.cats.syntax.all._

val feab = IO(1.asRight[String])
// feab: IO[Either[String, Int]] = Delay(
// thunk = <function0>,
// trace = StackTrace(
// ...

feab.innerForall(_ > 0)
.unsafeRunSync()
// res46: Boolean = true

feab.innerForall(_ > 1)
.unsafeRunSync()
// res47: Boolean = false

val feab2 = IO("Error".asLeft[Int])
// feab2: IO[Either[String, Int]] = Delay(
// thunk = <function0>,
// trace = StackTrace(
// ...

feab2.innerForall(_ > 1)
.unsafeRunSync()
// res48: Boolean = true

innerContains

val feab: F[Either[A, B]] = ...
feab.innerContains(B): F[Boolean]

Example

import cats.syntax.all._
import cats.effect._

import extras.cats.syntax.all._

val feab = IO(1.asRight[String])
// feab: IO[Either[String, Int]] = Delay(
// thunk = <function0>,
// trace = StackTrace(
// ...

feab.innerContains(1)
.unsafeRunSync()
// res50: Boolean = true

feab.innerContains(0)
.unsafeRunSync()
// res51: Boolean = false

val feab2 = IO("Error".asLeft[Int])
// feab2: IO[Either[String, Int]] = Delay(
// thunk = <function0>,
// trace = StackTrace(
// ...

feab2.innerContains(1)
.unsafeRunSync()
// res52: Boolean = false

innerCollectFirst

val feab: F[Either[A, B]] = ...
feab.innerCollectFirst[D](PartialFunction[B, D]): F[Option[D]]

Example

import cats.syntax.all._
import cats.effect._

import extras.cats.syntax.all._

val feab = IO(1.asRight[String])
// feab: IO[Either[String, Int]] = Delay(
// thunk = <function0>,
// trace = StackTrace(
// ...

feab.innerCollectFirst {
case 1 => 0
case 2 => 999
}
.unsafeRunSync()
// res54: Option[Int] = Some(value = 0)

val feab2 = IO(2.asRight[String])
// feab2: IO[Either[String, Int]] = Delay(
// thunk = <function0>,
// trace = StackTrace(
// ...
// IO[Either[String, Int]] = IO(Right(2))
feab2.innerCollectFirst {
case 1 => 0
case 2 => 999
}
.unsafeRunSync()
// res55: Option[Int] = Some(value = 999)

val feab3 = IO(3.asRight[String])
// feab3: IO[Either[String, Int]] = Delay(
// thunk = <function0>,
// trace = StackTrace(
// ...
// IO[Either[String, Int]] = IO(Right(3))
feab3.innerCollectFirst {
case 1 => 0
case 2 => 999
}
.unsafeRunSync()
// res56: Option[Int] = None

val feab4 = IO("Error".asLeft[Int])
// feab4: IO[Either[String, Int]] = Delay(
// thunk = <function0>,
// trace = StackTrace(
// ...
// IO[Either[String, Int]] = IO(Left("Error"))
feab4.innerCollectFirst {
case 1 => 0
case 2 => 999
}
.unsafeRunSync()
// res57: Option[Int] = None