Scala:从一种类型的集合转换为另一种类型的集合

发布于 2024-11-26 01:01:49 字数 526 浏览 4 评论 0原文

关于 Scala 中的 yield 命令和以下示例:

val values = Set(1, 2, 3)
val results = for {v <- values} yield (v * 2)

  • 谁能解释一下 Scala 如何知道要屈服于哪种类型的集合?我知道它是基于,但是我该如何编写复制产量的代码呢?
  • 有什么办法可以改变要生成的集合的类型吗?在示例中,我希望结果的类型为List而不是Set
  • 如果做不到这一点,从一个集合转换为另一个集合的最佳方法是什么?我知道 _:*,但由于 Set 不是 Seq 这不起作用。到目前为止我能找到的最好的是val listResults = List() ++ results

    诗。我知道该示例没有遵循推荐的功能方式(即使用map),但这只是一个示例。

  • Concerning the yield command in Scala and the following example:

    val values = Set(1, 2, 3)
    val results = for {v <- values} yield (v * 2)
    
  • Can anyone explain how Scala knows which type of collection to yield into? I know it is based on values, but how would I go about writing code that replicates yield?
  • Is there any way for me to change the type of the collection to yield into? In the example I want results to be of type List instead of Set.
  • Failing this, what is the best way to convert from one collection to another? I know about _:*, but as a Set is not a Seq this does not work. The best I could find thus far is val listResults = List() ++ results.

    Ps. I know the example does not following the recommended functional way (which would be to use map), but it is just an example.

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

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

    发布评论

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

    评论(3

    孤寂小茶 2024-12-03 01:01:49

    for 推导式由编译器翻译为使用 这个方案。

    丹尼尔的精彩回答回答了您的第一个问题。

    要更改结果集合的类型,您可以使用collection.breakout(也在我上面链接的帖子中进行了解释。)

    scala> val xs = Set(1, 2, 3)
    xs: scala.collection.immutable.Set[Int] = Set(1, 2, 3)
    
    scala> val ys: List[Int] = (for(x <- xs) yield 2 * x)(collection.breakOut)
    ys: List[Int] = List(2, 4, 6)
    

    您可以将Set转换为List 使用以下方式之一:

    scala> List.empty[Int] ++ xs
    res0: List[Int] = List(1, 2, 3)
    
    scala> xs.toList
    res1: List[Int] = List(1, 2, 3)
    

    推荐阅读: Scala 的架构收藏

    The for comprehensions are translated by compiler to map/flatMap/filter calls using this scheme.

    This excellent answer by Daniel answers your first question.

    To change the type of result collection, you can use collection.breakout (also explained in the post I linked above.)

    scala> val xs = Set(1, 2, 3)
    xs: scala.collection.immutable.Set[Int] = Set(1, 2, 3)
    
    scala> val ys: List[Int] = (for(x <- xs) yield 2 * x)(collection.breakOut)
    ys: List[Int] = List(2, 4, 6)
    

    You can convert a Set to a List using one of following ways:

    scala> List.empty[Int] ++ xs
    res0: List[Int] = List(1, 2, 3)
    
    scala> xs.toList
    res1: List[Int] = List(1, 2, 3)
    

    Recommended read: The Architecture of Scala Collections

    爱本泡沫多脆弱 2024-12-03 01:01:49

    如果您使用 map/flatmap/filter 而不是推导式,则可以使用 scala.collection.breakOut 来创建不同类型的集合:

    scala> val result:List[Int] = values.map(2*)(scala.collection.breakOut)
    result: List[Int] = List(2, 4, 6)
    

    如果您想构建自己的集合类(这对我来说是最接近“复制收益”的东西),您应该看看 本教程

    If you use map/flatmap/filter instead of for comprehensions, you can use scala.collection.breakOut to create a different type of collection:

    scala> val result:List[Int] = values.map(2*)(scala.collection.breakOut)
    result: List[Int] = List(2, 4, 6)
    

    If you wanted to build your own collection classes (which is the closest thing to "replicating yield" that makes any sense to me), you should have a look at this tutorial.

    生来就爱笑 2024-12-03 01:01:49

    试试这个:

    val values = Set(1, 2, 3)
    val results = for {v <- values} yield (v * 2).toList
    

    Try this:

    val values = Set(1, 2, 3)
    val results = for {v <- values} yield (v * 2).toList
    
    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文