ArrowChoice yields Arrows with choice, allowing distribution over coproducts.
ArrowChoice yields Arrows with choice, allowing distribution over coproducts.
Given two F
s (f
and g
), create a new F
with
domain the coproduct of the domains of f
and g
,
and codomain the coproduct of the codomains of f
and g
.
This is the sum notion to split
's product.
Example:
scala> import cats.implicits._ scala> val toLong: Int => Long = _.toLong scala> val toDouble: Float => Double = _.toDouble scala> val f: Either[Int, Float] => Either[Long, Double] = toLong +++ toDouble scala> f(Left(3)) res0: Either[Long,Double] = Left(3) scala> f(Right(3)) res1: Either[Long,Double] = Right(3.0)
Create a new F
that takes two inputs, but only modifies the first input
Create a new F
that takes two inputs, but only modifies the first input
Example:
scala> import cats.implicits._ scala> import cats.arrow.Strong scala> val f: Int => Int = _ * 2 scala> val fab = Strong[Function1].first[Int,Int,Int](f) scala> fab((2,3)) res0: (Int, Int) = (4,3)
Lift a function into the context of an Arrow.
Lift a function into the context of an Arrow.
In the reference articles "Arrows are Promiscuous...", and in the corresponding Haskell
library Control.Arrow
, this function is called arr
.
Given two F
s (f
and g
) with a common target type, create a new F
with the same target type, but with a source type of either f
's source
type OR g
's source type.
Given two F
s (f
and g
) with a common target type, create a new F
with the same target type, but with a source type of either f
's source
type OR g
's source type.
Example:
scala> import cats.implicits._ scala> val b: Boolean => String = _ + " is a boolean" scala> val i: Int => String = _ + " is an integer" scala> val f: (Either[Boolean, Int]) => String = b ||| i scala> f(Right(3)) res0: String = 3 is an integer scala> f(Left(false)) res0: String = false is a boolean
An F
that, given a source A
on either the right or left side, will
return that same A
object.
An F
that, given a source A
on either the right or left side, will
return that same A
object.
Example:
scala> import cats.implicits._ scala> val f: (Either[Int, Int]) => Int = Choice[Function1].codiagonal[Int] scala> f(Right(3)) res0: Int = 3 scala> f(Left(3)) res1: Int = 3
Contramap on the first type parameter and map on the second type parameter
Contramap on the first type parameter and map on the second type parameter
Example:
scala> import cats.implicits._ scala> import cats.arrow.Profunctor scala> val fab: Double => Double = x => x + 0.3 scala> val f: Int => Double = x => x.toDouble / 2 scala> val g: Double => Double = x => x * 3 scala> val h = Profunctor[Function1].dimap(fab)(f)(g) scala> h(3) res0: Double = 5.4
contramap on the first type parameter
contramap on the first type parameter
Create a new computation F
that merge outputs of f
and g
both having the same input
Create a new computation F
that merge outputs of f
and g
both having the same input
Example:
scala> import cats.implicits._ scala> val addEmpty: Int => Int = _ + 0 scala> val multiplyEmpty: Int => Double= _ * 1d scala> val f: Int => (Int, Double) = addEmpty &&& multiplyEmpty scala> f(1) res0: (Int, Double) = (1,1.0)
Note that the arrow laws do not guarantee the non-interference between the _effects_ of
f
and g
in the context of F. This means that f &&& g
may not be equivalent to g &&& f
.
map on the second type parameter
map on the second type parameter
Create a new F
that takes two inputs, but only modifies the second input
Create a new F
that takes two inputs, but only modifies the second input
Example:
scala> import cats.implicits._ scala> import cats.arrow.Strong scala> val f: Int => Int = _ * 2 scala> val fab = Strong[Function1].second[Int,Int,Int](f) scala> fab((2,3)) res0: (Int, Int) = (2,6)
Create a new computation F
that splits its input between f
and g
and combines the output of each.
Create a new computation F
that splits its input between f
and g
and combines the output of each.
Example:
scala> import cats.implicits._ scala> import cats.arrow.Arrow scala> val toLong: Int => Long = _.toLong scala> val toDouble: Float => Double = _.toDouble scala> val f: ((Int, Float)) => (Long, Double) = Arrow[Function1].split(toLong, toDouble) scala> f((3, 4.0f)) res0: (Long, Double) = (3,4.0)
Note that the arrow laws do not guarantee the non-interference between the _effects_ of
f
and g
in the context of F. This means that f *** g
may not be equivalent to g *** f
.
Must obey the laws defined in cats.laws.ArrowChoiceLaws.