Bitraverse

cats.Bitraverse
See theBitraverse companion object
trait Bitraverse[F[_, _]] extends Bifoldable[F], Bifunctor[F]

A type class abstracting over types that give rise to two independent cats.Traverses.

Attributes

Companion
object
Source
Bitraverse.scala
Graph
Supertypes
trait Bifunctor[F]
trait Bifoldable[F]
trait Serializable
class Object
trait Matchable
class Any
Show all
Known subtypes
class BinestedBitraverse[F, G, H]
Self type

Members list

Value members

Abstract methods

def bitraverse[G[_] : Applicative, A, B, C, D](fab: F[A, B])(f: A => G[C], g: B => G[D]): G[F[C, D]]

Traverse each side of the structure with the given functions.

Traverse each side of the structure with the given functions.

Example:

scala> import cats.syntax.all._

scala> def parseInt(s: String): Option[Int] = Either.catchOnly[NumberFormatException](s.toInt).toOption

scala> ("1", "2").bitraverse(parseInt, parseInt)
res0: Option[(Int, Int)] = Some((1,2))

scala> ("1", "two").bitraverse(parseInt, parseInt)
res1: Option[(Int, Int)] = None

Attributes

Source
Bitraverse.scala

Concrete methods

override def bimap[A, B, C, D](fab: F[A, B])(f: A => C, g: B => D): F[C, D]

The quintessential method of the Bifunctor trait, it applies a function to each "side" of the bifunctor.

The quintessential method of the Bifunctor trait, it applies a function to each "side" of the bifunctor.

Example:

scala> import cats.syntax.all._

scala> val x: (List[String], Int) = (List("foo", "bar"), 3)
scala> x.bimap(_.headOption, _.toLong + 1)
res0: (Option[String], Long) = (Some(foo),4)

Attributes

Definition Classes
Source
Bitraverse.scala
def bisequence[G[_] : Applicative, A, B](fab: F[G[A], G[B]]): G[F[A, B]]

Invert the structure from F[G[A], G[B]] to G[F[A, B]].

Invert the structure from F[G[A], G[B]] to G[F[A, B]].

Example:

scala> import cats.syntax.all._

scala> val rightSome: Either[Option[String], Option[Int]] = Either.right(Some(3))
scala> rightSome.bisequence
res0: Option[Either[String, Int]] = Some(Right(3))

scala> val rightNone: Either[Option[String], Option[Int]] = Either.right(None)
scala> rightNone.bisequence
res1: Option[Either[String, Int]] = None

scala> val leftSome: Either[Option[String], Option[Int]] = Either.left(Some("foo"))
scala> leftSome.bisequence
res2: Option[Either[String, Int]] = Some(Left(foo))

scala> val leftNone: Either[Option[String], Option[Int]] = Either.left(None)
scala> leftNone.bisequence
res3: Option[Either[String, Int]] = None

Attributes

Source
Bitraverse.scala
def compose[G[_, _]](implicit ev: Bitraverse[G]): Bitraverse[[α, β] =>> F[G[α, β], G[α, β]]]

If F and G are both cats.Bitraverse then so is their composition F[G[_, _], G[_, _]]

If F and G are both cats.Bitraverse then so is their composition F[G[_, _], G[_, _]]

Attributes

Source
Bitraverse.scala
def leftSequence[G[_], A, B](fgab: F[G[A], B])(implicit G: Applicative[G]): G[F[A, B]]

Sequence the left side of the structure.

Sequence the left side of the structure. For the right side, use the standard sequence from cats.Traverse.

Example:

scala> import cats.syntax.all._

scala> val optionalErrorRight: Either[Option[String], Int] = Either.right(123)
scala> optionalErrorRight.leftSequence
res1: Option[Either[String, Int]] = Some(Right(123))

scala> val optionalErrorLeftSome: Either[Option[String], Int] = Either.left(Some("something went wrong"))
scala> optionalErrorLeftSome.leftSequence
res2: Option[Either[String, Int]] = Some(Left(something went wrong))

scala> val optionalErrorLeftNone: Either[Option[String], Int] = Either.left(None)
scala> optionalErrorLeftNone.leftSequence
res3: Option[Either[String,Int]] = None

Attributes

Source
Bitraverse.scala
def leftTraverse[G[_], A, B, C](fab: F[A, B])(f: A => G[C])(implicit G: Applicative[G]): G[F[C, B]]

Traverse over the left side of the structure.

Traverse over the left side of the structure. For the right side, use the standard traverse from cats.Traverse.

Example:

scala> import cats.syntax.all._

scala> val intAndString: (Int, String) = (7, "test")

scala> Bitraverse[Tuple2].leftTraverse(intAndString)(i => Option(i).filter(_ > 5))
res1: Option[(Int, String)] = Some((7,test))

scala> Bitraverse[Tuple2].leftTraverse(intAndString)(i => Option(i).filter(_ < 5))
res2: Option[(Int, String)] = None

Attributes

Source
Bitraverse.scala

Inherited methods

def bifold[A, B](fab: F[A, B])(implicit A: Monoid[A], B: Monoid[B]): (A, B)

Attributes

Inherited from:
Bifoldable
Source
Bifoldable.scala
def bifoldLeft[A, B, C](fab: F[A, B], c: C)(f: (C, A) => C, g: (C, B) => C): C

Collapse the structure with a left-associative function

Collapse the structure with a left-associative function

Example:

scala> import cats.Bifoldable
scala> val fab = (List(1), 2)

Folding by addition to zero:
scala> Bifoldable[Tuple2].bifoldLeft(fab, Option(0))((c, a) => c.map(_ + a.head), (c, b) => c.map(_ + b))
res0: Option[Int] = Some(3)

With syntax extensions, bifoldLeft can be used like:

scala> import cats.syntax.all._
scala> fab.bifoldLeft(Option(0))((c, a) => c.map(_ + a.head), (c, b) => c.map(_ + b))
res1: Option[Int] = Some(3)

Attributes

Inherited from:
Bifoldable
Source
Bifoldable.scala
def bifoldMap[A, B, C](fab: F[A, B])(f: A => C, g: B => C)(implicit C: Monoid[C]): C

Collapse the structure by mapping each element to an element of a type that has a cats.Monoid

Collapse the structure by mapping each element to an element of a type that has a cats.Monoid

Attributes

Inherited from:
Bifoldable
Source
Bifoldable.scala
def bifoldRight[A, B, C](fab: F[A, B], c: Eval[C])(f: (A, Eval[C]) => Eval[C], g: (B, Eval[C]) => Eval[C]): Eval[C]

Collapse the structure with a right-associative function Right associative lazy bifold on F using the folding function 'f' and 'g'.

Collapse the structure with a right-associative function Right associative lazy bifold on F using the folding function 'f' and 'g'.

This method evaluates c lazily (in some cases it will not be needed), and returns a lazy value. We are using (_, Eval[C]) => Eval[C] to support laziness in a stack-safe way. Chained computation should be performed via .map and .flatMap.

For more detailed information about how this method works see the documentation for Eval[_].

Example:

scala> import cats.Bifoldable
scala> val fab = (List(1), 2)

Folding by addition to zero:
scala> val bifolded1 = Bifoldable[Tuple2].bifoldRight(fab, Eval.now(0))((a, c) => c.map(_ + a.head), (b, c) => c.map(_ + b))
scala> bifolded1.value
res0: Int = 3

With syntax extensions, bifoldRight can be used like:

scala> import cats.syntax.all._
scala> val bifolded2 = fab.bifoldRight(Eval.now(0))((a, c) => c.map(_ + a.head), (b, c) => c.map(_ + b))
scala> bifolded2.value
res1: Int = 3

Attributes

Inherited from:
Bifoldable
Source
Bifoldable.scala
def compose[G[_, _]](implicit G0: Bifunctor[G]): Bifunctor[[α, β] =>> F[G[α, β], G[α, β]]]

The composition of two Bifunctors is itself a Bifunctor

The composition of two Bifunctors is itself a Bifunctor

Attributes

Inherited from:
Bifunctor
Source
Bifunctor.scala
def compose[G[_, _]](implicit ev: Bifoldable[G]): Bifoldable[[α, β] =>> F[G[α, β], G[α, β]]]

Attributes

Inherited from:
Bifoldable
Source
Bifoldable.scala
def leftFunctor[X]: Functor[F]

Attributes

Inherited from:
Bifunctor
Source
Bifunctor.scala
def leftLiftTo[A, B, C[_]](fab: F[A, B])(implicit C: Applicative[C]): F[C[A], B]

Lift left into F using Applicative.

Lift left into F using Applicative. * Example:

scala> import cats.implicits._
scala> val x0: Either[String, Int] = Either.left("foo")
scala> val x1: Either[List[String], Int] = x0.leftLiftTo[List]

Attributes

Inherited from:
Bifunctor
Source
Bifunctor.scala
def leftMap[A, B, C](fab: F[A, B])(f: A => C): F[C, B]

apply a function to the "left" functor

apply a function to the "left" functor

Attributes

Inherited from:
Bifunctor
Source
Bifunctor.scala
def leftWiden[A, B, AA >: A](fab: F[A, B]): F[AA, B]

Widens A into a supertype AA.

Widens A into a supertype AA. Example:

scala> import cats.syntax.all._
scala> sealed trait Foo
scala> case object Bar extends Foo
scala> val x1: Either[Bar.type, Int] = Either.left(Bar)
scala> val x2: Either[Foo, Int] = x1.leftWiden

Attributes

Inherited from:
Bifunctor
Source
Bifunctor.scala
def rightFunctor[X]: Functor[[_] =>> F[X, _$3]]

Attributes

Inherited from:
Bifunctor
Source
Bifunctor.scala