代码之家  ›  专栏  ›  技术社区  ›  aioobe

Scala找到List和SeqProjection,需要时Seq和Set

  •  0
  • aioobe  · 技术社区  · 15 年前

    出现以下错误:

    TestCaseGenerator.scala:47: error: type mismatch;
     found   : List[(State, Seq.Projection[State])]
     required: Seq[(State, Set[State])]
        new LTS(Map(rndTrans: _*), Map(rndLabeling: _*))
                    ^
    one error found
    

    不知道该怎么办。

    这个 rndTrans 初始化如下:

    val rndTrans = for (s <- (0 to nStates).toList)
                       yield (new State(s) -> (for (s2 <- 0 to nStates
                           if prob(trans_probability))
                               yield new State(s2)))
    

    更新:我碰巧使用的是2.7版。

    2 回复  |  直到 15 年前
        1
  •  4
  •   Debilski    15 年前

    toSet 方法(或 toMap )不可用(因为运行的是旧版本的scala,或者因为转换尚未实现),通常可以应用以下方案之一。

    val collection: Seq[SomeType] = ...
    
    Set( collection: _* )
    

    Set() ++ collection
    

    第一个版本使用 :_* 将集合转换为序列参数,然后调用新集合类型的构造函数方法。第二个方法创建了一个新类型的空集合,然后添加( ++ )旧的收藏。

        2
  •  2
  •   mkneissl    15 年前

    一般来说 Seq 不是 Set . 尝试将值序列转换为集合。

    val rndTrans = for (s <- (0 to nStates).toList)
                       yield (new State(s) -> (for (s2 <- 0 to nStates
                           if prob(trans_probability))
                               yield new State(s2)).toSet)
    
    推荐文章