I have this a value of type List[EitherT[IO, String, Int]]
and I want to do sequence on it in order to map it into EitherT[IO,String, List[Int]]
I read and I found sequence method but It gives me an error saying that it needs an implicit Applicative of [G] how can this be resolved
CodePudding user response:
It's hard to guess the reasons of your compile error ("needs an implicit Applicative[G]
") without MCVE. Please provide the one.
Before Cats 2.2.0 it was necessary to import instances
https://meta.plasm.us/posts/2019/09/30/implicit-scope-and-cats/
https://github.com/typelevel/cats/releases/tag/v2.2.0
http://eed3si9n.com/herding-cats/import-guide.html
Why is import cats.implicits._ no longer necessary for importing type class instances?
import cats.instances.either._ // or cats.instances.all._ import cats.instances.list._ // or cats.instances.all._ import cats.syntax.traverse._ // or cats.syntax.all._ // or cats.implicits._
Starting from Cats 2.2.0 you still need to import syntaxes but no longer need to import instances.
Before Scala 2.13.0 you had to add to
build.sbt
scalacOptions = "-Ypartial-unification"
https://github.com/typelevel/cats/issues/2948
http://eed3si9n.com/herding-cats/partial-unification.html
value YpartialUnification is not a member of scala.tools.nsc.Settings
https://www.reddit.com/r/scala/comments/7ak9c5/ypartialunification/
Or maybe you need to specify generics (which were not inferred)
val l: List[Either[String, Int]] = ??? val l1: List[EitherT[IO, String, Int]] = ??? l.sequence[({ type G[X] = Either[String, X] })#G, Int] // using type lambda l1.sequence[({ type G[X] = EitherT[IO, String, X] })#G, Int] // using type lambda l.sequence[Either[String, *], Int] // using kind-projector l1.sequence[EitherT[IO, String, *], Int] // using kind-projector
Or you can unwrap-wrap
EitherT
EitherT( l1.traverse(_.value) .map( _.sequence[Either[String, *], Int] ) )
If you write a generic method maybe it's enough to add a context bound
def foo[G[_]: Applicative] = { // ^^^^^^^^^^^ here // ... using .sequence ... }