pawel.panasewicz pawel.panasewicz - 2 months ago 21
Scala Question

transform 'A => F[G[B]]' into 'F[G[A=>B]' scala

Given a function with signature:

A => F[G[B]]
. There are monads instances for
F
and
G
types.

Is it possible to transform it into something with the signature:
F[G[A=>B]
? Is there any general name for such transformation?

In other words what would be the implementation of
prettify2
?

def pretiffy(x: String): Future[Option[String]] = Future{if(x == "") None else Some(s">>>$x<<<")}
val pretiffy2: Future[Option[String => String]] = ???


Update: I'd appreciate answers using cats or scalaz.

Answer

Suppose we have a String=>List[Option[Integer]]. We need to produce a List[Option[String=>Integer]]. How should we approach this? For example, how long the resulting list should be? How many Nones should it contain?

Obviously these questions have no answers, which means the requested transformation cannot exist for an arbitrary monad (or indeed most monads, as one can ask similar questions about most monads).