值序列不是 cats.Applicative[F] 的成员

value sequence is not a member of cats.Applicative[F]

我正在将一些基于 scalaz 的代码迁移到 typelevel cats。

project/plugins.sbt

addCompilerPlugin("org.typelevel" % "kind-projector" % "0.11.3" cross CrossVersion.full)

build.sbt

scalacOptions ++= Seq("-feature", "-deprecation", "-Xlint", "-Xfatal-warnings", "-Ypartial-unification")
scalaVersion := "2.12.10"

基于旧 scalaz 的代码中的依赖项是

libraryDependencies += "com.github.julien-truffaut" %%% "monocle-macro" % "1.4.8"

这是旧的基于 scalaz 代码的实现,我想将其迁移到 cats。

import scalaz.Applicative
import scalaz.std.list._
import scalaz.syntax.applicative._
import scala.language.higherKinds
import monocle.PTraversal

def letterTraversal(predicate: Char ⇒ Boolean): PTraversal[String, String, Char, Char] = new PTraversal[String, String, Char, Char] {
         override def modifyF[F[_]: Applicative](f: Char ⇒ F[Char])(s: String): F[String] = {
           Applicative[F].sequence(s.toList map {
             case x if predicate(x) ⇒ f(x)
             case x ⇒ Applicative[F].point(x)
           }).map(_.mkString)
         }
     }

基于新代码猫的代码的依赖性是

libraryDependencies += "com.github.julien-truffaut" %%% "monocle-macro" % "2.0.0"

这是我的新密码

import scala.language.higherKinds
import monocle.PTraversal
import cats.Applicative
import cats.implicits._

    def letterTraversal(predicate: Char ⇒ Boolean): PTraversal[String, String, Char, Char] = new PTraversal[String, String, Char, Char] {
            override def modifyF[F[_]: Applicative](f: Char ⇒ F[Char])(s: String): F[String] = {
              Applicative[F].sequence(s.toList map {
                case x if predicate(x) ⇒ f(x)
                case x ⇒ Applicative[F].point(x)
              }).map(_.mkString)
            }
        }
When I do `sbt clean compile` I get the below error -

[error] /Users/rajkumar.natarajan/Documents/Coding/OS/reftree/demo/jvm/src/main/scala/reftree/demo/Data.scala:07:22: value sequence is not a member of cats.Applicative[F]

[error] Applicative[F].sequence(s.toList map {

[error] ^ [error] one error found

[error] (demoJVM / Compile / compileIncremental) Compilation failed

[error] Total time: 32 s, completed May 27, 2021, 4:36:58 PM

为什么即使我在作用域中添加了 import cats.implicits._,也没有获得 Applicative[F] 的序列函数?

惯用的写法是这样的:

import cats.syntax.all._
…
          s.toList.traverse {
            case x if predicate(x) ⇒ f(x)
            case x ⇒ x.pure[F]
          }.map(_.mkString)