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

发布于 2024-10-03 04:55:24 字数 611 浏览 4 评论 0原文

遇到以下错误:

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 版本。

Sitting with the following error:

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

Can't figure out what to do about it.

The rndTrans is initialized as follows:

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

Update: I happen to be using version 2.7.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

万人眼中万个我 2024-10-10 04:55:24

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

val collection: Seq[SomeType] = ...

Set( collection: _* )

Set() ++ collection

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

When a toSet method (or toMap) is not available (because one is running an older version of scala or because the conversion is just not implemented), one can often apply one of the following schemes.

val collection: Seq[SomeType] = ...

Set( collection: _* )

or

Set() ++ collection

The first version uses the :_* to convert the collection to a sequence argument and then calls a constructor method of the new collection type. The second method created an empty collection of the new type and then adds (++) the old collection to it.

娇纵 2024-10-10 04:55:24

通常,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)

Generally a Seq is not a Set. Try converting the value sequence to a 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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文