【发布时间】:2019-04-06 21:32:07
【问题描述】:
喂! :)
能否请人指出一个有用的 scala/cats 教程? 在过去的几天里,我一直在努力让一个类成为函子,我正要在我的显示器上打一个洞。 到目前为止,我找到的所有文档都对我没有帮助。
也许我应该试试 Eta...=D
这是我想变成函子的类。 此外,“show”的行为也不符合我的预期。
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")
}
}
【问题讨论】:
-
似乎编译得很好。你的问题到底是什么?
-
安德烈您好,感谢您的回复。我忘了说我想让这个类扩展 cat 的 Functor 并将必要的映射函数放在一个伴生对象中。我希望我能够使用中缀注释调用映射函数,如下所示:
fact: Fact[Int] = someFact map someFunction唉,它不起作用......也许我也是愚蠢的...... =(最好的问候亚历克斯 -
嗯...如果您想要扩展 Functor,那不是问题。如果您真的尝试实现 Functor,但它没有工作,那么这将是一个问题。目前,我没有看到任何提及
Functor的内容,除非在一个导入子句中。请提供一个minimal reproducible example 来说明实际问题是什么。 -
刚刚更新了我的问题...也许现在更清楚了...
-
在这种情况下,Dmytro Mitin 似乎猜对了您的问题。
标签: scala scala-cats