scala - 在 Scala 中正确实现 2 类型参数 Functor

标签 scala intellij-idea functional-programming

这个问题我在SO上看到好几次了,但是无论我怎么努力,我都无法让下面的代码编译通过。目标是为更简单的 Reader 实现一个 Functor 实现(代码是 here ):

  trait Functor[F[_]] {
    def fmap[A, B](fa: F[A])(f: A => B): F[B]
  }

  implicit class FunctorOps[F[_]: Functor, A](self: F[A]) {
    def fmap[B](f: A => B): F[B] = implicitly[Functor[F]].fmap(self)(f)
  }

  case class Reader[A, B](run: A => B)
  type ReaderF[X] = ({ type L[A] = Reader[X, A] })

  implicit def readerFunctors[E]: Functor[ReaderF[E]#L] = 
    new Functor[ReaderF[E]#L] {
       override def fmap[A, B](fa: Reader[E, A])(f: A => B): Reader[E, B] = 
          Reader(e => f(fa.run(e)))
    }

  val foo = Reader[String, Int](_ => 42)

  foo.fmap(_ + 1) // does not compile

我试图通过以下方式绕过隐式机制:

FunctorOps(foo).fmap(_ + 1)

但这会输出以下编译错误:

Error:(82, 23) type mismatch;
 found   : com.fp.Scratchpad.Reader[String,Int]
 required: ?F[?A]
Note that implicit conversions are not applicable because they are ambiguous:
 both method ArrowAssoc in object Predef of type [A](self: A)ArrowAssoc[A]
 and method Ensuring in object Predef of type [A](self: A)Ensuring[A]
 are possible conversion functions from com.fp.Scratchpad.Reader[String,Int] to ?F[?A]
  FunctorOps(foo).fmap(_ + 1)

预先感谢您的帮助。

更新

为了确保我的 FunctorOps 是正确的,我为 Id 创建了一个仿函数实例:

case class Id[A](value: A)
implicit val idF: Functor[Id] = new Functor[Id] {
  override def fmap[A, B](fa: Id[A])(f: A => B): Id[B] = Id(f(fa.value))
}

val id = Id(42)
id.fmap(_ + 1) // compiles

所以问题不是来自FunctorOps 隐式类。我怀疑 Scala 很难处理 lambda 类型...

更新 2

我试图简化问题但没有成功:

  trait Functor[F[_]] {
    def map[A, B](x: F[A])(f: A => B): F[B]
  }

  implicit class Ops[F[_], A](fa: F[A])(implicit F: Functor[F]) {
    def map[B](f: A => B): F[B] = F.map(fa)(f)
  }

  type FF[A] = ({ type F[B] = A => B })

  implicit def ff[E]: Functor[FF[E]#F] = new Functor[FF[E]#F] {
    override def map[A, B](x: E => A)(f: A => B): E => B = e => f(x(e))
  }

  val f: String => Int = _ => 42

  val value: Functor[FF[String]#F] = ff[String]
  val ops = new Ops[FF[String]#F, Int](f)(value)

  // These compile
  ops.map(_ + 1)("")
  value.map(f)(_ + 1)("")

  // This not
  f.map(_ + 1)

最佳答案

更新:
我认为,要使其正常工作,您需要在 build.sbt 中为编译器启用一些额外选项:

scalacOptions ++= Seq(
      "-Ypartial-unification",
      "-language:postfixOps",
      "-language:higherKinds",
      "-deprecation",
      "-encoding", "UTF-8",
      "-feature",      
      "-unchecked"
    )

有关部分统一 标志及其解决问题的更多信息 can be found here .

原始答案: 您是通过 Worksheet 还是在 IDEA 中的 Scratch 运行代码?我注意到,有时,尤其是在这些存在类型推断、隐式解析和更高级类型“魔法”的函数式编程任务中,IDEA 的 REPL 不能胜任该任务(但我不确定为什么)。

这就是说,我尝试在 IDEA 上运行以下命令:

object TestApp extends App{
  trait Functor[F[_]] {
    def fmap[A, B](fa: F[A])(f: A => B): F[B]
  }

  implicit class FunctorOps[F[_]: Functor, A](self: F[A]) {
    def fmap[B](f: A => B): F[B] = implicitly[Functor[F]].fmap(self)(f)
  }

  case class Reader[A, B](run: A => B)
  type ReaderF[X] = ({ type L[A] = Reader[X, A] })

  implicit def readerFunctors[E]: Functor[ReaderF[E]#L] =
    new Functor[ReaderF[E]#L] {
      override def fmap[A, B](fa: Reader[E, A])(f: A => B): Reader[E, B] =
        Reader(e => f(fa.run(e)))
    }

  val foo: Reader[String, Int] = Reader[String, Int](s => s.length)

  val i = foo.fmap(_ + 1)

  println(i.run("Test"))
  println(i.run("Hello World"))
}

它工作正常,打印 512。此外,正如其他人所提到的,您的代码适用于 Scastie,这是 IDEA 运行的另一个症状。

最后一点:您可能已经知道这一点,但是您可以使用 kind-projector compiler plugin 避免所有类型 lambda 的丑陋行为。 .

长话短说,删除 ReaderF[X] 类型别名,并使您的仿函数实例如下所示:

implicit def readerFunctors[X]: Functor[Reader[X,?]] =
    new Functor[Reader[X,?]] {
      override def fmap[B, C](fa: Reader[X,B])(f: B => C): Reader[X,C] =
        Reader(e => f(fa.run(e)))
    }

恕我直言,哪个更具可读性。

关于scala - 在 Scala 中正确实现 2 类型参数 Functor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55380160/

相关文章:

scala - 为什么 Scala 的类型系统不是 Clojure 中的库

scala - 在运行时检查 Scala 类型和类型删除

function - 以下警告是什么意思: 'side-effecting nullary methods are discouraged' ?

angularjs - Bower 安装卡在 json 输出 : "message": "Answer",

javascript - 用JavaScript创建功能对象的优点/缺点是什么?

design-patterns - 函数式编程的原则、最佳实践和设计模式

使用从不同范围导入时的 Scala 类型不匹配问题

scala - Slick 3.1.1 模糊隐式值涉及过滤器和隐式 CanBeQueryCondition

java - 下载并在Windows 32位操作系统安装的IntelliJ

functional-programming - 如何解释方案表达式 '(a ' b)