scala - 为什么我在这个简单的 Scala 代码上出现错误?

标签 scala

这是我的代码:

object Triple {
  object Num extends Enumeration {
    type Num = Value
    val one, two, three, four, five, six, seven, eight, nine = Value
  }
  val triples = for {
    first <- Num.values
    second <- Num.values
    third <- Num.values
    if (first != second)
    if (first != third)
    if (second != third)
  } yield Set(first, second, third)
}

我在第三行中遇到错误。它说:
Multiple markers at this line
    - not enough arguments for method map: (implicit bf: 
         scala.collection.generic.CanBuildFrom[sudoku.Triple.Num.ValueSet,scala.collection.immutable.Se    t[sudoku.Triple.Num.Value],That])That. Unspecified 
 value parameter bf.
- diverging implicit expansion for type 
 scala.collection.generic.CanBuildFrom[sudoku.Triple.Num.ValueSet,scala.collection.immutable.Set[sudoku.Triple.Num.Value],That] starting with 
 method newCanBuildFrom in object SortedSet
- not enough arguments for method map: (implicit bf: 
 scala.collection.generic.CanBuildFrom[sudoku.Triple.Num.ValueSet,scala.collection.immutable.Set[sudoku.Triple.Num.Value],That])That. Unspecified 
 value parameter bf.
- diverging implicit expansion for type 
 scala.collection.generic.CanBuildFrom[sudoku.Triple.Num.ValueSet,scala.collection.immutable.Set[sudoku.Triple.Num.Value],That] starting with 
 method newCanBuildFrom in object SortedSet

更新:Aleksey Izmailov 给出了很好的答案。该函数的一个稍微好一点的实现是 val 三元组=Num.values.subset(3).toSet

最佳答案

看起来你正在尝试做的是这样的:

scala> object Num extends Enumeration {
     |     type Num = Value
     |     val one, two, three, four, five, six, seven, eight, nine = Value
     |   }
defined module Num

scala>   val triples = Num.values.toList.combinations(3)
triples: Iterator[List[Num.Value]] = non-empty iterator

scala> triples.toList
res0: List[List[Num.Value]] = List(List(one, two, three), List(one, two, four), List(one, two, five), List(one, two, six), List(one, two, seven), List(one, two, eight), List(one, two, nine), List(one, three, four), List(one, three, five), List(one, three, six), List(one, three, seven), List(one, three, eight), List(one, three, nine), List(one, four, five), List(one, four, six), List(one, four, seven), List(one, four, eight), List(one, four, nine), List(one, five, six), List(one, five, seven), List(one, five, eight), List(one, five, nine), List(one, six, seven), List(one, six, eight), List(one, six, nine), List(one, seven, eight), List(one, seven, nine), List(one, eight, nine), List(two, three, four), List(two, three, five), List(two, three, six), List(two, three, seven), List(two, three...
scala> 

如果你做这样的事情,那么它的工作原理:
scala> object Triple {
     |   object Num extends Enumeration {
     |     type Num = Value
     |     val one, two, three, four, five, six, seven, eight, nine = Value
     |   }
     |   val triples = for {
     |     first <- Num.values
     |     second <- Num.values
     |     third <- Num.values
     |     if (first != second)
     |     if (first != third)
     |     if (second != third)
     |   } yield (first, second, third)
     | }
defined module Triple

scala> Triple.triples
res1: scala.collection.immutable.SortedSet[(Triple.Num.Value, Triple.Num.Value, Triple.Num.Value)] = TreeSet((one,two,three), (one,two,four), (one,two,five), (one,two,six), (one,two,seven), (one,two,eight), (one,two,nine), (one,three,two), (one,three,four), (one,three,five), (one,three,six), (one,three,seven), (one,three,eight), (one,three,nine), (one,four,two), (one,four,three), (one,four,five), (one,four,six), (one,four,seven), (one,four,eight), (one,four,nine), (one,five,two), (one,five,three), (one,five,four), (one,five,six), (one,five,seven), (one,five,eight), (one,five,nine), (one,six,two), (one,six,three), (one,six,four), (one,six,five), (one,six,seven), (one,six,eight), (one,six,nine), (one,seven,two), (one,seven,three), (one,seven,four), (one,seven,five), (one,seven,six), (one,...
scala> 

请注意,生成的集合的结果是 SortedSet .该结果由 Num.values 定义- 您可以转换 for领悟到map , flatMapfilter检查类型是如何产生的。

问题是你可以把可以排序的东西放进SortedSet像这样例如:
scala> scala.collection.immutable.SortedSet(1, 2)
res6: scala.collection.immutable.SortedSet[Int] = TreeSet(1, 2)

但是,您不能将没有定义排序的内容放入 SortedSet 中。因为 SortedSet保证内容已排序,并且应该知道排序是如何完成的(通过隐式排序):
scala> scala.collection.immutable.SortedSet(Set(1, 2), Set(3, 4))
<console>:8: error: No implicit Ordering defined for scala.collection.immutable.Set[Int].
              scala.collection.immutable.SortedSet(Set(1, 2), Set(3, 4))

Scala 寻找隐式 Ordering要在范围内定义并且没有为 Set 找到一个您正在使用的,或用于 Set[Int]在我的例子中。

现在让我们“修复”它:
scala> implicit val dummyOrdering: Ordering[Set[Int]] = Ordering.by(_.size)
dummyOrdering: Ordering[Set[Int]] = scala.math.Ordering$$anon$9@6f00d9a7

scala> scala.collection.immutable.SortedSet(Set(1, 2), Set(3, 4))
res8: scala.collection.immutable.SortedSet[scala.collection.immutable.Set[Int]] = TreeSet(Set(3, 4))

显然,这是一个糟糕的解决方案,真正的问题是在您的情况下类型选择不当。首先,您不需要使用 Set对于每个组合,因为您已经比较事物以避免重复。只需使用 Tuple3 .其次,如果你坚持使用Set ,然后转换 Num.valuesListfor理解:
scala> object Triple {
     |   object Num extends Enumeration {
     |     type Num = Value
     |     val one, two, three, four, five, six, seven, eight, nine = Value
     |   }
     |   val triples = for {
     |     first <- Num.values.toList
     |     second <- Num.values.toList
     |     third <- Num.values.toList
     |     if (first != second)
     |     if (first != third)
     |     if (second != third)
     |   } yield Set(first, second, third)
     | }
defined module Triple

希望这能说清楚。基本上问题在于选择了错误的类型。

关于scala - 为什么我在这个简单的 Scala 代码上出现错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24297638/

相关文章:

scala - 我想使用 scala 驱动程序在 mongodb 上执行查找操作

Scala:如何使用可变参数列表定义匿名函数?

scala - 在 Scala 中搜索具有特定名称的文件夹

scala - 当我发出 g8 命令时,Github 对等点未经过身份验证

scala - 对单元测试进行分组并仅运行一组

eclipse - 如何设置 SPARK_LOCAL_IP 地址

android - scala-android 插件

斯卡拉 Play : Routes optional parameter with regex?

algorithm - 乘以集合并随机与其他集合合并 - Apache Spark

scala - 在 Scala 中,如何重新分配元组值?