Question

Given a val that consists of the following (what I believe is a) type constructor with a curried function argument, F[A => B => C]...

val x: F[A => B => C] = foo() // foo does not matter

Is it possible for me to get F[C] from x?

val y: F[C] = x...

EDIT For context, I'm trying to implement the map3 function from Functional Programming in Scala.

Was it helpful?

Solution

The only way to get F[C] from F[A => B => C] is if you can apply an A and a B to it. That is, you'll need to evaluate the contained function. Use the apply twice, once to get an B => C and then once again to get C.

def eval(myApp: F[A => B => C])(value: F[A]): F[B => C]
def evalAgain(myApp: F[B => C])(value: F[B]): F[C]

but if you just want to be able to get F[C] directly from the function itself without evaluation, you're SOL.

Edit:

I believe it would look like this.

def eval(myApp: F[A => B => C])(value: F[A], next: F[B])(implicit ap: Applicative[F[_]]) = ap(ap(myApp, value), next)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top