1

我有以下仿函数定义:

import cats.Functor
import cats.syntax.functor._

object Theory {

  implicit val treeFunctor: Functor[Tree] =
    new Functor[Tree] {
      def map[A, B](fa: Tree[A])(f: A => B): Tree[B] =
        fa match {
          case Branch(left, right) =>
            Branch(map(left)(f), map(right)(f))
          case Leaf(value) =>
            Leaf(f(value))
        }
    }

  def main(args: Array[String]): Unit = {
    Branch(Leaf(10), Leaf(20)).map(_ * 2)
  }


}

为了:

sealed trait Tree[+A]

final case class Branch[A](left: Tree[A], right: Tree[A]) extends Tree[A]

final case class Leaf[A](value: A) extends Tree[A]

为什么编译器抱怨:

// <console>:42: error: value map is not a member of wrapper.Branch[
Int]
//
Branch(Leaf(10), Leaf(20)).map(_ * 2)
//

所以我必须创建一个智能构造函数:

object Tree {
  def branch[A](left: Tree[A], right: Tree[A]): Tree[A] =
    Branch(left, right)
  def leaf[A](value: A): Tree[A] =
    Leaf(value)
}

在这种情况下,什么是智能构造函数?

4

2 回答 2

3

in的声明在Functor[F[_]]in 中cats是不变的F。因此,aFunctor[Tree]既不是 的泛化,也不是 的特化Functor[Branch]。这些类型是不相关的。

您的代码的问题如下。表达方式

Branch(Leaf(10), Leaf(20))

是类型Branch[Int]。当您尝试.map[X]直接应用它时,您表示您希望得到 aBranch[X]作为结果。但是没有Functor[Branch]范围(并不是说你不能写,但就目前而言,没有)。

为了使用Functor[Tree],您必须向编译器明确说明您希望将此实例视为Tree[Int]. 铸造会起作用。或者使用隐藏Branch和暴露的自定义工厂方法Tree也可以:这就是“智能”构造函数正在做的事情。

于 2018-01-31T15:13:39.123 回答
0

您可以使用kittens和实现BranchandLeaf的实例,然后Tree可以派生 for 的实例。

libraryDependencies += "org.typelevel" %% "kittens" % "1.0.0-RC2"

  import cats.Functor
  import cats.syntax.functor._

  sealed trait Tree[+A]
  final case class Branch[A](left: Tree[A], right: Tree[A]) extends Tree[A]
  final case class Leaf[A](value: A) extends Tree[A]

  implicit val treeFunctor: Functor[Tree] = cats.derive.functor[Tree]

  implicit val branchFunctor: Functor[Branch] =
    new Functor[Branch] {
      def map[A, B](fa: Branch[A])(f: A => B): Branch[B] =
        fa match {
          case Branch(left, right) =>
            Branch(left.map(f), right.map(f))
        }
    }

    // or without extension method
//  implicit def branchFunctor(implicit treeFunctor: Functor[Tree]): Functor[Branch] =
//    new Functor[Branch] {
//      def map[A, B](fa: Branch[A])(f: A => B): Branch[B] =
//        fa match {
//          case Branch(left, right) =>
//            Branch(treeFunctor.map(left)(f), treeFunctor.map(right)(f))
//        }
//    }

  implicit val leafFunctor: Functor[Leaf] =
    new Functor[Leaf] {
      def map[A, B](fa: Leaf[A])(f: A => B): Leaf[B] =
        fa match {
          case Leaf(value) =>
            Leaf(f(value))
        }
    }

  def main(args: Array[String]): Unit = {
    (Branch(Leaf(10), Leaf(20)): Tree[Int]).map(_ * 2)
    Branch(Leaf(10), Leaf(20)).map(_ * 2)
    Leaf(10).map(_ * 2)
  }

实际上,您可以导出所有三个实例:

  implicit val treeFunctor: Functor[Tree] = cats.derive.functor[Tree]
  implicit val leafFunctor: Functor[Leaf] = cats.derive.functor[Leaf]
  implicit val branchFunctor: Functor[Branch] = cats.derive.functor[Branch]

  def main(args: Array[String]): Unit = {
    (Branch(Leaf(10), Leaf(20)): Tree[Int]).map(_ * 2)
    Branch(Leaf(10), Leaf(20)).map(_ * 2)
    Leaf(10).map(_ * 2)
  }
于 2018-02-03T15:24:08.570 回答