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)
extract
is the dual of pure
on Monad (via Applicative
)
and extracts the value from its context
extract
is the dual of pure
on Monad (via Applicative
)
and extracts the value from its context
Example:
scala> import cats.Id scala> import cats.Comonad scala> val id: Id[Int] = 3 scala> Comonad[Id].extract(id) res0: cats.Id[Int] = 3
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))
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
Lift a function f to operate on Functors
Lift a function f to operate on Functors
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
.
Comonad
Comonad is the dual of Monad. Whereas Monads allow for the composition of effectful functions, Comonads allow for composition of functions that extract the value from their context.
Must obey the laws defined in cats.laws.ComonadLaws.