如何在scala / cats中使用函子

时间:2019-04-06 12:15:00

标签: scala scala-cats

喂! :)

能否请有人指出我一个有用的scala / cats教程? 最近几天,我一直在努力使课程成为仿函数,而我将在显示器上打一个孔。 到目前为止,我找到的所有文档对我都没有帮助。

也许我应该试试Eta ... = D

这是我想用作函子的课程。 除了“显示”之外,它的行为也不像我预期的那样。

package org.hudelundpfusch.utilites.decisions.data

import cats.{Functor, Show}
import cats.kernel.Eq
import cats.syntax.functor._
import cats.syntax.show._

import scala.reflect.runtime.universe
import scala.reflect.runtime.universe._

case class Fact[T <: Any] (name: String, value: T) (implicit private val paramTypeTagT: WeakTypeTag[T])
  extends Equals {

  val paramType: universe.Type = paramTypeTagT.tpe

  val paramTypeClass: Option[Class[_ <: T]] = if (value != null) {
    Some(value.getClass)
  } else {
    None
  }

  def map[A, B](fa: Fact[A])(f: A => B): Fact[B] = Fact[B](fa.name, f(fa.value))

  override def canEqual(other: Any): Boolean = other.isInstanceOf[Fact[_]]

  override def equals(other: Any): Boolean = other match {
    case that: Fact[_] =>
      (that canEqual this) &&
        name == that.name
        paramType == that.paramType &&
        paramTypeClass == that.paramTypeClass &&
        value == that.value
    case _ => false
  }

  override def hashCode(): Int = {
    val state = Seq(name, paramType, paramTypeClass, value)
    state.map(_.hashCode()).foldLeft(0)((a, b) => 31 * a + b)
  }

  override def toString = s"Fact(name=${name}, paramType=$paramType, paramTypeClass=$paramTypeClass, value=$value)"

}

case object Fact extends Show[Fact[_]] {

  override def show(t: Fact[_]): String = t.toString

}

预先感谢

祝你愉快

亚历克斯

更新:

package org.hudelundpfusch.utilites.decisions.data

import cats.{Functor, Show}
import cats.kernel.Eq
import cats.syntax.functor._
import cats.syntax.show._

import scala.reflect.runtime.universe
import scala.reflect.runtime.universe._

case class Fact[T <: Any] (name: String, value: T) (implicit private val paramTypeTagT: WeakTypeTag[T])
  extends Functor[Fact]
    with Equals {

  val paramType: universe.Type = paramTypeTagT.tpe

  val paramTypeClass: Option[Class[_ <: T]] = if (value != null) {
    Some(value.getClass)
  } else {
    None
  }

  def map[A, B](fa: Fact[A])(f: A => B): Fact[B] = Fact[B](fa.name, f(fa.value))

  override def canEqual(other: Any): Boolean = other.isInstanceOf[Fact[_]]

  override def equals(other: Any): Boolean = other match {
    case that: Fact[_] =>
      (that canEqual this) &&
        name == that.name
        paramType == that.paramType &&
        paramTypeClass == that.paramTypeClass &&
        value == that.value
    case _ => false
  }

  override def hashCode(): Int = {
    val state = Seq(name, paramType, paramTypeClass, value)
    state.map(_.hashCode()).foldLeft(0)((a, b) => 31 * a + b)
  }

  override def toString = s"Fact(name=${name}, paramType=$paramType, paramTypeClass=$paramTypeClass, value=$value)"

}

好的,我现在尝试了这个

object Fact {
  implicit val factFunctor: Functor[Fact] = new Functor[Fact] {
    override def map[A, B](fa: Fact[A])(f: A => B): Fact[B] = Fact[B](fa.name, f(fa.value))
  }

  implicit def factShow[T]: Show[Fact[T]] = new Show[Fact[T]] {
    override def show(t: Fact[T]): String = this.toString
  }
}

不幸的是,对映射函数的调用看起来有点麻烦:

package org.hudelundpfusch.utilites.decisions.data

object Fuddel {
  def main(args: Array[String]): Unit = {
    val fact1: Fact[Int] = Fact("Fact-1", 23)
    val fact2 = Fact.factFunctor.map(fact1){x: Int => x * 2}

    println(s"$fact2")
  }
}

1 个答案:

答案 0 :(得分:5)

对于Cats,您不扩展Show,Functor特性(OOP方法),而是创建Show,Functor(FP方法)的隐式实例。

http://eed3si9n.com/herding-cats/Functor.html

https://typelevel.org/cats/typeclasses/functor.html

implicit def factShow[T]: Show[Fact[T]] = new Show[Fact[T]] {
  override def show(t: Fact[T]): String = ???
}

//  implicit object factFunctor extends Functor[Fact] {
//    override def map[A, B](fa: Fact[A])(f: A => B): Fact[B] = ???
//  }

implicit val factFunctor: Functor[Fact] = new Functor[Fact] {
  override def map[A, B](fa: Fact[A])(f: A => B): Fact[B] = ???
}

Show[Fact[Int]].show(Fact("a", 1))
Functor[Fact].map(Fact("a", 1))(_ + 1)

import cats.syntax.show._
Fact("a", 1).show

import cats.syntax.functor._
Fact("a", 1).map(_ + 1)

以防万一,您的Fact[_]中的case object Fact extends Show[Fact[_]]是存在类型,而我的Fact中的implicit object factFunctor extends Functor[Fact]是种类更高的类型。