Scala:元组的并行赋值

发布于 2024-08-20 01:16:03 字数 418 浏览 3 评论 0原文

是否可以在 Scala 中并行分配元组成员。如果没有,是否有另一种技术可以完成类似的事情?

val players = List(
    new Player("Django Reinhardt", 42), 
    new Player("Sol Hoopii", 57),
    new Player("Marc Ribot", 64)
)

val winners, losers = players.partition(p => p.score > 50)

// winners = List(Player name:Sol Hoopii score: 57, Player name:Marc Ribot score: 64)
// losers = List(Player name:Django Reinhardt score: 42)

Is it possible to assign tuple members in parallel in Scala. if not is there another technique to accomplish something similar?

val players = List(
    new Player("Django Reinhardt", 42), 
    new Player("Sol Hoopii", 57),
    new Player("Marc Ribot", 64)
)

val winners, losers = players.partition(p => p.score > 50)

// winners = List(Player name:Sol Hoopii score: 57, Player name:Marc Ribot score: 64)
// losers = List(Player name:Django Reinhardt score: 42)

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

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

发布评论

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

评论(1

凹づ凸ル 2024-08-27 01:16:03
val winners, losers = players.partition(p => p.score > 50)

将 (List, List) 元组分配给两个变量。如果你想解压元组,你必须使用

val (winners, losers) = players.partition(p => p.score > 50)

Which 完全符合你的要求。 :-)

val winners, losers = players.partition(p => p.score > 50)

Assignes the (List, List) tuple to two variables. If you want to unpack the tuple you have to use

val (winners, losers) = players.partition(p => p.score > 50)

Which does exactly what you want. :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文