I have this typeclass
import simulacrum._
@typeclass trait Functor[F[_]] {
def map[A, B](fa: F[A])(f: A => B) : F[B]
def lift[A, B](fa: F[A])(f: A => B) : F[A] => F[B] = fa => map(fa)(f)
def as[A, B](fa: F[A], b: => B) : F[B] = map(fa)(_ => b)
def void[A](fa: F[A]) : F[Unit] = as(fa, ())
}
and this is the implementation
object Functor {
implicit val listFunctor: Functor[List] = new Functor[List] {
def map[A, B](fa: List[A])(f: A => B) = fa.map(f)
}
implicit def functionFunctor[X]: Functor[X => ?] = new Functor[X => ?] {
def map[A, B](fa : X => A)(f : A => B) = fa andThen f
}
}
I can easily discover the List implicit implementation as
object Chapter1 extends App {
import Functor.ops._
List(1, 2, 3).as("foo").foreach(println)
}
The above works perfectly fine. I can also do
object Chapter1 extends App {
import Functor._
val func : Int => String = implicitly[Functor[Int => ?]].map(_ + 2)(_.toString)
println(func(5))
}
But when I try
object Chapter1 extends App {
import Functor.ops._
val x : Int => Int = _ + 2
val y : Int => String = x.map(_.toString)
}
It doesn't find my implicit implementation and says that the value map
is not a member of Int => Int