如何将函子与 scala/cat 一起使用



Aloha! :)

有人可以指出我一个有用的 scala/cats 教程吗?在过去的几天里,我正在努力使类成为函子,我即将在我的显示器上打一个洞。到目前为止,我找到的所有文档对我没有帮助。

也许我应该试一试埃塔... =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")
  }
}

对于 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)

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

相关内容

  • 没有找到相关文章

最新更新