简体   繁体   中英

Functor implementation for types with more than one type

let's say I have:

trait Get[F[_], A, B]{
  def get(a:A): F[B]
}

I want to be able to map over the result type B, ie I want to be able to do:

val getFoo: Get[IO, String, Foo] = ???
val foo2Bar: Foo => Bar = ???
val getBar: Get[IO, String, Bar] = getFoo.map(foo2Bar)

I understand that I should implement a Functor instance for Get but I am struggling as I don't know what type signature to use.

I tried the following:

  • implicit val functor:Functor[Get] =???

  • implicit val functor: Functor[Lambda[(F[_], K, A) => Get[F, K, A]]] =???

but they don't seem to be of the right type as I can't seem to use the functor syntax extension as illustrated at the top. What is the right way of expressing the type here? What would be the equivalent type be if I use the kind-projector plugin?

Try

import cats.syntax.functor._

implicit def functor[F[_]: Functor, A]: Functor[Get[F, A, ?]] = new Functor[Get[F, A, ?]] {
  override def map[B, B1](fa: Get[F, A, B])(f: B => B1): Get[F, A, B1] = a => fa.get(a).map(f)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM