coflatMap
is the dual of flatMap
on FlatMap
.
coflatMap
is the dual of flatMap
on FlatMap
. It applies
a value in a context to a function that takes a value
in a context and returns a normal value.
Example:
scala> import cats.implicits._ scala> import cats.CoflatMap scala> val fa = Some(3) scala> def f(a: Option[Int]): Int = a match { | case Some(x) => 2 * x | case None => 0 } scala> CoflatMap[Option].coflatMap(fa)(f) res0: Option[Int] = Some(6)
Replaces the A
value in F[A]
with the supplied value.
Replaces the A
value in F[A]
with the supplied value.
coflatten
is the dual of flatten
on FlatMap
.
coflatten
is the dual of flatten
on FlatMap
. Whereas flatten removes
a layer of F
, coflatten adds a layer of F
Example:
scala> import cats.implicits._ scala> import cats.CoflatMap scala> val fa = Some(3) fa: Option[Int] = Some(3) scala> CoflatMap[Option].coflatten(fa) res0: Option[Option[Int]] = Some(Some(3))
Alias for map, since map can't be injected as syntax if
the implementing type already had a built-in .map
method.
Alias for map, since map can't be injected as syntax if
the implementing type already had a built-in .map
method.
Example:
scala> import cats.implicits._ scala> val m: Map[Int, String] = Map(1 -> "hi", 2 -> "there", 3 -> "you") scala> m.fmap(_ ++ "!") res0: Map[Int,String] = Map(1 -> hi!, 2 -> there!, 3 -> you!)
Tuple the values in fa with the result of applying a function with the value
Tuple the values in fa with the result of applying a function with the value
Transform an F[A]
into an F[B]
by providing a transformation from A
to B
and one from B
to A
.
Transform an F[A]
into an F[B]
by providing a transformation from A
to B
and one from B
to A
.
Example:
scala> import cats.implicits._ scala> import scala.concurrent.duration._ scala> val durSemigroup: Semigroup[FiniteDuration] = | Invariant[Semigroup].imap(Semigroup[Long])(Duration.fromNanos)(_.toNanos) scala> durSemigroup.combine(2.seconds, 3.seconds) res1: FiniteDuration = 5 seconds
Lift a function f to operate on Functors
Lift a function f to operate on Functors
Tuples the A
value in F[A]
with the supplied B
value, with the B
value on the left.
Tuples the A
value in F[A]
with the supplied B
value, with the B
value on the left.
Tuples the A
value in F[A]
with the supplied B
value, with the B
value on the right.
Tuples the A
value in F[A]
with the supplied B
value, with the B
value on the right.
Empty the fa of the values, preserving the structure
Empty the fa of the values, preserving the structure
Lifts natural subtyping covariance of covariant Functors.
Lifts natural subtyping covariance of covariant Functors.
NOTE: In certain (perhaps contrived) situations that rely on universal
equality this can result in a ClassCastException
, because it is
implemented as a type cast. It could be implemented as map(identity)
, but
according to the functor laws, that should be equal to fa
, and a type
cast is often much more performant.
See this example
of widen
creating a ClassCastException
.
CoflatMap
is the dual ofFlatMap
.Must obey the laws in cats.laws.CoflatMapLaws