gpt4 book ai didi

scala - 找不到参数半群的隐式值

转载 作者:行者123 更新时间:2023-12-01 12:15:17 24 4
gpt4 key购买 nike

我只是尝试编译以下代码:

import cats.Monoid
import cats.instances.boolean._ // for Monoid
import cats.instances.int._ // for Monoid
import cats.instances.list._ // for Monoid
import cats.instances.string._ // for Monoid
import cats.syntax.apply._ // for imapN
import cats.syntax.semigroup._

case class Cat2(name: String, yearOfBirth: Int, favoriteFoods: List[String])

object FancyFunctor {

val tupleToCat: (String, Int, List[String]) => Cat2 =
Cat2.apply _

val catToTuple: Cat2 => (String, Int, List[String]) =
cat => (cat.name, cat.yearOfBirth, cat.favoriteFoods)

implicit val catMonoid: Monoid[Cat2] = (
Monoid[String],
Monoid[Int],
Monoid[List[String]]
).imapN(tupleToCat)(catToTuple)

def main(args: Array[String]): Unit = {

val garfield = Cat2("Garfield", 1978, List("Lasagne"))
val heathcliff = Cat2("Heathcliff", 1988, List("Junk Food"))

val a = garfield |+| heathcliff

}

}

我收到了错误消息:
[info] Compiling 1 Scala source to /home/developer/Desktop/scala/catssemigroupal/target/scala-2.12/classes ...
[error] /home/developer/Desktop/scala/catssemigroupal/src/main/scala/io/khinkali/FancyFunctor.scala:25:22: could not find implicit value for parameter semigroupal: cats.Semigroupal[cats.kernel.Monoid]
[error] ).imapN(tupleToCat)(catToTuple)
[error] ^
[error] one error found
[error] (Compile / compileIncremental) Compilation failed
[error] Total time: 1 s, completed Feb 24, 2018, 10:18:01 PM

我究竟做错了什么?

最佳答案

这与猫 1.0.1 编译:

import cats.Monoid
import cats.instances.boolean._ // for Monoid
import cats.instances.int._ // for Monoid
import cats.instances.list._ // for Monoid
import cats.instances.string._ // for Monoid
import cats.syntax.apply._ // for imapN
import cats.syntax.semigroup._ // for |+|
import cats.instances.invariant._ // for catsSemigroupalForMonoid: InvariantSemigroupal[Monoid]

object FancyFunctor {

/*
val tupleToCat: (String, Int, List[String]) => Cat2 =
Cat2.apply _

val catToTuple: Cat2 => (String, Int, List[String]) =
cat => (cat.name, cat.yearOfBirth, cat.favoriteFoods)

*/

case class Cat2(name: String, yearOfBirth: Int, favoriteFoods: List[String])
implicit val catMonoid: Monoid[Cat2] = (
Monoid[String],
Monoid[Int],
Monoid[List[String]]
).imapN(Cat2.apply)(c => Cat2.unapply(c).get)

def main(args: Array[String]): Unit = {

val garfield = Cat2("Garfield", 1978, List("Lasagne"))
val heathcliff = Cat2("Heathcliff", 1988, List("Junk Food"))

val a = garfield |+| heathcliff

}

}

如何仅使用浏览器(无 IDE)快速查找:
  • 搜索 Semigroupal (缺少的类型类)在 Doc
  • 搜索 Monoid在结果页面上使用浏览器的文本搜索
  • 点击返回Semigroupal[Monoid]的隐式方法,
  • 按照定义类的链接:InvariantMonoidalInstances
  • 寻找线性子类,选择比“所有”更精确的东西

  • 提示:矫枉过正的方法 cats.implicits._适用于您的代码。

    作为奖励:您不必重新定义 Cat2.applyCat2.unapply ,它们会自动为每个 case 提供-类(class)。

    关于scala - 找不到参数半群的隐式值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48967836/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com