Converting functors (F [A] => G [A]) to Scala (cats or scalars)

Is there a cool class in Cats or Scalaz that converts different types of containers? for instance

  • Option ~> Try
  • Try ~> The Future
  • Try ~> Either
  • Option ~> List

It seems that FunctionK/ ~>/ NaturalTransformationmight be what I'm looking for, but no instances are defined for them, and I don't know why.

+4
source share
1 answer

- , . ( List[A] Option[A]). , ~> " Scalaz:

def main(args: Array[String]): Unit = {
  val optionToList = new (Option ~> List) {
    override def apply[A](fa: Option[A]): List[A] = fa.toList
  }

  println(optionToList(Some(3)))
  println(optionToList(None))
}

:

List(3)
Nil

~> - NaturalTransformation[-F[_], +G[_]]:

/** A universally quantified function, usually written as `F ~> G`,
  * for symmetry with `A => B`.
  */
trait NaturalTransformation[-F[_], +G[_]] {
  self =>
  def apply[A](fa: F[A]): G[A]

  // Abbreviated for the answer
}
+5

Source: https://habr.com/ru/post/1664647/


All Articles